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);
}
}
}
}