Forums › đź’¬ NodeCanvas › ⚙️ Support › SerializeReference doesn’t work in custom task › Reply To: SerializeReference doesn’t work in custom task
Hello again,
I just created a Property Drawer which when used on a field, it will popup a selection of the field type implementations and create an instance of that selection. If you want to try it now please do the following:
1) Open up DrawerAttributes.cs and add this code somewhere within the namespace:
1 2 3 4 |
[AttributeUsage(AttributeTargets.Field)] public class ReferenceFieldAttribute : DrawerAttribute { } |
2) Open up AttributeDrawers.cs and add this code somewhere within the namespace as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class ReferenceFieldDrawer : AttributeDrawer<ReferenceFieldAttribute> { public override object OnGUI(GUIContent content, object instance) { var options = ReflectionTools.GetImplementationsOf(fieldInfo.FieldType); var selection = EditorUtils.Popup<System.Type>(content, instance != null? instance.GetType() : fieldInfo.FieldType, options); if (selection == null){ return instance = null; } if (instance == null || instance.GetType() != selection ) { if (!typeof(UnityEngine.Object).IsAssignableFrom(selection)){ return System.Activator.CreateInstance(selection); } } EditorGUI.indentLevel++; EditorUtils.ReflectedObjectInspector(instance, contextUnityObject); EditorGUI.indentLevel--; return instance; } } |
You can then use the [ReferenceField] attribute on your custom task interface type fields.
Please let me know how this works for you.
Thank you.