monobehaviour是unity脚本中最常见到的类之一,所有创建的脚本都是默认继承于它。
根据unity官方文档,monobehaviour继承于behaviour ,而behaviour 的描述是*Behaviours are Components that can be enabled or disabled。*这里说明自己创建的脚本就是和其他组件一样。
它有一个 属性叫 *runInEditMode* 意思是在编辑状态下调用,就是可以在脚本未激活状态下调用,估计是做一些初始化的事情(我还没有去试过:)。
它有一大票的*message*用于各种游戏常用事件相应![message的一部分](https://img-blog.csdn.net/20170617234614953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWlhLX0NVSVQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
他们可以被重构(我还没有去试过:)。
它有一些比较重要的继承的成员
- enabled
Enabled Behaviours are Updated, disabled Behaviours are not
2. isActiveAndEnabled
Has the Behaviour had enabled called.
3. gameObject
The game object this component is attached to. A component is always attached to a game object
4.tag
The tag of this game object.
5.transform
The Transform attached to this GameObject.
6.name
The name of the object
实例如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API_10MonoBehaviour : MonoBehaviour {
public Cube_manage go;
// Use this for initialization
void Start () {
Debug.Log(this.isActiveAndEnabled);
Debug.Log(this.enabled);
enabled = false;
Debug.Log(this.gameObject);
Debug.Log(this.name);
Debug.Log(this.tag);
Debug.Log(this.transform);
print("haha");
Debug.Log(go.isActiveAndEnabled);
Debug.Log(go.enabled);
enabled = false;
Debug.Log(go.gameObject);
Debug.Log(go.name);
Debug.Log(go.tag);
Debug.Log(go.transform);
}
// Update is called once per frame
void Update () {
}
}