- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
比较实用的教程,动态加入物体到场景中,很类似于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()
{
}
|
|