DefaultPoseScope.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class DefaultPoseScope : IDisposable
  6. {
  7. private bool m_Disposed;
  8. private SkeletonCache m_Skeleton;
  9. private BonePose[] m_Pose;
  10. private bool m_DoRestorePose = false;
  11. private bool m_UseLocalPose;
  12. public DefaultPoseScope(SkeletonCache skeleton, bool useLocalPose = true)
  13. {
  14. Debug.Assert(skeleton != null);
  15. m_Skeleton = skeleton;
  16. m_UseLocalPose = useLocalPose;
  17. if (m_Skeleton.isPosePreview)
  18. {
  19. m_DoRestorePose = true;
  20. if(useLocalPose)
  21. m_Pose = m_Skeleton.GetLocalPose();
  22. else
  23. m_Pose = m_Skeleton.GetWorldPose();
  24. m_Skeleton.RestoreDefaultPose();
  25. }
  26. }
  27. ~DefaultPoseScope()
  28. {
  29. if (!m_Disposed)
  30. Debug.LogError("Scope was not disposed! You should use the 'using' keyword or manually call Dispose.");
  31. }
  32. public void Dispose()
  33. {
  34. if (m_Disposed)
  35. return;
  36. m_Disposed = true;
  37. if (m_Skeleton != null && m_DoRestorePose)
  38. {
  39. if(m_UseLocalPose)
  40. m_Skeleton.SetLocalPose(m_Pose);
  41. else
  42. m_Skeleton.SetWorldPose(m_Pose);
  43. }
  44. }
  45. }
  46. }