File size: 5,553 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Tests
{
public static class UIBehaviourExtensions
{
private static object InvokeMethodAndRethrow(Type type, Object obj, string methodName, params object[] args)
{
BindingFlags flags = BindingFlags.Default;
flags |= BindingFlags.Public;
flags |= BindingFlags.NonPublic;
if (obj != null)
flags |= BindingFlags.Instance;
else
flags |= BindingFlags.Static;
MethodInfo method;
try
{
// Attempt to get the method plainly at first
method = type.GetMethod(methodName, flags);
}
catch (AmbiguousMatchException)
{
// If it's ambiguous, then attempt to get it using its params (though nulls would mess things up).
method = type.GetMethod(methodName, flags, null, args.Select(a => a != null ? a.GetType() : null).Where(a => a != null).ToArray(), new ParameterModifier[0]);
}
Assert.NotNull(method, string.Format("Not method {0} found on object {1}", methodName, obj));
return method.Invoke(obj, args);
}
public static object InvokeMethodAndRethrow<T>(Object obj, string methodName, params object[] args)
{
return InvokeMethodAndRethrow(typeof(T), obj, methodName, args);
}
public static object InvokeMethodAndRethrow(Object obj, string methodName, params object[] args)
{
return InvokeMethodAndRethrow(obj.GetType(), obj, methodName, args);
}
public static void InvokeOnEnable(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnEnable");
}
public static void InvokeOnDisable(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnDisable");
}
public static void InvokeAwake(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "Awake");
}
public static void InvokeRebuild(this UIBehaviour behaviour, CanvasUpdate type)
{
InvokeMethodAndRethrow(behaviour, "Rebuild", type);
}
public static void InvokeLateUpdate(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "LateUpdate");
}
public static void InvokeUpdate(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "Update");
}
public static void InvokeOnRectTransformDimensionsChange(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnRectTransformDimensionsChange");
}
public static void InvokeOnCanvasGroupChanged(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnCanvasGroupChanged");
}
public static void InvokeOnDidApplyAnimationProperties(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnDidApplyAnimationProperties");
}
}
public static class SelectableExtensions
{
public static void InvokeOnPointerDown(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerDown", data);
}
public static void InvokeOnPointerUp(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerUp", data);
}
public static void InvokeOnPointerEnter(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerEnter", data);
}
public static void InvokeOnPointerExit(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerExit", data);
}
public static void InvokeTriggerAnimation(this Selectable selectable, string triggerName)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "TriggerAnimation", triggerName);
}
public static void InvokeOnSelect(this Selectable selectable, string triggerName)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnSelect", triggerName);
}
}
public static class GraphicExtension
{
public static void InvokeOnPopulateMesh(this Graphic graphic, VertexHelper vh)
{
UIBehaviourExtensions.InvokeMethodAndRethrow(graphic, "OnPopulateMesh", vh);
}
}
public static class GraphicRaycasterExtension
{
public static void InvokeRaycast(Canvas canvas, Camera eventCamera, Vector2 pointerPosition, List<Graphic> results)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<GraphicRaycaster>(null, "Raycast", canvas, eventCamera, pointerPosition, results);
}
}
public static class ToggleGroupExtension
{
public static void InvokeValidateToggleIsInGroup(this ToggleGroup tgroup, Toggle toggle)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<ToggleGroup>(tgroup, "ValidateToggleIsInGroup", toggle);
}
}
}
|