Skip to content

Instantly share code, notes, and snippets.

@DmitriyYukhanov
Last active November 23, 2021 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitriyYukhanov/0ecf36beb1433fa1d65df10245c43681 to your computer and use it in GitHub Desktop.
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)
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