查看: 1468|回复: 4
打印 上一主题 下一主题

[其他] 向场景中动态加入物体

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-6-24 00:34:42 |只看该作者 |倒序浏览
   比较实用的教程,动态加入物体到场景中,很类似于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()
         

           {
         

           }
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

hyui    

1

主题

2

听众

6671

积分

高级设计师

Rank: 6Rank: 6

纳金币
2715
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-6-24 01:36:55 |只看该作者
Good for beginners!
回复

使用道具 举报

wucnj    

1

主题

1

听众

3160

积分

中级设计师

Rank: 5Rank: 5

纳金币
1065
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-6-25 09:34:16 |只看该作者
感谢分享!!!
回复

使用道具 举报

0

主题

1

听众

860

积分

初级设计师

Rank: 3Rank: 3

纳金币
0
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2014-6-25 10:11:56 |只看该作者
呵呵,基础功能
回复

使用道具 举报

0

主题

1

听众

569

积分

初级设计师

Rank: 3Rank: 3

纳金币
155
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2014-7-30 14:55:56 |只看该作者
学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-16 09:17 , Processed in 0.103897 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部