MotionHandleLinker.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Runtime.CompilerServices;
  2. using UnityEngine;
  3. using LitMotion.Collections;
  4. namespace LitMotion
  5. {
  6. [DisallowMultipleComponent]
  7. [AddComponentMenu("")]
  8. internal sealed class MotionHandleLinker : MonoBehaviour
  9. {
  10. FastListCore<MotionHandle> cancelOnDestroyList;
  11. FastListCore<MotionHandle> cancelOnDisableList;
  12. FastListCore<MotionHandle> completeOnDisableList;
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public void Register(MotionHandle handle, LinkBehaviour linkBehaviour)
  15. {
  16. switch (linkBehaviour)
  17. {
  18. case LinkBehaviour.CancelOnDestroy:
  19. cancelOnDestroyList.Add(handle);
  20. break;
  21. case LinkBehaviour.CancelOnDisable:
  22. cancelOnDisableList.Add(handle);
  23. break;
  24. case LinkBehaviour.CompleteOnDisable:
  25. completeOnDisableList.Add(handle);
  26. break;
  27. }
  28. }
  29. void OnDisable()
  30. {
  31. var cancelSpan = cancelOnDisableList.AsSpan();
  32. for (int i = 0; i < cancelSpan.Length; i++)
  33. {
  34. ref var handle = ref cancelSpan[i];
  35. if (handle.IsActive()) handle.Cancel();
  36. }
  37. var completeSpan = completeOnDisableList.AsSpan();
  38. for (int i = 0; i < completeSpan.Length; i++)
  39. {
  40. ref var handle = ref completeSpan[i];
  41. if (handle.IsActive()) handle.Complete();
  42. }
  43. }
  44. void OnDestroy()
  45. {
  46. var span = cancelOnDestroyList.AsSpan();
  47. for (int i = 0; i < span.Length; i++)
  48. {
  49. ref var handle = ref span[i];
  50. if (handle.IsActive()) handle.Cancel();
  51. }
  52. }
  53. }
  54. }