如何加载脚本,在游戏物体材料运行时Unity3d? [英] How to load scripts, materials on GameObject in runtime in Unity3d?

查看:441
本文介绍了如何加载脚本,在游戏物体材料运行时Unity3d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来Unity3d和JS脚本和我需要帮助symplify在我开发这里的应用场景,code是我的场景的概念:

I'm new to Unity3d and in JS scripting and I need help to symplify a scene and code in the app I'm developing Here is the concept of my scene:

有一个摄像头,通过屏蔽层,三架飞机看到。也有充当交换纹理的材料,而且分别运行此交换上的每个平面上的脚本。为了运行此脚本,我使用GUI与滚动列表按钮,所以当button1的是pssed $ P $,摄像头只能看到基准面和SCRIPT1运行在这个平面纹理material1。同样的情况在其他2架由pressing相应的按钮。你可以得到这个概念的视图中附加的图像1:

There is a Camera, that sees through layer masking, three planes. Also there is a material that acts as a swapping texture, and a script that runs this swapping respectively on each plane. In order to run this script, I use a GUI with a scroll list buttons, so when button1 is pressed, camera sees only plane1 and the script1 runs the texture material1 on this plane. The same happens on the other 2 planes by pressing the corresponding buttons. You can get a view of this concept in the attached image 1:

我需要你的帮助,是只保留一个平面相机待观察,并安装相应的脚本(1,2,3等),材料质地(1,2,3,以简化这个场景等),在此同一平面上,当玩家触摸相应的GUI按钮,在运行时。需要提及的是,虽然在GUI按钮是在名为GUI连接到一个空的游戏物体和脚本和材料,我需要一个动态附加,是一个名为动画作为一个孩子的Camera游戏对象的脚本。你可以得到附加的图像2这一概念的看法。

What I need your help for, is to simplify this scene by keeping only one plane for the camera to be seen, and attaching the respective scripts (1, 2, 3 etc), material as texture (1, 2, 3 etc) on this same plane when the player touches the corresponding GUI buttons, in runtime. Need to mention though that the GUI buttons are on a script named "GUI" attached to an empty GameObject and the script and materials I need to a dynamically attach, are on a GameObject named "Animator" as a child to a Camera. You can get a view of this concept in the attached image 2.

相信这种功能可以通过使用例如可实现gameObject.AddComponent(myScript的),但我是一个小白,我不能做到这一点。此外,我不知道如何运用不同的材料上这架飞机在运行。所以,我需要一些帮助和编码的例子也许,因为我觉得它难以遵循只提示没有看到一个例子。

I believe this functionality can be achieved by using e.g. "gameObject.AddComponent ("myScript")" , but as I'm a noob, I cannot make it happen. Furthermore, I do not know how to apply the different material on this plane in runtime. So I need some help and a coding example maybe, as I find it difficult to follow only tips without seeing an example.

感谢大家的时间阅读这篇文章,希望有人友好地帮助我在这个问题上。

Thank you all for your time reading this post, hope someone be kind enough to help me on this matter.

修改1

好吧,为了使其更加融为一体prehensive对任何人都愿意帮助,我可以通过自己的答案,我张贴层次和督察的截图,再加上统一的code片段事后帮助项目。

Well, in order to make it more comprehensive for anyone willing help, and for me be helped afterwards by their answers, I'm posting screenshots of Hierarchy and Inspector, plus a code snippet of Unity project.

在图像3,视图层次的,我表明,剧本ShowAnimation是游戏物体Animation_Buttons_Controller。通过这个剧本,我想访问例如游戏对象子Animator2inScene2父游戏物体AnimationCamera。

In image 3, view of Hierarchy, I'm showing that, script "ShowAnimation" is on gameObject "Animation_Buttons_Controller". Through this script, I want to access e.g. gameObject child "Animator2inScene2" in parent gameObject "AnimationCamera".

在图像4,鉴于督察,我展示的物质元素Texture_Animator2_Material,而应该在这个游戏对象只有在运行时添加脚本ScriptTextureAnimator2。

In image 4, view of Inspector, I'm showing the material element "Texture_Animator2_Material", and the script "ScriptTextureAnimator2" that should be added on this gameObject only in runtime.

以下是脚本中的code片段ShowAnimation.js有访问游戏对象子Animator2inScene2,并添加物质元素Texture_Animator2_Material,并在此游戏物体孩子脚本ScriptTextureAnimator2

Following is the code snippet in script "ShowAnimation.js" that has to access gameObject child "Animator2inScene2" and add material element "Texture_Animator2_Material" and the script "ScriptTextureAnimator2" on this gameObject child:

