Unity编辑器拓展(Inspector)

ContextMenu/ContextMenuItem

using UnityEngine;
using UnityEditor;
public class ContextTest : MonoBehaviour
{
    //在该附加脚本的 Inspector 中,当用户选择该上下文菜单时, 将执行此函数。
    [ContextMenu("Do Something")]
    void DoSomething()
    {
        Debug.Log("Perform operation");
    }

    //使用该属性可将上下文菜单添加到调用命名方法的字段。
    [ContextMenuItem("Reset", "ResetBiography")]
    public string playerBiography = "abcd";

    void ResetBiography()
    {
        playerBiography = "";
    }

#if UNITY_EDITOR
    //附加到Camera的菜单
    [MenuItem("CONTEXT/Camera/Log")]
    static void Log(MenuCommand command)
    {
        Debug.Log(command.context);
    }


#endif
}

Editor自定义Inspector面板

1.MyPlayer .cs

using UnityEngine;
using System.Collections;


public class MyPlayer : MonoBehaviour
{
    public int armor = 75;
    public int damage = 25;
    public GameObject gun;

    
}

 2.MyPlayerEditor

using UnityEditor;
using UnityEngine;
using System.Collections;

//编辑器可序列化
[CustomEditor(typeof(MyPlayer))]
[CanEditMultipleObjects]
public class MyPlayerEditor : Editor
{
    SerializedProperty damageProp;
    SerializedProperty armorProp;
    SerializedProperty gunProp;

    void OnEnable()
    {
        //赋值
        damageProp = serializedObject.FindProperty("damage");
        armorProp = serializedObject.FindProperty("armor");
        gunProp = serializedObject.FindProperty("gun");
    }

    public override void OnInspectorGUI()
    {
        // 更新值
        serializedObject.Update();

        EditorGUILayout.IntSlider(damageProp, 0, 100, new GUIContent("Damage"));

        //当同时编辑多个时,只有值一样才展示ProgressBar
        if (!damageProp.hasMultipleDifferentValues)
            ProgressBar(damageProp.intValue / 100.0f, "Damage");

        EditorGUILayout.IntSlider(armorProp, 0, 100, new GUIContent("Armor"));

        if (!armorProp.hasMultipleDifferentValues)
            ProgressBar(armorProp.intValue / 100.0f, "Armor");

        EditorGUILayout.PropertyField(gunProp, new GUIContent("Gun Object"));

        // 保存值
        serializedObject.ApplyModifiedProperties();
    }

    // 自定义ProgressBar
    void ProgressBar(float value, string label)
    {
        Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
        EditorGUI.ProgressBar(rect, value, label);
        EditorGUILayout.Space();
    }
}

Unity常用内置特性

 自定义特性

1.Attribute 继承 PropertyAttribute

2.渲染器继承 PropertyDrawer

3.使用 CustomPropertyDrawerAttribute 标记

4.在 OnGUI 渲染

5.使用 GetPropertyHeight 重新计算高度

MyDrawer.cs 

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

[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyDrawer : PropertyDrawer
{
    //绘制
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MyAttribute range = attribute as MyAttribute;

        EditorGUI.LabelField(new Rect(position.x, position.y, position.width, 20), "自定义特性");

        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(new Rect(position.x, position.y + 20, position.width, 20), property, range.min, range.max,"");
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(new Rect(position.x, position.y + 20, position.width, 20), property, Convert.ToInt32(range.min), Convert.ToInt32(range.max), "");
        else
            EditorGUI.LabelField(new Rect(position.x, position.y + 20, position.width, 20),"","Use Range with float or int.");
    }

    //重写属性高度
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label) + 25;
    }
}

 MyAttr.cs

using UnityEngine;

public class MyAttribute : PropertyAttribute
{
    public float min;
    public float max;

    public MyAttribute(float min, float max)
    {
        this.min = min;
        this.max = max;
    }
}

ObjectPreview 

 Object 的预览编辑器

1.继承 ObjectPreview

2.HasPreviewGUI 返回  true

3.OnPreviewGUI 渲染

4.CustomPreviewAttribute 标记

using UnityEngine;
using UnityEditor;


//所有的GameObject预览
[CustomPreview(typeof(GameObject))]
public class MyPreview : ObjectPreview
{
    public override bool HasPreviewGUI()
    {
        return true;
    }

    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        GUI.Label(r, target.name + " is being previewed");
    }
}

免费课程链接:

独立游戏《Unity打造关卡编辑器》Unity 独立游戏 关卡编辑器https://bycwedu.vipwan.cn/promotion_channels/72762192

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

萧寒大大

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

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

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

打赏作者

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

抵扣说明:

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

余额充值