MonoBehaviour base class and Component component in Unity

MonoBehaviour base class and Component component

1.MonoBehaviour base class

MonoBehaviour is derived from component script, so all public and protected properties, member variables, methods and other functions of component script are also available in MonoBehaviour. After inheriting mono, these functions can be mounted on game objects.

public class No4_MonoBehaviour : MonoBehaviour
{
    
    
    void Start()
    {
    
      
        Debug.Log("No4_MonoBehaviour组件的激活状态是:"+this.enabled);
        Debug.Log("No4_MonoBehaviour组件挂载的对象名称是:" + this.name);
        Debug.Log("No4_MonoBehaviour组件挂载的标签名称是:" + this.tag);
        Debug.Log("No4_MonoBehaviour组件是否已激活并启用Behaviour:" + this.isActiveAndEnabled);
        print("Trigger");
        
        //常使用的一些方法
        //Destroy();
        //FindObjectsOfType();
        //Instantiate();
    }
}

2.Component component

Mono inherits from Behavior, Behavior inherits from Component, and Component inherits from Object. Children have all the public and protected properties, member variable methods and other functions of their parents and above (lowly derived base classes), and are mounted The function is actually Component, that is, the script component we write actually refers to the Component component, and Mono is encapsulated and extended on this basis.
The use and acquisition of components:
a. Components are mounted on a certain game object and can be used after searching and obtaining them through the game object
b. Search through other components

(1). The use of this script component itself

public class No5_Component : MonoBehaviour
{
    
    
    public int testValue;
    void Start()
    {
    
    
        No5_Component no5_Component = gameObject.GetComponent<No5_Component>();
        Debug.Log(no5_Component.testValue);//输出no5_Component组件中testValue的值
    }
}

(2). The script is not mounted on the object (wrong usage)

public class No5_Component : MonoBehaviour
{
    
    

    void Start()
    {
    
    
        //注意:本游戏物体未挂载No2_EventFunction脚本组件
        No2_EventFunction no2_EventFunction = gameObject.GetComponent<No2_EventFunction>();
        Debug.Log(no2_EventFunction);
        //输出为NULL,因为这里未获取No2_EventFunction脚本组件。
        Debug.Log(no2_EventFunction.attackValue);
        //输出会报错。因为未获取组件No2_EventFunction并想要调取里面的成员变量或方法会报错
    }
}

(3). The game object is not mounted in this script component

public class No5_Component : MonoBehaviour
{
    
    

    void Start()
    {
    
    
        //查找获取名称为Gris的游戏物体(未引用)
        GameObject grisGo = GameObject.Find("Gris");
        Debug.Log(grisGo.GetComponent<SpriteRenderer>());//输出grisGo获取到游戏物体的SpriteRenderer组件
    }
}

(4).Game objects are referenced

public class No5_Component : MonoBehaviour
{
    
    
    //将一个游戏物体拖到此脚本的enemyGos进行引用(带子物体的父物体)
    public GameObject fatherGameObject;//获取父游戏物体
    public GameObject sonGameObject;//获取子游戏物体

    void Start()
    {
    
    
        //有对象被脚本引用
        //GetComponentInChildren
        Debug.Log(fatherGameObject.GetComponentInChildren<BoxCollider>());//输出enemyGos中子物体和父物体的第一个BoxCollider组件(从上往下遍历的第一个)
        //GetComponentsInChildren
        Debug.Log(fatherGameObject.GetComponentsInChildren<BoxCollider>());//输出enemyGos中子物体和父物体的所有BoxCollider组件的数组()

        //GetComponentInParent
        Debug.Log(sonGameObject.GetComponentInParent<BoxCollider>());//输出enemyGos中子物体的父物体

        //b.通过其他组件查找
        SpriteRenderer sr= grisGo.GetComponent<SpriteRenderer>();
        sr.GetComponent<Transform>();
        this.GetComponent<Transform>();
    }
}

(5). Search through other components

public class No5_Component : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        //b.通过其他组件查找
        GameObject grisGo = GameObject.Find("Gris");
        SpriteRenderer sr= grisGo.GetComponent<SpriteRenderer>();
        sr.GetComponent<Transform>();//通过SpriteRenderer调用Transform
        
        //获取自身组件
        //this.GetComponent<Transform>(); == GetComponent<Transform>();
    }
}

おすすめ

転載: blog.csdn.net/qq_43007056/article/details/130772564