Unity编辑器扩展

代码放在Assets/Editor中:

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> MyWindow : EditorWindow {
<span style="color: #0000ff">const <span style="color: #0000ff">string MenuName = <span style="color: #800000">"<span style="color: #800000">Atwal Code Snippet<span style="color: #800000">"<span style="color: #000000">;
<span style="color: #0000ff">enum<span style="color: #000000"> Code_Type
{
input_code,rigidbody_force_code,camera_follow
}

</span><span style="color: #0000ff"&gt;static</span> Code_Type type = <span style="color: #800080"&gt;0</span><span style="color: #000000"&gt;;
</span><span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;string</span> Code_Name = <span style="color: #800000"&gt;""</span><span style="color: #000000"&gt;;

[MenuItem(MenuName </span>+ <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/H&amp;V Input</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)]
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; Code_HVInput() {
    type </span>=<span style="color: #000000"&gt; Code_Type.input_code;
    Code_Name </span>= <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;H&amp;V Input</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
    GetWindow(</span><span style="color: #0000ff"&gt;typeof</span><span style="color: #000000"&gt;(MyWindow));
}

[MenuItem(MenuName </span>+ <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/Rigidbody Force</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)]
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; Code_RigidbodyForce()
{
    type </span>=<span style="color: #000000"&gt; Code_Type.rigidbody_force_code;
    Code_Name </span>= <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Rigidbody Force</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
    GetWindow(</span><span style="color: #0000ff"&gt;typeof</span><span style="color: #000000"&gt;(MyWindow));
}

[MenuItem(MenuName </span>+ <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;/Camera Follow</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;)]
</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; Code_CameraFollow()
{
    type </span>=<span style="color: #000000"&gt; Code_Type.camera_follow;
    Code_Name </span>= <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;Camera Follow</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
    GetWindow(</span><span style="color: #0000ff"&gt;typeof</span><span style="color: #000000"&gt;(MyWindow));
}

</span><span style="color: #0000ff"&gt;private</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; OnGUI()
{
    Debug.Log(type);
    </span><span style="color: #0000ff"&gt;string</span> content = <span style="color: #800000"&gt;""</span><span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;switch</span><span style="color: #000000"&gt;(type)
    {
        </span><span style="color: #0000ff"&gt;case</span><span style="color: #000000"&gt; Code_Type.input_code:
            content </span>= <span style="color: #800000"&gt;@"</span><span style="color: #800000"&gt;float moveHorizontal = Input.GetAxis(""Horizontal"");

float moveVertical = Input.GetAxis(""Vertical""); <span style="color: #800000">"<span style="color: #000000">;
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case<span style="color: #000000"> Code_Type.rigidbody_force_code:
content = <span style="color: #800000">@"<span style="color: #800000">Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
GetComponent().AddForce(movement speed Time.deltaTime); <span style="color: #800000">"<span style="color: #000000">;
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case<span style="color: #000000"> Code_Type.camera_follow:
content = <span style="color: #800000">@"<span style="color: #800000">public GameObject player;
private Vector3 offset;

// Use this for initialization
void Start () {
//摄像机与跟随物体的初始相对位置
offset = transform.position - player.transform.position;
}

// Update is called once per frame
void LateUpdate () {
//跟随物体的位置加上相对位置
transform.position = player.transform.position + offset;
}<span style="color: #800000">"<span style="color: #000000">;
<span style="color: #0000ff">break<span style="color: #000000">;
}
GUILayout.Label(Code_Name + <span style="color: #800000">"<span style="color: #800000"> Code<span style="color: #800000">"<span style="color: #000000">,EditorStyles.boldLabel);
GUILayout.TextArea(content,GUILayout.ExpandHeight(<span style="color: #0000ff">true<span style="color: #000000">));
}
}

效果:

相关文章

这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...
这篇文章主要介绍Unity中地面检测方案的示例分析,文中介绍的...
这篇文章主要介绍了Unity游戏开发中如何消除不想要的黄色警告...
这篇文章主要介绍了Unity中有多少种渲染队列,具有一定借鉴价...
这篇文章主要介绍Unity中如何实现Texture,文中介绍的非常详...
小编给大家分享一下Unity中DOTS要实现的特点有哪些,相信大部...