AnimancerEvent.ParameterBoxed.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerEvent
  6. partial struct AnimancerEvent
  7. {
  8. /// <summary>
  9. /// An <see cref="Parameter{T}"/>s which internally boxes value types
  10. /// to avoid re-boxing them every <see cref="Invoke"/>.
  11. /// </summary>
  12. /// https://kybernetik.com.au/animancer/api/Animancer/ParameterBoxed_1
  13. public abstract class ParameterBoxed<T> : Parameter<T>,
  14. IParameter
  15. #if UNITY_EDITOR
  16. , ISerializationCallbackReceiver
  17. #endif
  18. where T : struct
  19. {
  20. /************************************************************************************************************************/
  21. private object _Boxed;
  22. /// <inheritdoc/>
  23. public override T Value
  24. {
  25. get => base.Value;
  26. set
  27. {
  28. base.Value = value;
  29. _Boxed = null;
  30. }
  31. }
  32. /// <inheritdoc/>
  33. object IParameter.Value
  34. {
  35. get => Value;
  36. set => Value = (T)value;
  37. }
  38. /// <inheritdoc/>
  39. public override void Invoke()
  40. {
  41. CurrentParameter = _Boxed ??= base.Value;
  42. Current.InvokeBoundCallback();
  43. }
  44. /************************************************************************************************************************/
  45. #if UNITY_EDITOR
  46. /************************************************************************************************************************/
  47. /// <inheritdoc/>
  48. void ISerializationCallbackReceiver.OnBeforeSerialize() { }
  49. /// <inheritdoc/>
  50. void ISerializationCallbackReceiver.OnAfterDeserialize()
  51. => _Boxed = null;
  52. /************************************************************************************************************************/
  53. #endif
  54. /************************************************************************************************************************/
  55. }
  56. }
  57. }