Unity 编辑器扩展常用功能

一些编辑器扩展功能收集:
在这里插入图片描述

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class TestEditor : EditorWindow
{
    private static TestEditor _window;

    private Font font;
    private Object obj;
    private float f;
    private int i;
    private bool isok = true;
    private string str;

    public Vector2 mPos2;

    public Vector3 mPos3;

    public Vector4 mPos4;

    public Rect mRect;

    public Bounds mBounds;

    public int index;
    public EnumTest mEnum;

    public AnimationCurve animationCurve = new AnimationCurve ();

    [ColorUsage(true,true)]
    Color color;

    Transform[] childs = null;
    

    [MenuItem ("Tools/菜单名")]
    public static void GUIDRefReplaceWin()
    {
        Rect wr = new Rect (0, 0, 300, 1000);//窗口大小
        _window = (TestEditor)GetWindowWithRect (typeof (TestEditor), wr, false, "窗口名");// false 表示不能停靠的
        _window.Show ();

    }
    void OnGUI()
    {
        GUILayout.Space (20);//间隔
        GUILayout.Label ("\t╔═╗╚║╝显示文字(  ͡◉ ͜ʖ ͡◉)");

        childs = Selection.transforms;//当前选中的物体

        font = (Font)EditorGUILayout.ObjectField ("字体", font, typeof (Font), true);
        obj = EditorGUILayout.ObjectField ("物体", obj, typeof (Object), true);
        i=EditorGUILayout.IntField ("int",i);
        f = EditorGUILayout.FloatField ("float", f);
        GUILayout.Label ("普通输入框");
        str = EditorGUILayout.TextField(str);
        GUILayout.Label ("可换行输入框");
        str = EditorGUILayout.TextArea (str);
        GUILayout.Label ("密码输入框");
        str = EditorGUILayout.PasswordField (str);

        f = EditorGUILayout.Slider ("Slider", f, 0, 1);
        EditorGUILayout.BeginHorizontal ();
        isok = GUILayout.Toggle (isok, "Toggle");
        isok = GUILayout.Toggle (isok, "Toggle");
        EditorGUILayout.EndHorizontal ();

        color = EditorGUILayout.ColorField (color); //颜色输入框

        if (GUILayout.Button ("按钮"))
        {
            GameObject go = new GameObject ();
            EditorUtility.SetDirty (go);
            DestroyImmediate (go);//让场景变为未保存状态
        }

        this.mPos2 = EditorGUILayout.Vector2Field ("二维坐标", this.mPos2);

        this.mPos3 = EditorGUILayout.Vector3Field ("三维坐标", this.mPos3);

        this.mPos4 = EditorGUILayout.Vector4Field ("四维坐标", this.mPos4);

        EditorGUILayout.Space ();

        EditorGUILayout.LabelField ("矩阵");
        this.mRect = EditorGUILayout.RectField (this.mRect);

        EditorGUILayout.Space ();

        EditorGUILayout.LabelField ("距离");
        this.mBounds = EditorGUILayout.BoundsField (this.mBounds);

        string[] strs = new[]
        {
            "数组下标0",
            "数组下标1",
            "数组下标2",
        };

        int[] intArr = new[]
        {
            1,
            2,
            3,
        };
        //字符选择,返回选择的字符数组下标
        this.index = EditorGUILayout.Popup (this.index, strs);
        //字符选择,返回对应的整数数组的整数值
        this.index = EditorGUILayout.IntPopup (this.index, strs, intArr);
        //枚举选择
        this.mEnum = (EnumTest)EditorGUILayout.EnumPopup (this.mEnum);

        animationCurve=EditorGUILayout.CurveField ("曲线", animationCurve);
    }
    public enum EnumTest
    {
        Int1,
        Str2,
        Float3,
        Color4
    }
    /// <summary>
    /// 获取场景中所有物体
    /// </summary>
    /// <returns></returns>
    private static List<GameObject> GetAllSceneObjectsWithInactive()
    {
        var allTransforms = Resources.FindObjectsOfTypeAll (typeof (Transform));
        var previousSelection = Selection.objects;
        Selection.objects = allTransforms.Cast<Transform> ()
            .Where (x => x != null)
            .Select (x => x.gameObject)
            //如果你只想获取所有在Hierarchy中被禁用的物体,反注释下面代码
            //.Where(x => x != null && !x.activeInHierarchy)
            .Cast<UnityEngine.Object> ().ToArray ();

        var selectedTransforms = Selection.GetTransforms (SelectionMode.Editable | SelectionMode.ExcludePrefab);
        Selection.objects = previousSelection;

        return selectedTransforms.Select (tr => tr.gameObject).ToList ();
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity编辑器扩展是指通过自定义脚本来扩展Unity编辑器功能和界面。在Unity中,可以使用编辑器扩展来创建自定义的面板、工具栏、菜单项等,以便更好地支持开发者的工作流程和需求。 在编辑器扩展中,有几个重要的类需要了解。首先是Editor类,它是所有自定义编辑器的基类,通过继承Editor类可以创建自定义的Inspector面板,用于编辑特定组件的属性。其次是EditorWindow类,它是创建自定义窗口的基类,可以用于创建独立的编辑器工具窗口。然后是EditorGUI和EditorGUILayout类,它们用于在自定义面板中创建UI元素,如按钮、文本框、滑动条等。这两个类的区别在于EditorGUI需要手动计算UI元素的位置和大小,而EditorGUILayout会自动进行排版和计算坐标。此外,还有一些其他常用的类,如GUI、GUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等,它们提供了各种用于编辑器扩展功能和工具。 使用编辑器扩展可以提高开发效率,使开发者能够更方便地进行编辑器自定义和工作流程优化。通过自定义面板和工具,可以根据项目需求添加额外的功能和工具,提供更好的用户体验和开发环境。同时,编辑器扩展也为开发者提供了更多的自由度和创造力,可以根据自己的需求进行定制和扩展。 总结起来,Unity编辑器扩展是通过自定义脚本来扩展Unity编辑器功能和界面,可以创建自定义的面板、工具栏、菜单项等,提供更好的开发环境和用户体验。常用的类包括Editor、EditorWindow、EditorGUI、EditorGUILayout、GUI、GUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐沢

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

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

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

打赏作者

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

抵扣说明:

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

余额充值