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:
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public interface IFruit
{
}
public abstract class Fruit : IFruit
{
public string fruitName;
}
[Serializable]
public class Orange : Fruit
{
public enum OrangeType
{
Valencia,
Blood,
Jaffa
}
[SerializeField] private OrangeType orangeType = default;
}
[Serializable]
public class Apple : Fruit
{
public enum AppleType
{
Gala,
GrannySmith,
RedDelicious
}
[SerializeField] private AppleType appleType = default;
[SerializeField] [Range(0, 9)] private int count;
}
public class InspectorTesting : MonoBehaviour
{
[SerializeReference] public List<IFruit> fruits = new List<IFruit>();
}
#if UNITY_EDITOR
[CustomEditor(typeof(InspectorTesting))]
public class InspectorTestingEditor : Editor
{
private InspectorTesting source;
private SerializedObject sourceRef;
private SerializedProperty fruits;
private int dropDownSelection;
private Type[] types;
private string[] names;
private void OnEnable()
{
source = (InspectorTesting) target;
sourceRef = serializedObject;
GetProperties();
}
void GetProperties()
{
types = GetAllOfInterface(typeof(Fruit));
names = new string[1];
names[0] = "Add A Fruit!";
names = names.Concat(GetAllOfInterfaceNames(typeof(Fruit))).ToArray();
fruits = sourceRef.FindProperty("fruits");
}
public override void OnInspectorGUI()
{
DisplayProperties();
sourceRef.ApplyModifiedProperties();
}
void DisplayProperties()
{
dropDownSelection = EditorGUILayout.Popup(dropDownSelection, names);
if (dropDownSelection != 0)
{
var fruit = Activator.CreateInstance(types[dropDownSelection - 1]) as Fruit;
fruit.fruitName = fruit.GetType().Name;
source.fruits.Add(fruit);
dropDownSelection = 0;
}
EditorGUILayout.PropertyField(fruits, true);
}
string[] GetAllOfInterfaceNames(Type _interfaceType)
{
return GetAllOfInterface(_interfaceType)
.Select(x => x.Name)
.ToArray();
}
Type[] GetAllOfInterface(Type _interfaceType)
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(x => _interfaceType.IsAssignableFrom(x) && x != _interfaceType && !x.IsAbstract)
.OrderBy(x => x.Name)
.ToArray();
}
}
#endif