查看: 1807|回复: 7
打印 上一主题 下一主题

向场景中动态加入物体(一)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

跳转到指定楼层
楼主
发表于 2011-11-15 14:34:00 |只看该作者 |倒序浏览


           比较实用的教程,动态加入物体到场景中,很类似于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
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

沙发
发表于 2012-1-21 23:30:30 |只看该作者
林志玲,娇滴滴,祝你新年一定要好好的啦!小沈阳,笑嘻嘻,祝你新年一定要杠杠的哈!春晚群星,闹哄哄的,祝你新年辞旧迎新开创新纪元!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-2-23 23:22:11 |只看该作者
呵呵,很漂亮啊
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-3-1 23:32:33 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

5#
发表于 2012-3-11 23:31:08 |只看该作者
頂。。。
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-5-8 23:20:18 |只看该作者
读铁系缘分,顶铁系友情
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

7#
发表于 2012-9-24 23:18:50 |只看该作者
提醒猪猪,千万不能让你看见
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-10-2 23:26:02 |只看该作者
我看看就走,你们聊!
回复

使用道具 举报

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

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

GMT+8, 2024-9-21 21:43 , Processed in 0.136830 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部