MonoBehaviour.Awake 唤醒

function Awake () : void

Description描述

Awake is called when the script instance is being loaded.

当一个脚本实例被载入时Awake被调用。

Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag . Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

Awake用于在游戏开始之前初始化变量或游戏状态。在脚本整个生命周期内它仅被调用一次.Awake在所有对象被初始化之后调用,所以你可以安全的与其他对象对话或用诸如 GameObject.FindWithTag 这样的函数搜索它们。每个游戏物体上的Awke以随机的顺序被调用。因此,你应该用Awake来设置脚本间的引用,并用Start来传递信息Awake总是在Start之前被调用。它不能用来执行协同程序。

Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor.

C#和Boo用户注意:Awake不同于构造函数,物体被构造时并没有定义组件的序列化状态。Awake像构造函数一样只被调用一次。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	private GameObject target;
	void Awake() {
		target = GameObject.FindWithTag("Player");
	}
}
private var target : GameObject;

function Awake () {
	target = GameObject.FindWithTag ("Player");
}

Awake cannot be a co-routine.

Awake不能用作协同程序.

最后修改:2011年1月2日 Sunday 14:51

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。