Unity 脚本组件的操作

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<CubeScript> ();
            }
        }
        if (GUILayout.Button ("删除立方体脚本组件", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj.GetComponent<CubeScript>());
            }
        }
        if (GUILayout.Button ("立即删除立方体对象", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj);
            }
        }
        if (GUILayout.Button ("5秒后删除立方体对象", GUILayout.Height (50))) {
            if (obj) {
                Destroy (obj, 5);
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容