textarea value不显示_使用Odin在Inspector面板显示枚举中文名

1454b7e37b269591d2773bb40d363161.png

2020.8.5 更新

Odin最新版本已经支持本篇文章实现的功能,Odin牛逼!!!

3a75a9989e0693027ed3876a255e243e.png

### 前言

Odin这个神器插件想必现在已经家喻户晓了,如果还不知道的建议重。。。重新去百度一下Odin插件。

简而言之使用Odin之后,能应付90%的编辑器拓展工作,并节省至少70%的体力,摸鱼力UPUP。

但是封装这么到位的插件也是有短肋的,他对于用户来说就是一个黑盒,想要拓展功能往往比较麻烦,有些极端情况甚至需要修改源代码(有UE4内味了,味大了),不过总体而言还是利大于弊的,大家酌情选择使用。

那么这篇文章就是带领大家解决一个比较常见的痛点,Odin无法在Inspector面板显示出枚举类型的中文名,比如

fafa7c9719b0f72c03333d8e358dd2d3.png

他就只能这样

c470ca7b8c17e008592b8e72d5a7cf7a.png

虽然说那个下拉窗口里是中文,方便我们选择,但是在Inspector面板显示英文的话仍然不够直观,所以我们尽可能去实现这一个功能,来进一步提高生产力

**别来跟我提Unity原生的枚举中文显示解决方案嗷,太丑了点**

### 正文

首先要去扒拉Odin的源码,我这边已经扒拉好了

这就是绘制枚举类型的核心函数,第一个Label代表我们的字段名,第二个Label就是我们具体枚举的显示文字了

    /// <summary>Draws an enum selector field using the enum selector.</summary>
    public static T DrawEnumField(
      GUIContent label,
      GUIContent contentLabel,
      T value,
      GUIStyle style = null)
    {
      int controlId;
      Rect valueRect;
      SirenixEditorGUI.GetFeatureRichControlRect(label, out controlId, out bool _, out valueRect);
      Action<EnumSelector<T>> bindSelector;
      Func<IEnumerable<T>> resultGetter;
      if (OdinSelector<T>.DrawSelectorButton<EnumSelector<T>>(valueRect, contentLabel, style ?? EditorStyles.popup, controlId, out bindSelector, out resultGetter))
      {
        EnumSelector<T> enumSelector = new EnumSelector<T>();
        if (!EditorGUI.showMixedValue)
          enumSelector.SetSelection(value);
        OdinEditorWindow odinEditorWindow = enumSelector.ShowInPopup(new Vector2(valueRect.xMin, valueRect.yMax));
        if (EnumSelector<T>.isFlagEnum)
          odinEditorWindow.OnClose += new Action(enumSelector.SelectionTree.Selection.ConfirmSelection);
        bindSelector(enumSelector);
      }
      if (resultGetter != null)
        value = resultGetter().FirstOrDefault<T>();
      return value;
    }

目标函数找到了,剩下的就很简单了,Odin提供给我们自定义绘制字段值的功能,然后我们再只需要用一下反射即可

还是上面那个例子

    [CustomValueDrawer("TestDrawEnumByChinese")]
    [LabelText("测试中文枚举")]
    public enum TestEnum
    {
        [LabelText("测试1")]
        Test1,

        [LabelText("测试2")]
        Test2
    }
        private static TestEnum TestDrawEnumByChinese(TestEnum value, GUIContent label)
        {
            Type targetEnumType = typeof (TestEnum);
            LabelTextAttribute targetEnumLabelTextAttribute = targetEnumType.GetAttribute<LabelTextAttribute>();

            FieldInfo[] fieldInfos = targetEnumType.GetFields();

            string[] targetItemNames = Enum.GetNames(targetEnumType);

            string finalShowName = "";
            for (int i = 0; i < targetItemNames.Length; i++)
            {
                if (targetItemNames[i] == value.ToString())
                {
                    finalShowName = value.ToString();
                    break;
                }
            }

            for (int i = 0; i < fieldInfos.Length; i++)
            {
                if (fieldInfos[i].Name == finalShowName)
                {
                    finalShowName = fieldInfos[i].GetAttribute<LabelTextAttribute>().Text;
                    break;
                }
            }

            return EnumSelector<TestEnum>.DrawEnumField(new GUIContent(targetEnumLabelTextAttribute.Text), new GUIContent(finalShowName), value);
        }

然后我们随便找个Mono脚本声明一个TestEnum字段即可

然后就可以在Inspector面板看到结果啦

917dac2db46aff392890ce3979c59ce4.png

### 优化及注意点

当然了,由于我们用到了反射,并且Odin会频繁调用这个自定义的绘制函数,所以我们肯定还是要做一下优化的,主要有以下几点

  • - 单独做一个静态类用来定义项目中所有需要中文显示枚举名的自定义绘制函数(都差不太多),方便管理
  • - 适配标注System.Flag属性的枚举
  • - Cache住反射信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值