Skip to content

Instantly share code, notes, and snippets.

@jfranmora
Created July 16, 2019 19:00
Show Gist options
  • Save jfranmora/bd590060241c41e605c81cd58982d7e4 to your computer and use it in GitHub Desktop.
Save jfranmora/bd590060241c41e605c81cd58982d7e4 to your computer and use it in GitHub Desktop.
Extension for Unity.UI.Toggle to set isOn value without send callbacks
using System.Reflection;
using UnityEngine.UI;
public static class UIExtensions
{
private static readonly MethodInfo toggleMethod;
static UIExtensions()
{
toggleMethod = GetSetMethod(typeof(Toggle));
}
public static void SetSilently(this Toggle toggle, bool value)
{
toggleMethod.Invoke(toggle, new object[] { value, false });
}
private static MethodInfo GetSetMethod(System.Type type)
{
var methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
for (var i = 0; i < methods.Length; i++)
{
if (methods[i].Name == "Set" && methods[i].GetParameters().Length == 2)
{
return methods[i];
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment