Unity小技巧——自定义面板—Enum的变换变量

预览图:
请添加图片描述

写一个 Enum 普通脚本 不要放到Eidtor文件夹内 否则找不到此脚本

using UnityEngine;

public enum format
{
	数字,
	字符串,
	图片
}
public class Drop2 : MonoBehaviour
{
	public format Enum_type;
	public int number;
	public string str;
	public Sprite sprite;

}


重点
写 UnityEditor 脚本 这个需要放到Eidtor文件夹内

这个地方一定记住要关联自己的脚本 否则寻找不到变量
请添加图片描述


 using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Drop2))]//关联之前的脚本
public class EnumVar : Editor
{
    private SerializedObject test;//序列化
    private SerializedProperty Enum_type, number, str, sprite;//定义类型
    void OnEnable()
    {
        test = new SerializedObject(target);
        //找到指定的变量 以及 Enum
        Enum_type = test.FindProperty("Enum_type");
        number = test.FindProperty("number");
        str = test.FindProperty("str");
        sprite = test.FindProperty("sprite");
    }
    public override void OnInspectorGUI()
    {
        test.Update();//更新test
        EditorGUILayout.PropertyField(Enum_type);

        if (Enum_type.enumValueIndex == 0)
        {//当选择第一个枚举类型
            EditorGUILayout.PropertyField(number);
        }
        else if (Enum_type.enumValueIndex == 1)
        {
            EditorGUILayout.PropertyField(str);
        }
        else if (Enum_type.enumValueIndex == 2)
        {
            EditorGUILayout.PropertyField(sprite);
        }
        //serializedObject.ApplyModifiedProperties();
        test.ApplyModifiedProperties();//应用
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

臭臭~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值