在Unity3D中,游戏对象(GameObject)的行为是由附加其上的脚本来控制的,游戏开发者通过编写脚本来控制游戏中的所有对象,如移动Camera等。GameObject可以被附加不同类型的组件,但每种类型的组件只能有一个或没有。脚本本质上也是一种组件。
在Unity3D中默认的脚本代码如下所示:
// ***** C# script *****
using UnityEngine;
using System.Collections;
public class ffc : MonoBehaviour {
// Use this for initialization
void Start () {
}
/ Update is called once per frame
void Update () {
}
}
//***** Java script *****
#pragma strict
function Start () {
}
function Update () {
}
由此可见,脚本代码与大家熟悉的Java代码类似,即都是由以下两部分组成:
• 变量
• 函数
• 其它代码:在任何函数之外的代码在物体被加载的时候运行。这个可以用来初始化脚本状态。
MonoBehaviour是所有脚本的基类,每个Javascript脚本自动继承MonoBehaviour,使用C#或Boo时,需要显式继承MonoBehaviour.
脚本变量就是指类的成员变量(即在JavaScript或C#或Boo中定义的成员变量,而不是基类MonoBehaviour中定义的变量),在Unity3D中将成员变量设为公有的时候,当把它附加到游戏对象后,可以在游戏对象的监视面板中的脚本组件那栏里面看到该“公有变量”,即可以在编辑器中直接对该公有变量进行赋值,同时在Debug状态下也可以在面板中看到它的值。
变量名 | 描述 |
transform | The Transform attached to this GameObject (null if there is none attached). |
rigidbody | The Rigidbody attached to this GameObject (null if there is none attached). |
camera | The Camera attached to this GameObject (null if there is none attached). |
light | The Light attached to this GameObject (null if there is none attached). |
animation | The Animation attached to this GameObject (null if there is none attached). |
constantForce | The ConstantForce attached to this GameObject (null if there is none attached). |
renderer | The Renderer attached to this GameObject (null if there is none attached). |
audio | The AudioSource attached to this GameObject (null if there is none attached). |
guiText | The GUIText attached to this GameObject (null if there is none attached). |
networkView | The NetworkView attached to this GameObject (Read Only). (null if there is none attached) |
guiTexture | The GUITexture attached to this GameObject (Read Only). (null if there is none attached) |
collider | The Collider attached to this GameObject (null if there is none attached). |
hingeJoint | The HingeJoint attached to this GameObject (null if there is none attached). |
particleEmitter | The ParticleEmitter attached to this GameObject (null if there is none attached). |
gameObject | The game object this component is attached to. A component is always attached to a game object. |
tag | The tag of this game object. |
类函数 | 描述 |
Destroy | Removes a gameobject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. It is strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Makes the object target not be destroyed automatically when loading a new scene. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Returns a list of all active loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
类函数 | 描述 |
GetComponent | Returns the component ofType type if the game object has one attached, null if it doesn't. function GetComponent (type :Type) : Component |
GetComponent | Returns the component withnametype if the game object has one attached, null if it doesn't. function GetComponent (type :string) : Component |
GetComponentInChildren | Returns the component of Type type in the GameObject or any of its children using depth first search. |
GetComponentsInChildren | Returns all components of Type type in the GameObject or any of its children. |
GetComponents | Returns all components of Type type in the GameObject. |
CompareTag | Is this game object tagged tag? |
SendMessageUpwards | Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour |
SendMessage | Calls the method named methodName on every MonoBehaviour in this game object. |
BroadcastMessage | Calls the method named methodName on every MonoBehaviour in this game object or any of its children. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the game object. |
函数名 | 描述 |
Update | Update is called every frame, if the MonoBehaviour is enabled. |
LateUpdate | LateUpdate is called every frame, if the Behaviour is enabled. LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update. |
FixedUpdate | This function is called every fixed framerate frame, if the MonoBehaviour is enabled. |
函数名 | 描述 |
Awake | Awake is called when the script instance is being loaded. |
Start | Start is called just before any of the Update methods is called the first time. |
Reset | Reset to default values. |
函数名 | 描述 |
OnMouseEnter | nMouseEnter is called when the mouse entered the GUIElement or Collider. |
OnMouseOver | OnMouseOver is called every frame while the mouse is over the GUIElement or Collider. |
OnMouseExit | OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider. |
OnMouseDown | OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. |
OnMouseUp | OnMouseUp is called when the user has released the mouse button. |
OnMouseUpAsButton | OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed. |
OnMouseDrag | OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse. |
函数名 | 描述 |
OnTriggerEnter | OnTriggerEnter is called when the Collider other enters the trigger. |
OnTriggerExit | OnTriggerExit is called when the Collider other has stopped touching the trigger. |
OnTriggerStay | OnTriggerStay is called once per frame for every Collider other that is touching the trigger. |
函数名 | 描述 |
OnCollisionEnter | OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. |
OnCollisionExit | OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider. |
OnCollisionStay | OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider. |
在Unity3D中,可以在监视器面板中修改物体的组件属性,但是更多的时候,需要使用脚本来进行动态操作。
最常见的一个情形是需要使用脚本访问附加到相同游戏对象(GameObject)上的另一个组件(当前脚本就是一个组件,其他的组件也就是另一个组件了)。一个组件实质上是一个类的实例,因而首先需要做的是获取想要操作的组件实例的引用。这个通过GetComponent函数来实现。典型的,可能会想要将一个组件赋值给一个变量,如下代码所示:
void Start () {
Rigidbody rb = GetComponent();
}
如果想要访问另一个脚本文件,也可以使用GetComponent,只需使用脚本的类名作为该函数的组件类型参数(因为脚本本来就也是一个组件)。
// You can access script components in the same way as other components.
function Start () {
var someScript : ExampleScript;
someScript = GetComponent (ExampleScript);
someScript.DoSomething ();
}
由于一些组件类型经常使用,unity提供了一些内置的变量来访问它们,参见1.2(内置变量),其示例代码如下:
void Start () {
transform.position = Vector3.zero;
}
虽然游戏对象(GameObject)都有各自的组件(包括脚本)进行处理,使用代码进行跟踪其他物体是常有的事。例如,一个追赶的敌人可能需要知道玩家的位置,Unity提供了一系列的方法来获取其他对象,以适合不同的场合。
例如将一个游戏对象拖给一个Transform的成员变量,就会自动的将游戏对象的Transform组件和该变量映射起来。
直接将对象和变量链接起来在处理需要有永久链接的对象的时候是最有用的方法。同时也可以使用一个数组变量和几个相同类型的对象链接起来,但是这种链接必须在Unity3D编辑器中完成,而不能在运行时进行。
public class WaypointManager : MonoBehaviour {
public Transform waypoints;
void Start() {
waypoints = new Transform[transform.childCount];
int i = 0;
for (Transform t in transform) {
waypoints[i++] = t;
}
}
}
同时也可以使用Tranfrom.Find来查找某个具体的子对象。使用Transform来进行对象查找操作是因为每一个游戏对象都有Transfrom组件。
GameObject player;
void Start() {
player = GameObject.Find("MainHeroCharacter");
}
HingeJoint[ ] hinges = FindObjectsOfType(typeof(HingeJoint)) as HingeJoint[ ];
实例化更多通常用于实例投射物(如子弹、榴弹、破片、飞行的铁球等),AI敌人,粒子爆炸或破坏物体的替代品。
// Instantiates 10 copies of prefab each 2 units apart from each other
var prefab : Transform;
for (var i : int = 0;i < 10; i++) {
Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}
值得注意的是用于进行拷贝的对象并不一定需要放置在场景中。更普遍的做法是将一个预设(Prefab)拖到脚本的对应公有成员变量上,实例化的时候直接对这个成员变量进行实例化即可。
// Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
function Update () {
// Ctrl was pressed, launch a projectile
//按Ctrl发射炮弹
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
//在该变换位置和旋转,实例化炮弹
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current object's Z axis
//沿着当前物体的Z轴给克隆的物体一个初速度。
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}
同时也有一个Destroy函数在帧更新函数完成后或设定的一个延时时间后销毁一个对象。
// Kills the game object
//销毁游戏物体
Destroy (gameObject);
// Removes this script instance from the game object
//从游戏物体删除该脚本实例
Destroy (this);
// Removes the rigidbody from the game object
//从游戏物体删除刚体
Destroy (rigidbody);
// Kills the game object in 5 seconds after loading the object
//加载物体5秒后销毁游戏物体
Destroy (gameObject, 5);
// When the user presses Ctrl, it will remove the script
// named FooScript from the game object
//当按下Ctrl将从游戏物体删除名为FooScript的脚本
function Update () {
if (Input.GetButton ("Fire1") && GetComponent (FooScript))
Destroy (GetComponent (FooScript));
}
注意到Destroy函数可以销毁单独的组件而不对游戏对象本身产生影响,一个通常易犯的错误是Destroy(this); 这句代码仅仅销毁脚本组件,而不销毁该脚本所附加在的对象。
IEnumerator Fade() {
for (float f = 1f; f <= 0; f -= 0.1f) {
Color c = renderer.material.color;
c.alpha = f;
renderer.material.color = c;
yield return;
}
}
void Update() {
if (Input.GetKeyDown("f")) {
StartCoroutine("Fade");
}
}
IEnumerator Fade() {
for (float f = 1f; f <= 0; f -= 0.1f) {
Color c = renderer.material.color;
c.alpha = f;
renderer.material.color = c;
yield return new WaitForSeconds(.1f);
}
}
参考:http://game.ceeger.com/Script/