2012年11月27日 星期二

【程式範例】切換物件

複製程式貼製javascript上再拖曳到攝影機上
把想要切換的物件拖曳至攝影機程式裡的ObjectA、ObjectB、ObjectC、ObjectD
這是範例所以請拖曳四個物件(手邊若沒有物件,可以使用內建的Cube、Capsule..等 內建物件必須使用Prefab
物件重複無所謂,若沒有放滿,執行會發生問題

var ObjectA: GameObject;
var ObjectB: GameObject;
var ObjectC: GameObject;
var ObjectD: GameObject;
var TempActor: String = "";
private var Clone: GameObject;

//http://easyunity.blogspot.com

function Start() {
    SwitchActor("A");
}

function Update() {}

function OnGUI() {
    GUI.BeginGroup(Rect(20, 20, 200, 315));

    GUI.Box(Rect(0, 0, 200, 315), "Actor List");
    if (GUI.Button(Rect(20, 30, 160, 50), "Actor A")) SwitchActor("A");
    if (GUI.Button(Rect(20, 100, 160, 50), "Actor B")) SwitchActor("B");
    if (GUI.Button(Rect(20, 170, 160, 50), "Actor C")) SwitchActor("C");
    if (GUI.Button(Rect(20, 240, 160, 50), "Actor D")) SwitchActor("D");
    GUI.EndGroup();
}

function SwitchActor(mag) {
    if (mag != TempActor) {
        Destroy(GameObject.Find("Actor" + TempActor));
        switch (mag) {
            case "A":
                TempActor = "A";
                Clone = Instantiate(ObjectA, Vector3(0, 0, 0), Quaternion.identity);
                Clone.name = "ActorA";
                print("A");
                break;

            case "B":
                TempActor = "B";
                Clone = Instantiate(ObjectB, Vector3(0, 0, 0), Quaternion.identity);
                Clone.name = "ActorB";
                print("B");
                break;

            case "C":
                TempActor = "C";
                Clone = Instantiate(ObjectC, Vector3(0, 0, 0), Quaternion.identity);
                Clone.name = "ActorC";
                print("C");
                break;

            case "D":
                TempActor = "D";
                Clone = Instantiate(ObjectD, Vector3(0, 0, 0), Quaternion.identity);
                Clone.name = "ActorD";
                print("D");
                break;
        }
    }
}

0 ♥:

張貼留言