SolidAlloy / GenericUnityObjects

Generic UnityEngine.Objects
Other
167 stars 13 forks source link

Can't Reorder Elements of a UnityEvent #17

Closed JingHaoYao closed 3 years ago

JingHaoYao commented 3 years ago

Hi, I've been working with UnityEvents in the inspector and I noticed in GenericUnityInternals that it overrides a custom property drawer for unityevents.

https://forum.unity.com/threads/reorder-unity-events-in-the-inspector.350712/ https://answers.unity.com/questions/1398221/how-to-re-arrange-button-onclick-event-in-unity.html?childToView=1846873#answer-1846873

I've been wanting to reorder callbacks of unityevents in the inspector (as unity doesn't seem to be changing the way listeners are called anytime soon), but I can't write a custom property drawer while GenericUnityEventDrawer does so. Would it be possible to override SetupReorderableList(ReorderableList list) in GenericUnityEventDrawer and set the list to be draggable?

Thank you!

SolidAlloy commented 3 years ago

Hi, great advice! I've made callbacks reorderable by default and will release it in a new version soon, but you should be able to override GenericUnityEventDrawer yourself too.

  1. Create a property drawer for UnityEvents.
  2. Use DrawerReplacer.ReplaceDefaultDrawer to override the default drawer
  3. Use callback order 1 or greater in DidReloadScripts so that it runs after GenericUnityEventDrawer

    [CustomPropertyDrawer(typeof(UnityEventBase), true)]
    public class CustomUnityEventDrawer : GenericUnityEventDrawer
    {
    // You can inherit from the default UnityEventDrawer too.
    
    [DidReloadScripts(1)]
    private static void ReplaceDefaultDrawer()
    {
        DrawerReplacer.ReplaceDefaultDrawer<UnityEventBase, CustomUnityEventDrawer>();
    }
    }
SolidAlloy commented 3 years ago

Callbacks are now reorderable in 2.10.0 :tada: