Unity编辑器拓展(Handles/EditorTool)

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 独立游戏 关卡编辑器icon-default.png?t=LA92https://bycwedu.vipwan.cn/promotion_channels/72762192

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity编辑器拓展Editor Extension)可以通过自定义的脚本来扩展Unity编辑器的功能和界面,以满足特定项目的需求。通过编辑器拓展,开发者可以创建自定义的编辑器窗口、工具栏按钮、菜单项、检视面板等,来增强Unity编辑器的功能和流程。 要创建一个Unity编辑器拓展,你可以使用C#编写一个继承自Editor类的脚本。这个脚本可以通过Unity的Inspector面板来设置相关的属性和行为。以下是一个简单的示例: ```csharp using UnityEngine; using UnityEditor; public class MyEditorExtension : EditorWindow { [MenuItem("Custom Tools/My Editor Window")] public static void OpenWindow() { // 创建并打开一个自定义的编辑器窗口 MyEditorExtension window = (MyEditorExtension)EditorWindow.GetWindow(typeof(MyEditorExtension)); window.Show(); } private void OnGUI() { // 在编辑器窗口中绘制UI元素 GUILayout.Label("Hello, I am a custom editor window!"); if (GUILayout.Button("Click Me")) { Debug.Log("Button clicked!"); } } } ``` 上述代码创建了一个自定义的编辑器窗口,并在窗口中绘制了一个标签和一个按钮。通过在Unity编辑器中点击"Custom Tools"菜单下的"My Editor Window",可以打开这个自定义的编辑器窗口。 除了编辑器窗口,你还可以通过继承Editor类来创建自定义的检视面板、菜单项等。Unity官方文档中有更详细的教程和示例,可以帮助你更深入地了解和使用Unity编辑器拓展
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寒大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值