TransformResetter.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Samples.InverseKinematics
  5. {
  6. /// <summary>Records the positions and rotations of a set of objects so they can be returned later on.</summary>
  7. ///
  8. /// <remarks>
  9. /// <strong>Sample:</strong>
  10. /// <see href="https://kybernetik.com.au/animancer/docs/samples/ik/puppet">
  11. /// Puppet</see>
  12. /// </remarks>
  13. ///
  14. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.InverseKinematics/TransformResetter
  15. ///
  16. [AddComponentMenu(Strings.SamplesMenuPrefix + "Inverse Kinematics - Transform Resetter")]
  17. [AnimancerHelpUrl(typeof(TransformResetter))]
  18. public class TransformResetter : MonoBehaviour
  19. {
  20. /************************************************************************************************************************/
  21. [SerializeField] private Transform[] _Transforms;
  22. private Vector3[] _StartingPositions;
  23. private Quaternion[] _StartingRotations;
  24. /************************************************************************************************************************/
  25. protected virtual void Awake()
  26. {
  27. int count = _Transforms.Length;
  28. _StartingPositions = new Vector3[count];
  29. _StartingRotations = new Quaternion[count];
  30. for (int i = 0; i < count; i++)
  31. {
  32. Transform transform = _Transforms[i];
  33. _StartingPositions[i] = transform.localPosition;
  34. _StartingRotations[i] = transform.localRotation;
  35. }
  36. }
  37. /************************************************************************************************************************/
  38. // Called by a UI Button.
  39. // This method is not called Reset because that is a MonoBehaviour message (like Awake).
  40. // That would cause Unity to call it in Edit Mode when we first add this component.
  41. // And since the _StartingPositions would be null it would throw a NullReferenceException.
  42. public void ReturnToStartingValues()
  43. {
  44. for (int i = 0; i < _Transforms.Length; i++)
  45. _Transforms[i].SetLocalPositionAndRotation(_StartingPositions[i], _StartingRotations[i]);
  46. }
  47. /************************************************************************************************************************/
  48. }
  49. }