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