纳金网

标题: 向场景中动态加入物体(一) [打印本页]

作者: 会飞的鱼    时间: 2011-11-15 14:34
标题: 向场景中动态加入物体(一)


           比较实用的教程,动态加入物体到场景中,很类似于flash中将库中元素实例化。下面是英文教程。
           

           If you work a lot in Flash, you’re used to the process of adding a linkage name to a MovieClip in the library, and then using the new keyword and addChild method to dynamically pull the clip out of the library and display it on the stage.
         

            
         

           You can do something very similar in Unity 3D, but the process is slightly different. Be sure to read our Flash to Unity 3D Glossary if you’re coming over to Unity 3D from Flash.
           

           1. Create the GameObject
           

           1、创建物体
           

           We’ll make it real simple. Click GameObject > Create Other > Cube in the menus to add a cube primitive to the Scene.
         









            
         

           A cube appears somewhere in your Scene…
         




           … and the Cube GameObject is listed in the Hierarchy.
         

           2. Create an Empty Prefab
           

           2、创建空模板
         

           A Prefab is a lot like a Flash MovieClip. It’s going to contain our Cube GameObject. To create the Prefab, click the Create button in the Project panel, and choose Prefab from the resulting menu.
         




           Click here …
         




           and then here …
         




           … to get this.
         

           Right/alternate-click on the Prefab, or click and hold (Mac)/press F2 (PC) to rename the Prefab. I called mine “Cube”. This won’t create a naming conflict or anything.
           

           3. Click/drag the Cube into the Prefab
           

           3、将前面创建的物体加入模板中,类似于在flash中创建mc
           

           Click and drag the Cube GameObject from the Hierarchy panel into the empty grey Prefab you just created in the Project panel. The Prefab lights up blue to indicate that it’s got junk in the trunk.
         




           It’s now safe to delete the Cube GameObject from the Scene (via the Hierarchy panel) by highlighting it and pressing Delete (PC) or CMD+Delete (Mac). The Cube is safe and sound inside the Prefab.
           

           4. Create a new Javascript
           

           4、创建js脚本
           

           Our Cube Prefab is ready to be instantiated (instantiate == make an instance [copy] of). Let’s create a javascript to do the instantiating. In the Project panel, click Create > Javascript to make a new script.
         














            
         

            
         

           Name the javascript something sensible. Then, click/drag the script onto the Main Camera. (We do this because our script won’t execute unless it’s hooked up to a GameObject in the Scene.)
           

           5. Store a Reference to the Prefab
           

           5、创建一个参考,指向模板物体
           

           This is the fanciest/weirdest thing we’ll do in the entire tutorial.
           

           Double-click the script to open it up in Unitron or Uniscite (depending on whether you have a Mac or a PC).
           

           Declare a variable at the top of the script called myCube:
         

           var myCube : GameObject;
           

            function Update()
           

           {
           

           }
           

           Save the script and close the editor.
         

           In order to store a reference to the cube Prefab, we need to click and drag it into the variable we just created.
         

            
         

           In the Hierarchy panel, click on the Main Camera, which is the GameObject to which we attached our script.
           

           In the Inspector panel, look for the script component. You’ll see the myCube variable we created, and next to it is a piece of text that says None (Game Object)
         




           What in the Hells?
         

           myCube is the name of the variable (bucket), and the None (Game Object) is where the value goes.
         

           Now, you need to click and drag the Cube Prefab from the Project panel into that value area that i’ve highlighted.
         




           This is the result:
         




           This is going to take some getting used to.
         

           It’s weird. i know.
         

           NOTE: You called the variable “myCube”, but it gets displayed in the IDE as “My Cube”. Why is that? Chalk it up to a Unity idiosyncrasy that i’m not a huge fan of. Check the script. Your variable is still called “myCube” where it counts.
           

           6. Instantiate the Prefab
           

           6、实例化模板物体,主要是这句代码:Instantiate(myCube, pos, rot);
           

           Now, with a reference to that Prefab, you’re all ready to go. Double-click to edit your script again, and punch in the following code:
         

           var myCube : GameObject;
         

           function Start()
         

           {
         

                var pos : Vector3(0,0,0); // this is where the Cube will appear when it's instantiated
         

                var rot : Quaternion = Quaternion.identity; // Quaternion.identity essentially means "no rotation"
         

                Instantiate(myCube, pos, rot); // The Instantiate command takes a GameObject, a Vector3 for position and a Quaternion for rotation.
         

           }
         

            function Update()
         

           {
         

           }
           

            
         

作者: 菜刀吻电线    时间: 2012-1-21 23:30
林志玲,娇滴滴,祝你新年一定要好好的啦!小沈阳,笑嘻嘻,祝你新年一定要杠杠的哈!春晚群星,闹哄哄的,祝你新年辞旧迎新开创新纪元!

作者: tc    时间: 2012-2-23 23:22
呵呵,很漂亮啊

作者: 菜刀吻电线    时间: 2012-3-1 23:32
很经典,很实用,学习了!

作者: 菜刀吻电线    时间: 2012-3-11 23:31
頂。。。

作者: 晃晃    时间: 2012-5-8 23:20
读铁系缘分,顶铁系友情

作者: 晃晃    时间: 2012-9-24 23:18
提醒猪猪,千万不能让你看见

作者: 晃晃    时间: 2012-10-2 23:26
我看看就走,你们聊!





欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5