Unity Editor 编辑器扩展 十 Handle控件

目录

  • Handle控件
  • 按钮控制柄窗口

Handle控件

按钮,控制柄,窗口

效果见图:
Unity Editor 编辑器扩展 十 Handle控件_第1张图片

创建HandleTest.cs脚本,挂载到一个控物体上,然后在Editor文件夹下创建如下脚本:

using UnityEngine;
using UnityEditor;

[CustomEditor (typeof(HandleTest))]
public class HandleInspector : Editor
{

    int windowID = 1234;
    Rect windowRect;
    void OnSceneGUI ()
    {

//      1.物体控制柄
        var component = target as HandleTest;
        PositionHandle2 (component);
        PositionHandle (component.transform);

//      2.控制按钮
        HandleButton ();

//      3.控制窗口
        GUIWindow ();
    }

    void PositionHandle2 (HandleTest component)
    {
        var transform = component.transform;
        if (Tools.current == Tool.Move) {
            transform.rotation = Handles.RotationHandle (transform.rotation, transform.position);
        }
        else
            if (Tools.current == Tool.Rotate) {
                transform.position = Handles.PositionHandle (transform.position, transform.rotation);
            }
        Handles.color = Color.blue;
        Handles.DrawAAPolyLine (component.vertex);
        PositionHandle (component.transform);
    }

    static void HandleButton ()
    {
        Handles.BeginGUI ();
        GUILayout.Button ("Button", GUILayout.Width (50));
        Handles.EndGUI ();
        using (new HandleGUIScope ()) {
            GUILayout.Button ("Button", GUILayout.Width (50));
        }
    }

    //添加控制窗口
    void GUIWindow ()
    {
        windowRect = GUILayout.Window (windowID, windowRect, id =>  {
            GUI.contentColor = Color.green;
            EditorGUILayout.LabelField ("Label");
            EditorGUILayout.ColorField (Color.cyan);
            EditorGUILayout.ToggleLeft ("Toggle", false);
            GUILayout.Button ("Button");
            GUI.DragWindow ();
        }, "Window", GUILayout.Width (100));
    }

    //控制柄
    Vector3 PositionHandle (Transform transform)
    {
        var size = 1;
        var position = transform.position;
        Handles.color = Handles.xAxisColor;
        position = Handles.Slider (position, transform.right, size, Handles.CircleCap, 1); //X 軸
        Handles.color = Color.cyan;
        position = Handles.Slider (position, transform.up); //Y 軸
        Handles.color = Color.black;
        position = Handles.Slider (position, transform.forward); //Z 軸

        transform.position =
            Handles.FreeMoveHandle (
            transform.position,
            transform.rotation,
            size,
            Vector3.one, Handles.RectangleCap);

        transform.rotation =
            Handles.FreeRotateHandle (
            transform.rotation,
            transform.position,
            2);
        return position;
    }
}

public class HandleGUIScope : GUI.Scope
{
    public HandleGUIScope ()
    {
        Handles.BeginGUI ();
    }

    protected override void CloseScope ()
    {
        Handles.EndGUI ();
    }
}

选择这个控物体时,就会出现上图的效果。

本文链接:http://blog.csdn.net/WarrenMondeville/article/details/53802111

你可能感兴趣的:(unity,Editor)