Search Unity

Bug fix for my custom LabelAttribute?

Discussion in 'Editor & General Support' started by TheRebirth2393390183, Sep 13, 2017.

  1. TheRebirth2393390183

    TheRebirth2393390183

    Joined:
    Jun 5, 2017
    Posts:
    8
    I'm currently building custom editor for my fellow. One of the fundamental tasks I need to get it done is... you can call it "Editor Localization" - Custom editors should display comprehensible translated texts, and I want to write as few CustomEditor classes as possible, so I decided that a LabelAttribute should be an ideal replacement for CustomEditor.
    The first version of LabelAttribute was finished at a pretty early stage. I've been using this script since several months ago and it works good. It works with Integers, Booleans, Vectors... However, when I tried to label a class or a list, something goes wrong.

    Here's the source code for my LabelAttribute:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class LabelAttribute : PropertyAttribute
    5. {
    6.     public string label;
    7.  
    8.     public LabelAttribute(string Label)
    9.     {
    10.         label = Label;
    11.     }
    12. }
    13.  
    14. [CustomPropertyDrawer(typeof(LabelAttribute))]
    15. public class LabelDrawer : PropertyDrawer
    16. {
    17.     LabelAttribute labelAttribute
    18.     {
    19.         get
    20.         {
    21.             return (LabelAttribute)attribute;
    22.         }
    23.     }
    24.  
    25.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    26.     {
    27.         label.text = labelAttribute.label;
    28.  
    29.         EditorGUI.PropertyField(position, property, label, true);
    30.     }
    31. }
    And some code for a ScriptableObject, as a text case for LabelAttribute:

    Code (CSharp):
    1. [Serializable]
    2.     public class ItemActiveEffect
    3.     {
    4.         public ItemEffectParameter SuccessRate;
    5.         public ItemEffectParameter LifeGain;
    6.         public ItemEffectParameter BombGain;
    7.         public ItemEffectParameter ConstitutionExpGain;
    8.         public ItemEffectParameter AgilityExpGain;
    9.         public ItemEffectParameter KnowledgeExpGain;
    10.         public ItemEffectParameter LuckExpGain;
    11.         public ItemEffectParameter PhysicalStrengthExpGain;
    12.         public ItemEffectParameter PhysicalDefenseExpGain;
    13.         public ItemEffectParameter MysticStrengthExpGain;
    14.         public ItemEffectParameter MysticDefenseExpGain;
    15.     }
    16.  
    17.     [Serializable]
    18.     public class ItemPassiveEffect
    19.     {
    20.         public ItemEffectParameter ConstitutionExpGain;
    21.         public ItemEffectParameter AgilityExpGain;
    22.         public ItemEffectParameter KnowledgeExpGain;
    23.         public ItemEffectParameter LuckExpGain;
    24.         public ItemEffectParameter PhysicalStrengthExpGain;
    25.         public ItemEffectParameter PhysicalDefenseExpGain;
    26.         public ItemEffectParameter MysticStrengthExpGain;
    27.         public ItemEffectParameter MysticDefenseExpGain;
    28.     }
    29.  
    30.     [CreateAssetMenu(fileName = "New Item.asset", menuName ="创建RPG数据/创建物品", order = 7), Serializable]
    31.     public class RPGItem : ScriptableObject
    32.     {
    33.  
    34.         [Label("物品名称")]
    35.         public string               Name = "物品";
    36.  
    37.         [Label("素材名称")]
    38.         public string               FileName = "New Item";          // 与 物品名称 的不同之处是,素材名称 指定的是该物品的数据对应的素材在该工程目录中的素材文件的名称。
    39.  
    40.         [Label("图标")]
    41.         public Texture2D            Icon;                           // 决定该物品在背包界面和编辑器中的显示方式。
    42.  
    43.         [Label("物品描述"), TextArea(minLines:2, maxLines:4)]
    44.         public string               Description;                    // 物品在游戏的背包界面中将会显示的描述。
    45.  
    46.         [Label("占用空间")]
    47.         public Vector2              SpaceUsage = new Vector2 (1, 1);
    48.  
    49.         [Label("物品类型")]
    50.         public ItemCategory         Category = ItemCategory.Fixed;  // 物品类型将决定它是否会显示在装备界面的列表中。
    51.  
    52.         [Label("可消耗")]
    53.         public bool                 Consumable = true;              // 可消耗的物品在被使用过后就会消失。
    54.  
    55.         [Label("耐久度")]
    56.         public float                Durability = 0;                 // 用 0 表示无限。对于可使用但不可消耗的物品(如药剂),耐久度可能会在使用后下降。
    57.  
    58.         [Label("腐败速度")]
    59.         public float                DecaySpeed = 0;                 // 当腐败速度大于 0 时,物品的耐久度会不断下降,小于 0 时则会修复。
    60.  
    61.         [Label("主动效果")]
    62.         public ItemActiveEffect     ActiveEffect;                   // 当物品被使用时的效果。
    63.  
    64.         [Label("被动效果")]
    65.         public ItemPassiveEffect    PassiveEffect;                  // 当物品被携带或装备时的效果。
    66.  
    67.         [Label("排除角色")]
    68.         public List<RPGCharacter>   ExcludedCharacters;             // 排除角色列表。列表中的角色不能住装备该物品、使用物品或成为该物品的作用对象。
    69.  
    70.     }
    A class containing multiple members(ActiveEffect and PassiveEffect in this example) can be labeled, as well as float and bool. But it cannot successfully label a list, despite the fact that there's a label for my list, it displays its original variable name instead.
    Also when I expanded ItemActiveEffect and ItemPassiveEffect, everything's overlapping together and it looks nasty.



    I wonder if there's a way to fix this without having to explicitly write a CustomEditor for this class (this would become a huge bunch of repetitive code I have to write if I had to choose this method.)?
    Thanks for your patience!