Handles
Scene视图中的自定义 3D GUI 控件和绘制操作。
UnityEditor.Handles - Unity 脚本 APIhttps://docs.unity.cn/cn/current/ScriptReference/Handles.html
1.添加一个脚本到场景中的物体上(HandleExample.cs)
using UnityEngine;
[ExecuteInEditMode]
public class HandleExample : MonoBehaviour
{
public float shieldArea = 5.0f;
}
2.添加个脚本继承Editor,在OnSceneGUI绘制(HandleExampleEditor.cs)
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(HandleExample))]
class HandleExampleEditor : Editor
{
protected virtual void OnSceneGUI()
{
HandleExample handleExample = (HandleExample)target;
if (handleExample == null)
{
return;
}
Handles.color = Color.yellow;
GUIStyle style = new GUIStyle();
style.normal.textColor = Color.green;
Vector3 position = handleExample.transform.position + Vector3.up * 2f;
string posString = position.ToString();
//handle文本绘制
Handles.Label(position,
posString + "\nShieldArea: " +
handleExample.shieldArea.ToString(),
style
);
//可以使用IMGUI
Handles.BeginGUI();
if (GUILayout.Button("Reset Area", GUILayout.Width(100)))
{
handleExample.shieldArea = 5;
}
Handles.EndGUI();
//绘制弧线
Handles.DrawWireArc(
handleExample.transform.position,
handleExample.transform.up,
-handleExample.transform.right,
180,
handleExample.shieldArea);
//绘制改变shieldArea的锥形点
handleExample.shieldArea =
Handles.ScaleValueHandle(handleExample.shieldArea,
handleExample.transform.position + handleExample.transform.forward * handleExample.shieldArea,
handleExample.transform.rotation,
1, Handles.ConeHandleCap, 1);
}
}
EditorTool
使用此类可实现自定义编辑器工具。
EditorTools.EditorTool - Unity 脚本 APIhttps://docs.unity.cn/cn/current/ScriptReference/EditorTools.EditorTool.html 1.添加[EditorTool("Name")]特性
2.继承EditorTool
3.在OnToolGUI使用Hanles的API绘制
using System;
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;
[EditorTool("Move Right")]
class PlatformTool : EditorTool
{
public override void OnToolGUI(EditorWindow window)
{
window.ShowNotification(new GUIContent("Move Right"));
EditorGUI.BeginChangeCheck();
Vector3 position = Tools.handlePosition;
using (new Handles.DrawingScope(Color.green))
{
position = Handles.Slider(position, Vector3.right);
}
//如果在调用 EditorGUI.BeginChangeCheck 后 GUI 状态发生更改,则返回 true,否则返回 false。
if (EditorGUI.EndChangeCheck())
{
Vector3 delta = position - Tools.handlePosition;
Undo.RecordObjects(Selection.transforms, "Move Right");
foreach (var transform in Selection.transforms)
transform.position += delta;
}
}
}
编辑器免费课程:
独立游戏《Unity打造关卡编辑器》Unity 独立游戏 关卡编辑器https://bycwedu.vipwan.cn/promotion_channels/72762192