Search Unity

  1. Curious about what's going to be in 2020.1? Have a look at the 2020.1 beta blog post.
    Dismiss Notice
  2. Want to see 2020.1b in action? Sign up for our Beta 2020.1 Overview Webinar on April 20th for a live presentation from our evangelists and a Q&A session with guests from R&D.
    Dismiss Notice
  3. Interested in giving us feedback? Join our online research interviews on a broad range of topics and share your insights with us.
    Dismiss Notice
  4. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Serialize Reference isn't working with Custom Attributes (for example - Range)

Discussion in '2019.3 Beta' started by crocvr, Nov 25, 2019.

  1. crocvr

    crocvr

    Joined:
    Oct 7, 2015
    Posts:
    44
    I am trying to combine serialize reference with my custom attribute but it ignores and use default drawer.
    I tried to setup with Int property and Range attributes to see if the problem on my custom attribute implementation but it is not.

    Sample script:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7.  
    8. #endif
    9.  
    10.  
    11. public interface IFruit
    12. {
    13. }
    14.  
    15. public abstract class Fruit : IFruit
    16. {
    17.     public string fruitName;
    18. }
    19.  
    20. [Serializable]
    21. public class Orange : Fruit
    22. {
    23.     public enum OrangeType
    24.     {
    25.         Valencia,
    26.         Blood,
    27.         Jaffa
    28.     }
    29.  
    30.     [SerializeField] private OrangeType orangeType = default;
    31. }
    32.  
    33. [Serializable]
    34. public class Apple : Fruit
    35. {
    36.     public enum AppleType
    37.     {
    38.         Gala,
    39.         GrannySmith,
    40.         RedDelicious
    41.     }
    42.  
    43.     [SerializeField] private AppleType appleType = default;
    44.  
    45.     [SerializeField] [Range(0, 9)] private int count;
    46. }
    47.  
    48. public class InspectorTesting : MonoBehaviour
    49. {
    50.     [SerializeReference] public List<IFruit> fruits = new List<IFruit>();
    51. }
    52.  
    53. #if UNITY_EDITOR
    54.  
    55. [CustomEditor(typeof(InspectorTesting))]
    56. public class InspectorTestingEditor : Editor
    57. {
    58.     private InspectorTesting source;
    59.     private SerializedObject sourceRef;
    60.     private SerializedProperty fruits;
    61.  
    62.     private int dropDownSelection;
    63.     private Type[] types;
    64.     private string[] names;
    65.  
    66.     private void OnEnable()
    67.     {
    68.         source = (InspectorTesting) target;
    69.         sourceRef = serializedObject;
    70.  
    71.         GetProperties();
    72.     }
    73.  
    74.     void GetProperties()
    75.     {
    76.         types = GetAllOfInterface(typeof(Fruit));
    77.         names = new string[1];
    78.         names[0] = "Add A Fruit!";
    79.         names = names.Concat(GetAllOfInterfaceNames(typeof(Fruit))).ToArray();
    80.         fruits = sourceRef.FindProperty("fruits");
    81.     }
    82.  
    83.     public override void OnInspectorGUI()
    84.     {
    85.         DisplayProperties();
    86.         sourceRef.ApplyModifiedProperties();
    87.     }
    88.  
    89.     void DisplayProperties()
    90.     {
    91.         dropDownSelection = EditorGUILayout.Popup(dropDownSelection, names);
    92.         if (dropDownSelection != 0)
    93.         {
    94.             var fruit = Activator.CreateInstance(types[dropDownSelection - 1]) as Fruit;
    95.             fruit.fruitName = fruit.GetType().Name;
    96.             source.fruits.Add(fruit);
    97.             dropDownSelection = 0;
    98.         }
    99.  
    100.         EditorGUILayout.PropertyField(fruits, true);
    101.     }
    102.  
    103.     string[] GetAllOfInterfaceNames(Type _interfaceType)
    104.     {
    105.         return GetAllOfInterface(_interfaceType)
    106.             .Select(x => x.Name)
    107.             .ToArray();
    108.     }
    109.  
    110.     Type[] GetAllOfInterface(Type _interfaceType)
    111.     {
    112.         return AppDomain.CurrentDomain.GetAssemblies()
    113.             .SelectMany(s => s.GetTypes())
    114.             .Where(x => _interfaceType.IsAssignableFrom(x) && x != _interfaceType && !x.IsAbstract)
    115.             .OrderBy(x => x.Name)
    116.             .ToArray();
    117.     }
    118. }
    119.  
    120. #endif
     
    phobos2077 and kyuskoj like this.
  2. kyuskoj

    kyuskoj

    Joined:
    Aug 28, 2013
    Posts:
    31
    Same issue here. I think SerializeReference doesn't work with polymorhisim on the inspector.
    Unity 2019.3.0b11.
     
unityunity