Created
July 16, 2019 19:00
-
-
Save jfranmora/bd590060241c41e605c81cd58982d7e4 to your computer and use it in GitHub Desktop.
Extension for Unity.UI.Toggle to set isOn value without send callbacks
This file contains hidden or 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
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