BoneGizmoToggle.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. using UnityEngine.U2D.Animation;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal interface IBoneGizmoToggle
  6. {
  7. bool enableGizmos { get; }
  8. void OnGUI();
  9. }
  10. internal class BoneGizmoToggle : IBoneGizmoToggle
  11. {
  12. private bool m_EnableGizmos;
  13. private bool m_CurrentEnableGizmoState;
  14. public bool enableGizmos
  15. {
  16. get { return m_CurrentEnableGizmoState; }
  17. }
  18. public BoneGizmoToggle()
  19. {
  20. SpriteSkin.onDrawGizmos.AddListener(OnDrawGizmos);
  21. }
  22. ~BoneGizmoToggle()
  23. {
  24. SpriteSkin.onDrawGizmos.RemoveListener(OnDrawGizmos);
  25. }
  26. //This callback will be called before OnSceneGUI in a Repaint event
  27. private void OnDrawGizmos()
  28. {
  29. m_EnableGizmos = true;
  30. //One time is enough
  31. SpriteSkin.onDrawGizmos.RemoveListener(OnDrawGizmos);
  32. }
  33. public void OnGUI()
  34. {
  35. //Ignore events other than Repaint
  36. if (Event.current.type != EventType.Repaint)
  37. return;
  38. if (m_CurrentEnableGizmoState != m_EnableGizmos)
  39. SceneView.RepaintAll();
  40. m_CurrentEnableGizmoState = m_EnableGizmos;
  41. //Assume the Gizmo toggle is disabled and listen to the event again
  42. m_EnableGizmos = false;
  43. SpriteSkin.onDrawGizmos.RemoveListener(OnDrawGizmos);
  44. SpriteSkin.onDrawGizmos.AddListener(OnDrawGizmos);
  45. }
  46. }
  47. }