纳金网

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

作者: 狂风大尉    时间: 2014-6-24 00: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()
         

           {
         

           }

作者: hyui    时间: 2014-6-24 01:36
Good for beginners!
作者: wucnj    时间: 2014-6-25 09:34
感谢分享!!!
作者: ORZ2013    时间: 2014-6-25 10:11
呵呵,基础功能
作者: hxsdsjr    时间: 2014-7-30 14:55
学习了




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