AsyncTriggerExtensions.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System.Threading;
  3. using UnityEngine;
  4. using Cysharp.Threading.Tasks.Triggers;
  5. namespace Cysharp.Threading.Tasks
  6. {
  7. public static class UniTaskCancellationExtensions
  8. {
  9. #if UNITY_2022_2_OR_NEWER
  10. /// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
  11. public static CancellationToken GetCancellationTokenOnDestroy(this MonoBehaviour monoBehaviour)
  12. {
  13. return monoBehaviour.destroyCancellationToken;
  14. }
  15. #endif
  16. /// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
  17. public static CancellationToken GetCancellationTokenOnDestroy(this GameObject gameObject)
  18. {
  19. return gameObject.GetAsyncDestroyTrigger().CancellationToken;
  20. }
  21. /// <summary>This CancellationToken is canceled when the MonoBehaviour will be destroyed.</summary>
  22. public static CancellationToken GetCancellationTokenOnDestroy(this Component component)
  23. {
  24. #if UNITY_2022_2_OR_NEWER
  25. if (component is MonoBehaviour mb)
  26. {
  27. return mb.destroyCancellationToken;
  28. }
  29. #endif
  30. return component.GetAsyncDestroyTrigger().CancellationToken;
  31. }
  32. }
  33. }
  34. namespace Cysharp.Threading.Tasks.Triggers
  35. {
  36. public static partial class AsyncTriggerExtensions
  37. {
  38. // Util.
  39. static T GetOrAddComponent<T>(GameObject gameObject)
  40. where T : Component
  41. {
  42. #if UNITY_2019_2_OR_NEWER
  43. if (!gameObject.TryGetComponent<T>(out var component))
  44. {
  45. component = gameObject.AddComponent<T>();
  46. }
  47. #else
  48. var component = gameObject.GetComponent<T>();
  49. if (component == null)
  50. {
  51. component = gameObject.AddComponent<T>();
  52. }
  53. #endif
  54. return component;
  55. }
  56. // Special for single operation.
  57. /// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
  58. public static UniTask OnDestroyAsync(this GameObject gameObject)
  59. {
  60. return gameObject.GetAsyncDestroyTrigger().OnDestroyAsync();
  61. }
  62. /// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
  63. public static UniTask OnDestroyAsync(this Component component)
  64. {
  65. return component.GetAsyncDestroyTrigger().OnDestroyAsync();
  66. }
  67. public static UniTask StartAsync(this GameObject gameObject)
  68. {
  69. return gameObject.GetAsyncStartTrigger().StartAsync();
  70. }
  71. public static UniTask StartAsync(this Component component)
  72. {
  73. return component.GetAsyncStartTrigger().StartAsync();
  74. }
  75. public static UniTask AwakeAsync(this GameObject gameObject)
  76. {
  77. return gameObject.GetAsyncAwakeTrigger().AwakeAsync();
  78. }
  79. public static UniTask AwakeAsync(this Component component)
  80. {
  81. return component.GetAsyncAwakeTrigger().AwakeAsync();
  82. }
  83. }
  84. }