Unity中MonoBehaviour基类与Component组件

MonoBehaviour基类与Component组件

1.MonoBehaviour基类

MonoBehaviour派生自组件脚本,因此组件脚本所有的公有,保护的属性,成员变量,方法等功能,MonoBehaviour也都有,继承mono之后这类可以挂载到游戏物体上。

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组件

Mono继承自Behaviour,Behaviour继承自Compontent,Compontent继承自Object子辈拥有父辈以及父辈以上(派生程度低的基类)所有的公有,保护的属性,成员变量方法等功能,挂载功能其实是Component,也就是我们写的脚本组件其实指的是Component组件,而Mono是在此基础上进行的封装与扩展。
组件的使用与获取:
a.组件都是在某一个游戏物体身上挂载的,可以通过游戏物体查找获取之后使用
b.通过其他组件查找

(1).在此脚本组件自身的使用

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).脚本未挂载在本物体的使用(错误用法)

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).游戏物体未挂载在本脚本组件

public class No5_Component : MonoBehaviour
{
    
    

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

(4).游戏物体被引用

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).通过其他组件查找

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