Unity 脚本组件的操作

Unity 脚本组件的操作_第1张图片
A557E5DA-5C1D-46BA-A9FF-54AE833A16EC.png

CubeScript 源码:

public class CubeScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Debug.Log ("脚本添加成功");
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    void OnDestroy() {
        Debug.Log ("脚本删除成功");
    }
}

源码:

public class test04 : MonoBehaviour {
    //对象
    private GameObject obj;

    // Use this for initialization
    void Start () {
        obj = GameObject.Find ("Cube");
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    void OnGUI() {
        if (GUILayout.Button ("给立方体添加脚本组件", GUILayout.Height (50))) {
            if (obj) {
                obj.AddComponent ();
            }
        }
        if (GUILayout.Button ("删除立方体脚本组件", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj.GetComponent());
            }
        }
        if (GUILayout.Button ("立即删除立方体对象", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj);
            }
        }
        if (GUILayout.Button ("5秒后删除立方体对象", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj, 5);
            }
        }
    }
}

你可能感兴趣的:(Unity 脚本组件的操作)