MonoBehaviourMessagesTriggers.tt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. <#
  8. var empty = new (string, string)[0];
  9. var triggers = new (string triggerName, string methodName, string returnType, string handlerInterface, (string argType, string argName)[] arguments)[]
  10. {
  11. ("AnimatorIK", "OnAnimatorIK", "int", null, new []{ ("int", "layerIndex") }),
  12. ("AnimatorMove", "OnAnimatorMove", "AsyncUnit", null, empty),
  13. ("OnCanvasGroupChanged", "OnCanvasGroupChanged", "AsyncUnit", null, empty ),
  14. ("CollisionEnter2D", "OnCollisionEnter2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
  15. ("CollisionExit2D", "OnCollisionExit2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
  16. ("CollisionStay2D", "OnCollisionStay2D", "Collision2D", null, new []{ ("Collision2D", "coll") }),
  17. ("CollisionEnter", "OnCollisionEnter", "Collision", null, new []{ ("Collision", "coll") }),
  18. ("CollisionExit", "OnCollisionExit", "Collision", null, new []{ ("Collision", "coll") }),
  19. ("CollisionStay", "OnCollisionStay", "Collision", null, new []{ ("Collision", "coll") }),
  20. ("Enable", "OnEnable", "AsyncUnit", null, empty),
  21. ("Disable", "OnDisable", "AsyncUnit", null, empty),
  22. ("JointBreak", "OnJointBreak", "float", null, new []{ ("float", "breakForce") }),
  23. ("JointBreak2D", "OnJointBreak2D", "Joint2D", null, new []{ ("Joint2D", "brokenJoint") }),
  24. ("Update", "Update", "AsyncUnit", null, empty),
  25. ("FixedUpdate", "FixedUpdate", "AsyncUnit", null, empty),
  26. ("LateUpdate", "LateUpdate", "AsyncUnit", null, empty),
  27. ("ParticleCollision", "OnParticleCollision", "GameObject", null, new []{ ("GameObject", "other") }),
  28. ("RectTransformDimensionsChange", "OnRectTransformDimensionsChange", "AsyncUnit", null, empty),
  29. ("RectTransformRemoved", "OnRectTransformRemoved", "AsyncUnit", null, empty),
  30. ("BeforeTransformParentChanged", "OnBeforeTransformParentChanged", "AsyncUnit", null, empty),
  31. ("TransformParentChanged", "OnTransformParentChanged", "AsyncUnit", null, empty),
  32. ("TransformChildrenChanged", "OnTransformChildrenChanged", "AsyncUnit", null, empty),
  33. ("TriggerEnter2D", "OnTriggerEnter2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
  34. ("TriggerExit2D", "OnTriggerExit2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
  35. ("TriggerStay2D", "OnTriggerStay2D", "Collider2D", null, new []{ ("Collider2D", "other") }),
  36. ("TriggerEnter", "OnTriggerEnter", "Collider", null, new []{ ("Collider", "other") }),
  37. ("TriggerExit", "OnTriggerExit", "Collider", null, new []{ ("Collider", "other") }),
  38. ("TriggerStay", "OnTriggerStay", "Collider", null, new []{ ("Collider", "other") }),
  39. ("BecameInvisible", "OnBecameInvisible", "AsyncUnit", null, empty),
  40. ("BecameVisible", "OnBecameVisible", "AsyncUnit", null, empty),
  41. // Mouse... #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
  42. ("MouseDown", "OnMouseDown", "AsyncUnit", null, empty),
  43. ("MouseDrag", "OnMouseDrag", "AsyncUnit", null, empty),
  44. ("MouseEnter", "OnMouseEnter", "AsyncUnit", null, empty),
  45. ("MouseExit", "OnMouseExit", "AsyncUnit", null, empty),
  46. ("MouseOver", "OnMouseOver", "AsyncUnit", null, empty),
  47. ("MouseUp", "OnMouseUp", "AsyncUnit", null, empty),
  48. ("MouseUpAsButton", "OnMouseUpAsButton", "AsyncUnit", null, empty),
  49. // new in v2
  50. ("ApplicationFocus", "OnApplicationFocus", "bool", null, new []{("bool", "hasFocus") }),
  51. ("ApplicationPause", "OnApplicationPause", "bool", null, new []{("bool", "pauseStatus") }),
  52. ("ApplicationQuit", "OnApplicationQuit", "AsyncUnit", null, empty),
  53. ("AudioFilterRead", "OnAudioFilterRead", "(float[] data, int channels)", null, new[]{("float[]", "data"), ("int", "channels")}),
  54. ("ControllerColliderHit", "OnControllerColliderHit", "ControllerColliderHit", null, new[]{("ControllerColliderHit", "hit")}),
  55. ("DrawGizmos", "OnDrawGizmos", "AsyncUnit", null, empty),
  56. ("DrawGizmosSelected", "OnDrawGizmosSelected", "AsyncUnit", null, empty),
  57. ("GUI", "OnGUI", "AsyncUnit", null, empty),
  58. ("ParticleSystemStopped", "OnParticleSystemStopped", "AsyncUnit", null, empty),
  59. ("ParticleTrigger", "OnParticleTrigger", "AsyncUnit", null, empty),
  60. ("PostRender", "OnPostRender", "AsyncUnit", null, empty),
  61. ("PreCull", "OnPreCull", "AsyncUnit", null, empty),
  62. ("PreRender", "OnPreRender", "AsyncUnit", null, empty),
  63. ("RenderImage", "OnRenderImage", "(RenderTexture source, RenderTexture destination)", null, new[]{("RenderTexture", "source"), ("RenderTexture", "destination")}),
  64. ("RenderObject", "OnRenderObject", "AsyncUnit", null, empty),
  65. ("ServerInitialized", "OnServerInitialized", "AsyncUnit", null, empty),
  66. ("Validate", "OnValidate", "AsyncUnit", null, empty),
  67. ("WillRenderObject", "OnWillRenderObject", "AsyncUnit", null, empty),
  68. ("Reset", "Reset", "AsyncUnit", null, empty),
  69. // uGUI
  70. ("BeginDrag", "OnBeginDrag", "PointerEventData", "IBeginDragHandler", new []{ ("PointerEventData", "eventData") }),
  71. ("Cancel", "OnCancel", "BaseEventData", "ICancelHandler", new []{ ("BaseEventData", "eventData") }),
  72. ("Deselect", "OnDeselect", "BaseEventData", "IDeselectHandler", new []{ ("BaseEventData", "eventData") }),
  73. ("Drag", "OnDrag", "PointerEventData", "IDragHandler", new []{ ("PointerEventData", "eventData") }),
  74. ("Drop", "OnDrop", "PointerEventData", "IDropHandler", new []{ ("PointerEventData", "eventData") }),
  75. ("EndDrag", "OnEndDrag", "PointerEventData", "IEndDragHandler", new []{ ("PointerEventData", "eventData") }),
  76. ("InitializePotentialDrag", "OnInitializePotentialDrag", "PointerEventData", "IInitializePotentialDragHandler", new []{ ("PointerEventData", "eventData") }),
  77. ("Move", "OnMove", "AxisEventData", "IMoveHandler", new []{ ("AxisEventData", "eventData") }),
  78. ("PointerClick", "OnPointerClick", "PointerEventData", "IPointerClickHandler", new []{ ("PointerEventData", "eventData") }),
  79. ("PointerDown", "OnPointerDown", "PointerEventData", "IPointerDownHandler", new []{ ("PointerEventData", "eventData") }),
  80. ("PointerEnter", "OnPointerEnter", "PointerEventData", "IPointerEnterHandler", new []{ ("PointerEventData", "eventData") }),
  81. ("PointerExit", "OnPointerExit", "PointerEventData", "IPointerExitHandler", new []{ ("PointerEventData", "eventData") }),
  82. ("PointerUp", "OnPointerUp", "PointerEventData", "IPointerUpHandler", new []{ ("PointerEventData", "eventData") }),
  83. ("Scroll", "OnScroll", "PointerEventData", "IScrollHandler", new []{ ("PointerEventData", "eventData") }),
  84. ("Select", "OnSelect", "BaseEventData", "ISelectHandler", new []{ ("BaseEventData", "eventData") }),
  85. ("Submit", "OnSubmit", "BaseEventData", "ISubmitHandler", new []{ ("BaseEventData", "eventData") }),
  86. ("UpdateSelected", "OnUpdateSelected", "BaseEventData", "IUpdateSelectedHandler", new []{ ("BaseEventData", "eventData") }),
  87. // 2019.3
  88. ("ParticleUpdateJobScheduled", "OnParticleUpdateJobScheduled", "UnityEngine.ParticleSystemJobs.ParticleSystemJobData", null, new[]{("UnityEngine.ParticleSystemJobs.ParticleSystemJobData", "particles")}),
  89. // Oneshot
  90. // Awake, Start, Destroy
  91. };
  92. triggers = triggers.OrderBy(x => x.handlerInterface != null).ThenBy(x => x.handlerInterface != null ? x.handlerInterface : x.methodName).ToArray();
  93. Func<string, string> ToInterfaceName = x => $"IAsync{x}Handler";
  94. Func<string, string> ToUniTaskName = x => x == "AsyncUnit" ? "UniTask" : $"UniTask<{x}>";
  95. Func<string, string> ToCastUniTasSourceType = x => x == "AsyncUnit" ? "IUniTaskSource" : $"IUniTaskSource<{x}>";
  96. Func<string, string> OnInvokeSuffix = x => x == "AsyncUnit" ? ".AsUniTask()" : $"";
  97. Func<(string argType, string argName)[], string> BuildMethodArgument = x => string.Join(", ", x.Select(y => y.argType + " " + y.argName));
  98. Func<(string argType, string argName)[], string> BuildResultParameter = x => x.Length == 0 ? "AsyncUnit.Default" : "(" + string.Join(", ", x.Select(y => y.argName)) + ")";
  99. Func<string, bool> IsParticleSystem = x => x == "ParticleUpdateJobScheduled";
  100. Func<string, bool> IsMouseTrigger = x => x.StartsWith("Mouse");
  101. Func<string, string> RequirePhysicsModule = x => (x.StartsWith("Collision") || x.StartsWith("Collider") || x.StartsWith("ControllerCollider") || x.StartsWith("Joint") || x.StartsWith("Trigger"))
  102. ? (x.Contains("2D") ? "UNITASK_PHYSICS2D_SUPPORT" : "UNITASK_PHYSICS_SUPPORT")
  103. : null;
  104. Func<string, bool> IsUguiSystem = x => x != null;
  105. #>
  106. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  107. using System.Threading;
  108. using UnityEngine;
  109. #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
  110. using UnityEngine.EventSystems;
  111. #endif
  112. namespace Cysharp.Threading.Tasks.Triggers
  113. {
  114. <# foreach(var t in triggers) { #>
  115. #region <#= t.triggerName #>
  116. <# if(IsUguiSystem(t.handlerInterface)) { #>
  117. #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
  118. <# } #>
  119. <# if(IsParticleSystem(t.triggerName)) { #>
  120. #if UNITY_2019_3_OR_NEWER && (!UNITY_2019_1_OR_NEWER || UNITASK_PARTICLESYSTEM_SUPPORT)
  121. <# } #>
  122. <# if(IsMouseTrigger(t.triggerName)) { #>
  123. #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_METRO)
  124. <# } #>
  125. <# if(RequirePhysicsModule(t.triggerName) != null) { #>
  126. #if !UNITY_2019_1_OR_NEWER || <#= RequirePhysicsModule(t.triggerName) #>
  127. <# } #>
  128. public interface <#= ToInterfaceName(t.methodName) #>
  129. {
  130. <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async();
  131. }
  132. public partial class AsyncTriggerHandler<T> : <#= ToInterfaceName(t.methodName) #>
  133. {
  134. <#= ToUniTaskName(t.returnType) #> <#= ToInterfaceName(t.methodName) #>.<#= t.methodName #>Async()
  135. {
  136. core.Reset();
  137. return new <#= ToUniTaskName(t.returnType) #>((<#= ToCastUniTasSourceType(t.returnType) #>)(object)this, core.Version);
  138. }
  139. }
  140. public static partial class AsyncTriggerExtensions
  141. {
  142. public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this GameObject gameObject)
  143. {
  144. return GetOrAddComponent<Async<#= t.triggerName #>Trigger>(gameObject);
  145. }
  146. public static Async<#= t.triggerName #>Trigger GetAsync<#= t.triggerName #>Trigger(this Component component)
  147. {
  148. return component.gameObject.GetAsync<#= t.triggerName #>Trigger();
  149. }
  150. }
  151. [DisallowMultipleComponent]
  152. public sealed class Async<#= t.triggerName #>Trigger : AsyncTriggerBase<<#= t.returnType #>><#= (t.handlerInterface == null) ? "" : $", {t.handlerInterface}" #>
  153. {
  154. void <#= (t.handlerInterface == null) ? "" : $"{t.handlerInterface}." #><#= t.methodName #>(<#= BuildMethodArgument(t.arguments) #>)
  155. {
  156. RaiseEvent(<#= BuildResultParameter(t.arguments) #>);
  157. }
  158. public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler()
  159. {
  160. return new AsyncTriggerHandler<<#= t.returnType #>>(this, false);
  161. }
  162. public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler(CancellationToken cancellationToken)
  163. {
  164. return new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, false);
  165. }
  166. public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async()
  167. {
  168. return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, true)).<#= t.methodName #>Async();
  169. }
  170. public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async(CancellationToken cancellationToken)
  171. {
  172. return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(this, cancellationToken, true)).<#= t.methodName #>Async();
  173. }
  174. }
  175. <# if(IsUguiSystem(t.handlerInterface)) { #>
  176. #endif
  177. <# } #>
  178. <# if(RequirePhysicsModule(t.triggerName) != null) { #>
  179. #endif
  180. <# } #>
  181. <# if(IsParticleSystem(t.triggerName) || IsMouseTrigger(t.triggerName)) { #>
  182. #endif
  183. <# } #>
  184. #endregion
  185. <# } #>
  186. }