Unity 关于脚本的内容

添加脚本、删除脚本、调用脚本的方法、给脚本的方法传递参数。

效果图:
5EA74BA01A3F309B804E31A5E378AE43

脚本代码:
C# :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour {

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

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

    public void thePublicMethod() {
        Debug.Log ("您执行了脚本的公开方法");
    }

    public void thePublicMethod02(string value1, string value2) {
        Debug.Log ("您执行了脚本的公开方法 02 value1 : "+ value1 + "  value2 : "+ value2);
    }

    void thePriveMethod() {
        Debug.Log ("您执行了脚本的私有方法");
    }

    void thePriveMethod02(string value) {
        Debug.Log ("您执行了脚本的私有方法 02 value : " + value);
    }
    void thePriveMethod03(string [] values) {
        Debug.Log ("您执行了脚本的私有方法 03");
        foreach (string value in values) {
            Debug.Log ("参数 => " + value);
        }
    }
}

执行代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoreIt : MonoBehaviour {

    GameObject obj;

    void Start () {
        obj = GameObject.Find ("Cube");
    }

    void OnGUI () {
        if (GUILayout.Button("给方块添加脚本",GUILayout.Height(50))) {
            if (obj) {
                obj.AddComponent<test>();
            }
        }

        if (GUILayout.Button("删除方块的脚本",GUILayout.Height(50))) {
            if (obj) {
                Destroy(obj.GetComponent<test>());
            }
        }

        if (GUILayout.Button("立即删除方块对象",GUILayout.Height(50))) {
            if (obj) {
                Destroy(obj);
            }
        }

        if (GUILayout.Button("5秒后,删除方块对象",GUILayout.Height(50))) {
            if (obj) {
                Destroy(obj,5);
            }
        }


        GUILayout.BeginArea (new Rect (Screen.width / 2, 0, Screen.width/2, Screen.height));

        if (GUILayout.Button ("执行脚本里面的公开方法", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    ts.thePublicMethod ();
                }
            }
        }

        if (GUILayout.Button ("另一种,执行脚本里面的公开方法", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    ts.SendMessage ("thePublicMethod");
                }
            }
        }

        if (GUILayout.Button ("执行脚本里面的私有方法", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    ts.SendMessage ("thePriveMethod");
                }
            }
        }

        if (GUILayout.Button ("执行脚本里面的公开方法 02,并且传递参数", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    ts.thePublicMethod02 ("木子才", "Muzico");
                }
            }
        }

        if (GUILayout.Button ("执行脚本里的私有方法 02,并且传递一个参数", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    ts.SendMessage ("thePriveMethod02", "木子才");
                }
            }
        }

        if (GUILayout.Button ("执行脚本里的私有方法 03,并且传递多个参数", GUILayout.Height (50))) {
            if (obj) {
                test ts = obj.GetComponent<test> ();
                if (ts) {
                    string[] values = new string[] {"木子才","普特斯","圣华尔"};
                    ts.SendMessage ("thePriveMethod03", values);
                }
            }
        }

        GUILayout.EndArea ();
    }
}