您的位置:首页 > 移动开发 > Unity3D

Unity Editor 编辑器扩展 十 Handle控件

2016-12-22 01:51 1261 查看
目录

Handle控件

按钮控制柄窗口

Handle控件

按钮,控制柄,窗口

效果见图:



创建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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息