1. 程式人生 > 其它 >【Unity】指令碼:UI介面實現基本按鍵操作物體移動

【Unity】指令碼:UI介面實現基本按鍵操作物體移動

點選UI介面摁按鈕,實現對應移動等功能

    private void Update()
    {
        time = Time.time;
        if(time_go == 1)
        {
            this.transform.position += Vector3.forward * Time.deltaTime;
            
        }
    }
    private void OnGUI()
    {
        if (GUILayout.Button("Tag"))
        {
            //print("ok");
            print(this.tag);
        }
        if (GUILayout.Button("位置重置"))
        {
            this.transform.position = new Vector3(0, 0, (float)-6.57);
        }
        if (GUILayout.Button("Z方向移動10m"))
        {
            this.transform.position = new Vector3(0, 0, 10);
        }
        if(GUILayout.Button("持續移動"))
        {
            time_go = 1;
        }
        if (GUILayout.Button("停止移動"))
        {
            time_go = 0;
        }
        if (GUILayout.Button("變成紅色"))
        {
            this.GetComponent<MeshRenderer>().material.color = Color.red;
        }
    }

以上