Last active
November 23, 2021 10:07
-
-
Save DmitriyYukhanov/0ecf36beb1433fa1d65df10245c43681 to your computer and use it in GitHub Desktop.
Listen to AdvancedDropdown selection cancellation in Unity 2022.1 using Reflection (actual as per 2022.1.0a15)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CodeStage.Gists.Editor | |
{ | |
using System; | |
using System.Reflection; | |
using UnityEditor.IMGUI.Controls; | |
using UnityEngine; | |
public class DropdownSelectionCanceledExample : AdvancedDropdown | |
{ | |
//... | |
private void SubscribeToSelectionCancel() | |
{ | |
var field = typeof(AdvancedDropdown).GetField("m_WindowInstance", BindingFlags.Instance | BindingFlags.NonPublic); | |
if (field == null) | |
{ | |
Debug.LogError($"Couldn't find {nameof(AdvancedDropdown)}.m_WindowInstance field!"); | |
return; | |
} | |
var windowInstance = field.GetValue(this); | |
if (windowInstance == null) | |
{ | |
Debug.LogError($"Couldn't get {nameof(windowInstance)}!"); | |
return; | |
} | |
var selectionCanceledEventInfo = windowInstance.GetType().GetEvent("selectionCanceled", BindingFlags.Instance | BindingFlags.Public); | |
if (selectionCanceledEventInfo == null) | |
{ | |
Debug.LogError("Couldn't find AdvancedDropdownWindow.selectionCanceled event!"); | |
return; | |
} | |
Action handler = OnSelectionCanceled; | |
Delegate convertedHandler = Delegate.CreateDelegate(selectionCanceledEventInfo.EventHandlerType, handler.Target, handler.Method); | |
selectionCanceledEventInfo.AddEventHandler(windowInstance, convertedHandler); | |
} | |
private void OnSelectionCanceled() | |
{ | |
Debug.Log("An AdvancedDropdown closed without selection."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment