- Home /
How to re-arrange button onclick event in unity?
I was trying move up and down the button functions, which I applied before. Unfortunately I was unable to swipe up or down the functions. Is it possible to do so? Please help.
Even if you could, you should not rely on the order of the callbacks. AFAI$$anonymous$$, callbacks may not be called in the order they have been provided.
Answer by vlab22 · Jul 06, 2021 at 01:33 PM
I guess I solve it right now.
Unity 2020.3.5f1
Apparently works with all UnityEvents because of the
[CustomPropertyDrawer(typeof(UnityEventBase), true)] //includeChildren param
So it's applied to all subclasses of UnityEvent
Put this code inside an Editor folder.
using UnityEditor;
using UnityEditorInternal;
using UnityEngine.Events;
[CustomPropertyDrawer(typeof(UnityEventBase), true)]
public class BaseCustomUnityEventDrawer : UnityEventDrawer
{
protected override void SetupReorderableList(ReorderableList list)
{
base.SetupReorderableList(list);
list.draggable = true;
}
}
I really don't know why it's not a default.
Code explanation,
Using the UnityEventDrawer to hack in UnityEvent custom editor.
Before this, I had lots of headaches trying to redraw, add elements, etc to the UnityEvent inspector but I've got strange errors because of how UnityEvent draws in the inspector.
Couldn't do anything override OnGUI etc.
So while researching the drawer I found this ReorderableList field (draggable) and voilá!
https://github.com/vlab22/unityevent-reordering
Another reason why threads shouldn't be locked, blocked, expired, etc. I really got nothing to add just a big massive thank you. Just bumped into this a few hours after dealing with libraries that would try to do this by hand capturing Editor events and whatnot.
Answer by daviddickball · Feb 02, 2018 at 05:56 PM
Apparently we can't do this, and there's a technical reason why not. Apparently "internally this uses a delegate where call order is not defined" - there's more info and discussion about this here: https://forum.unity.com/threads/solved-unityevent-invoke-calling-order.265292/#post-1753393
Answer by TheFastLane · Oct 05, 2020 at 01:42 PM
I've seen the code myself, and it does have a very specific order. ...listeners shown in the inspector are called in the order shown first, then listeners added programmatically are called in the order they were added.
See @JoshuaMcKenzie answer that indicates there is a specific order here: Button On Click execution order?
If his answer is correct there should absolutely be functionality for re-arranging the event call order in the inspector.