編輯模式下,控制對象移動

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   使用   ar   for   div   

  有時候我們可能會有這樣的需求,就是在編輯模式下,控制移動情境中的物體,這裡面有兩個點要解決:

  (1)怎麼在編輯模式下運行一個指令碼;

  (2)怎麼有效地響應滑鼠按鍵。

  第一個問題可以使用編輯器屬性ExecuteInEditMode讓指令碼在編輯模式也能即時地更新。下面的代碼用來控制物體的移動,並且可以在編輯模式下運行:

using UnityEngine;using System.Collections;#if UNITY_EDITORusing UnityEditor;#endif[ExecuteInEditMode]public class MoveComponent : MonoBehaviour{    private CharacterController controller;    private float speed = 3f;    private float rotateSpeed = 50f;    public void Init()    {        controller = transform.GetComponent("CharacterController") as CharacterController;        if (controller == null) Debug.Log("CharacterController not add!");    }    public void MoveForward()    {        if (controller == null) return;        controller.Move(Time.fixedDeltaTime * transform.forward * speed);    }    public void MoveBack()    {        if (controller == null) return;        controller.Move(-Time.fixedDeltaTime * transform.forward * speed);    }    public void TurnLeft()    {        if (controller == null) return;        transform.Rotate(Vector3.up, -Time.fixedDeltaTime * rotateSpeed);    }    public void TurnRight()    {        if (controller == null) return;        transform.Rotate(Vector3.up, Time.fixedDeltaTime * rotateSpeed);    }}

  第二個問題的解決方案是使用EditorWindow的OnGUI來響應KeyDown訊息,下面的代碼通過建立一個測試物體,並為其添加MoveComponent來讓其移動:

using UnityEngine;using UnityEditor;using System.Collections;#if UNITY_EDITORusing UnityEditor;#endifpublic class CameraHelperEditor : EditorWindow{    public static CameraHelperEditor window = null;    [MenuItem("MyEditor/CameraHelperEditor")]    public static void ShowWindow()    {        window = EditorWindow.GetWindow(typeof(CameraHelperEditor), false, "CameraHelperEditor") as CameraHelperEditor;    }    private const string Prefab = "Test/Cube";    private GameObject player = null;    private MoveComponent moveComp = null;    void OnGUI()    {        if (GUILayout.Button("Init"))        {            player = GameObject.Instantiate(Resources.Load(Prefab)) as GameObject;            player.AddComponent("CharacterController");            moveComp = player.AddComponent("MoveComponent") as MoveComponent;            moveComp.Init();        }        switch (Event.current.type)        {            case EventType.keyDown:                {                    if (Event.current.keyCode == KeyCode.W)                    {                        Debug.Log(Time.deltaTime);                        Debug.Log(Time.fixedDeltaTime);                        moveComp.MoveForward();                    }                    else if (Event.current.keyCode == KeyCode.S)                    {                        moveComp.MoveBack();                    }                    else if (Event.current.keyCode == KeyCode.A)                    {                        moveComp.TurnLeft();                    }                    else if (Event.current.keyCode == KeyCode.D)                    {                        moveComp.TurnRight();                    }                }                break;        }    }    void OnDestroy()    {        if(player != null)        {            GameObject.DestroyImmediate(player);            player = null;        }        moveComp = null;    }}

  可以看到按鍵訊息的響應都是放在OnGUI裡面處理的。

  對於該問題的解決我相信還有更好的方法,希望夥伴們能夠留言給予指點。

 

編輯模式下,控制對象移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.