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()
{
}
}