Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIBehaviourExtensions.cs 5.4KB

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