属性:RequireComponent

RequireComponent的作用:
当Test.cs脚本挂载到对象上时,自动添加依赖的组件或其他脚本到该对象上
依赖unity组件:

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]//依赖Button
public class Test : MonoBehaviour
{
	Button myButton;
    // Start is called before the first frame update
    void Start()
    {
        myButton = GetComponent <Button> (); 
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

依赖脚本:

using UnityEngine;
RequireComponent(typeof(Test))]
public class Test2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}
发布了24 篇原创文章 · 获赞 0 · 访问量 639

猜你喜欢

转载自blog.csdn.net/u014589770/article/details/105434284