if (GUI.Button (Rect (2,2,100,20), "PlayAnimation2")) 
            {
            var addScriptOnPlane2 : GameObject[] = GameObject.FindGameObjectsWithTag("Animator");     
            //for(var addTextureScript2 : GameObject in addScriptOnPlane2)

if(addScriptOnPlane2)
            { 

            // Adds the script named ScriptTextureAnimator2 to the game object child "Animator2inScene2"
            gameObject.AddComponent("ScriptTextureAnimator2");

            // Adds the script named Texture_Animator2_Material to the game object child "Animator2inScene2"
            var addMaterial : Material;

            addMaterial = Resources.Load("Materials/Materials/Texture_Animator2_Material",typeof(Material)) as Material;

}
}

问题是,玩,我得到鬼键无功能和控制台以下错误:

Problem is that on play, I get no function on gui button and the following error in console:


的NullReferenceException:未将对象引用设置到对象的实例
ScriptTextureAnimator2.FixedUpdate()

" NullReferenceException: Object reference not set to an instance of an object ScriptTextureAnimator2.FixedUpdate ()"

我知道,我已经错过了一些东西编译至关重要,但什么可能有多大呢?请看看,并张贴在我的作弄建议。谢谢你。

I know, I've missed something vital for compilation, but what may that be? Please take a look and post a recommendation on my codding. Thank you.

推荐答案

我真的AP preciate您制作出的照片这一问题,从而使之更愉快的阅读:)

I really appreciate you for making pictures out of this question, thus making it more enjoyable to read :)

要在运行时改变你的脚本,你需要摧毁旧脚本,并添加新的:

To change your script at runtime, you need to destroy the old script and add the new one:

破坏旧脚本:

Destroy(plane.GetComponent(TestScript)); // get component of type TestScript

添加新的脚本:

plane.AddComponent(TestScript); // add component of type TestScript

要在运行时更改材料:

prepare全局变量和从检查材料为它分配:

Prepare global variables for your material and assign it with the materials from the inspector:

var material1 : Material;
var material2 : Material;
var material3 : Material;

然后,你可以改变你的材料,你选择的是这样的材料:

Then you can just change your material to the material of your choice like this:

plane.renderer.material = material1; //改变飞机材料material1

看到你的榜样,这些脚本将在GUI按钮把里面。结果
所以你需要做的GUI按钮脚本内的公共游戏对象变量,以便您可以将平面分配给它。结果
请注意,在code以上我总是写平面作为基类,这就是我的意思大概在GUI按钮大众游戏对象。

Seeing your example, these scripts will be put inside the GUI Button.
So you'll need to make a public game object variable inside GUI button scripts so that you can assign your plane to it.
Notice that in the code above I always write plane as the base class, that is what I meant about the public game object in the GUI button.

只是声明这样的平面游戏对象变量:

Just declare the plane game object variable like this:

var plane : GameObject;

和在检查分配给它的平面物体。

And assign it your plane object at the inspector.

希望这有助于!

编辑1:结果
该错误可能是由您的code段的错位造成的。结果
你已经放置在您的函数 OnGUI()?结果
我已修改了code,以满足您的需要:

EDIT 1:
That error is possibly caused by misplacement of your code snippet.
Have you placed your function to OnGUI()?
I have modified your code to match your needs:

function OnGUI () {
    if (GUI.Button (Rect (2,2,100,20), "PlayAnimation2")){
        var addScriptOnPlane2 : GameObject[] = GameObject.FindGameObjectsWithTag("Animator");     

        for(var addTextureScript2 : GameObject in addScriptOnPlane2)
        { 
            // Adds the script named ScriptTextureAnimator2 to the game object child "Animator2inScene2"
            addTextureScript2.AddComponent("ScriptTextureAnimator2");

            // Adds the script named Texture_Animator2_Material to the game object child "Animator2inScene2"
            var addMaterial : Material;

            addMaterial = GameObject.Instantiate(Resources.Load("Materials/Materials/Texture_Animator2_Material",typeof(Material))) as Material;

            addTextureScript2.renderer.material = addMaterial;
        }
    }
}

请注意,为脚本和物质添加到你想要的游戏对象,则必须,如果你想从其他游戏物体做指定游戏物体的变量。

Note that to add the script and material to the gameobject you want, you must specify the variable of that gameobject if you want to do it from other gameobject.

gameObject.AddComponent("ScriptTextureAnimator2"); 
// gameObject = this.gameObject = Animation_Buttons_Controller gameobject
// change it to addTextureScript2.AddComponent("ScriptTextureAnimator2"); 

我也分配哪些你没有做过的材料。结果
请注意, Resources.Load 将在资产资源 C>,所以一定要确保你把你的材料在那里。

I have also assign the material which you haven't done before.
Please note that Resources.Load will search for directory Resources in Assets, so make sure you put your material in there.

这篇关于如何加载脚本,在游戏物体材料运行时Unity3d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