Unity中在Inspector窗口使用按钮调试方法

目录

一、写一个Button属性类

二、写一个Custom Editor的Editor工具类

三、写一个测试类

四、简单小结


在Unity中我们很多时候,希望能够在Editor模式下快速调用我们类中的方法,除了专门写一个Debug测试类去调用,也可以通过使用第三方插件的方式来调用,比如使用Odin Inspector插件中的Button特性,今天我将教你使用Unity原生代码写一个实现相同功能的特性

一、写一个Button属性类

using System;
using System.Collections;
using System.Collections.Generic;
using OpenCover.Framework.Model;
using UnityEngine;

public class ButtonAttribute : Attribute
{
    public string ButtonName { get; private set; }
    public int Width { get; private set; }
    public int Height { get; private set; }

    public ButtonAttribute()
    {

    }

    public ButtonAttribute(string buttonName, int width = 0, int height = 0)
    {
        ButtonName = buttonName;
        Width = width;
        Height = height;
    }
}

类中包括无参和有参两个构造方法,有参的参数中,buttonName可以指定Button在Inspector窗口显示的名字,width和heigth分别指定绘制的Button的尺寸

二、写一个Custom Editor的Editor工具类

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

[CustomEditor(typeof(MonoBehaviour), true)]
public class ButtonAttributeEditor : UnityEditor.Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        var methods = target.GetType().GetMethods();

        foreach (var method in methods)
        {
            var attributes = method.GetCustomAttributes(typeof(ButtonAttribute), false);
            if (attributes.Length > 0)
            {
                var buttonAttribute = (ButtonAttribute)attributes[0];
                string methodName = buttonAttribute.ButtonName;
                if (string.IsNullOrEmpty(buttonAttribute.ButtonName))
                {
                    methodName = method.Name;
                }

                GUILayout.BeginHorizontal();
                if (buttonAttribute.Width > 0)
                {
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button(methodName, GUILayout.Width(buttonAttribute.Width), GUILayout.Height(buttonAttribute.Height > 0 ? buttonAttribute.Height : 30)))
                    {
                        method.Invoke(target, null);
                    }
                    GUILayout.FlexibleSpace();
                }
                else
                {
                    if (GUILayout.Button(methodName, GUILayout.Height(buttonAttribute.Height > 0 ? buttonAttribute.Height : 25)))
                    {
                        method.Invoke(target, null);
                    }
                }
                GUILayout.EndHorizontal();
            }
        }
    }
}

通过继承OnInspectorGUI来在Inspector窗口进行绘制,通过反射获取MonoBehaviour及其所有子类的方法,这里需要注意一点,CustonEditor第二个参数一定要设置成true,这样才能对Mono的子类生效,里面是我简单写的一个样式,大家也可以自行对绘制的样式进行自定义

三、写一个测试类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonAttributeTest : MonoBehaviour
{
    [Button]
    public void TestMethod()
    {
        Debug.Log("默认按钮");
    }

    [Button("测试方法",100,50)]
    public void TestMethod2()
    {
        Debug.Log("自定义按钮");
    }
}

在要调用的方法上用Button特性修饰,将代码挂载在场景中,就可以在Inspector窗口看到对应的按钮了,如图所示

点击对应按钮,就可以得到如下结果

四、简单小结

在这里,我们教你使用Unity的特性Attribute和自定义Editor编辑器的方式实现了Inspector窗口快速调用方法的功能,Unity中的特性和自定义Editor编辑器的功能还可以实现很多其他强大的操作,以后我还会为大家介绍,大家也可以自己尝试一下~

猜你喜欢

转载自blog.csdn.net/he153422079/article/details/143200025