当前位置:首页 » 《随便一记》 » 正文

那些年我踩的坑(1)——Unity编辑器SerializedProperty.enumValueIndex_RaymondCL

17 人参与  2021年12月15日 12:51  分类 : 《随便一记》  评论

点击全文阅读


这个是我自己的坑,自己挖的坑,然后自己跳了下去

首先上结论,以后别再犯同样的错误:
SerializedProperty.enumValueIndex得到的是当前枚举值在所有值中的排序索引,不是枚举值!!!是enumValueIndex,是Index!!!不是enumValue!

错误的写法:


public enum EventType
{
    None = 0,
    Test = 2,
    Attack = 1,
    Test3 = 3,
    Damage = 4,
}

[System.Serializable]
public class ActionEvent
{
    public EventType eventType;
    public float atkValue;
    public float damageValue;
}

public class Actor : MonoBehaviour
{
    public ActionEvent actionEvent;
}

[CustomPropertyDrawer(typeof(ActionEvent), true)]
public class ActionEventDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            SerializedProperty eventType = property.FindPropertyRelative("eventType");
            EditorGUI.PropertyField(position, eventType);
            position.y += EditorGUIUtility.singleLineHeight;
            //把enumValueIndex当成枚举值就是最大的错误
            switch (eventType.enumValueIndex)  
            {
                case (int)EventType.Attack:
                    EditorGUI.PropertyField(position, property.FindPropertyRelative("atkValue"));
                    break;
                case (int)EventType.Damage:
                    EditorGUI.PropertyField(position, property.FindPropertyRelative("damageValue"));
                    break;
            }
        }
    }
}

由于我的枚举被我从中间,删除了几个,导致枚举值不连续,因此最开始我使用Popup绘制时,一直以为是使用SerializedProperty.enumNames导致的问题,因为这个names是连续的数组,是对不上枚举值的,所以一直在这里改,一直没有注意到enumValueIndex的问题。

正确写法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
[CustomPropertyDrawer(typeof(ActionEvent), true)]
public class ActionEventDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            SerializedProperty eventType = property.FindPropertyRelative("eventType");
            EditorGUI.PropertyField(position, eventType);
            position.y += EditorGUIUtility.singleLineHeight;
            var eventTypeValue = Enum.GetValues(typeof(EventType)).GetValue(eventType.enumValueIndex);
            switch (eventTypeValue)  
            {
                case EventType.Attack:
                    EditorGUI.PropertyField(position, property.FindPropertyRelative("atkValue"));
                    break;
                case EventType.Damage:
                    EditorGUI.PropertyField(position, property.FindPropertyRelative("damageValue"));
                    break;
            }
        }
    }
}

我们在调试的窗口中可以看到枚举的值数组列:

var values = Enum.GetValues(typeof(FPSPlayerActionEventType));

在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/31606.html

枚举  错误  数组  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 山海不相逢内容精选(温逸尘沈衿)_山海不相逢内容精选(温逸尘沈衿)
  • (番外)+(全书)霍沉洲沈青禾此去经年人未还(霍沉洲沈青禾)_(霍沉洲沈青禾此去经年人未还)列表_笔趣阁(霍沉洲沈青禾)
  • (番外)+(全书)霍沉洲沈青禾(此去经年人未还霍沉洲沈青禾)完结_(霍沉洲沈青禾)列表_笔趣阁(此去经年人未还霍沉洲沈青禾)
  • 「重回八零,拒绝替嫁冲喜」章节彩蛋限时释出‌_卫东玉兰苏夏人气小说未删减节选
  • 重生七零祁同伟不再是农民儿子结局+番外纯净版全书免费重生七零祁同伟不再是农民儿子结局+番外纯净版全书免费
  • 傅雅宁的神女老婆,却在背地承欢作乐顾尘傅雅宁全书在线
  • 全文神女老婆,却在背地承欢作乐全局(顾尘傅雅宁)列表_全文神女老婆,却在背地承欢作乐全局
  • (番外)+(全书)此去经年人未还全书+番外+后续免费下载_(沈青禾霍沉洲)此去经年人未还全书+番外+后续列表_笔趣阁(沈青禾霍沉洲)
  • 完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续列表_完结文毁容的姐姐和瞎眼的我离开后,姜家两兄弟悔哭了+后续(林梦婉)
  • 妻子辱我爸受贿自杀,我掏出一等军功章节选推荐_[陈素云辰朋友]小说精彩章节分享
  • 全书浏览苔藓爬满旧日诺言新上(顾砚廷慕晚夏)_苔藓爬满旧日诺言新上(顾砚廷慕晚夏)全书结局
  • 顾尘傅雅宁(神女老婆,却在背地承欢作乐+后续+结局)结局_(顾尘傅雅宁神女老婆,却在背地承欢作乐+后续+结局全书结局)结局列表_笔趣阁(顾尘傅雅宁)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1