AnimationWindowUtil.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace net.shutosg.UnityEditor
  6. {
  7. public static class AnimationWindowUtil
  8. {
  9. private static readonly Type AnimWindowType = UnityEditorAssembly.GetClassType("AnimationWindow");
  10. public static EditorWindow[] GetOpenedAnimationWindows()
  11. {
  12. return (EditorWindow[])Resources.FindObjectsOfTypeAll(AnimWindowType);
  13. }
  14. public static bool IsAnimationWindow(this EditorWindow window)
  15. {
  16. return window.GetType() == AnimWindowType;
  17. }
  18. public static EditorWindow GetAnimationWindow()
  19. {
  20. return EditorWindow.GetWindow(AnimWindowType, false, "Animation", false);
  21. }
  22. public static AnimEditor GetAnimEditor()
  23. {
  24. var focusedWindow = EditorWindow.focusedWindow;
  25. var openedAnimationWindows = GetOpenedAnimationWindows();
  26. // そもそもAnimationウィンドウを開いているかチェック
  27. if (openedAnimationWindows.Length == 0) throw new AnimationWindowNotFoundException();
  28. if (openedAnimationWindows.Length > 1 && !focusedWindow.IsAnimationWindow()) throw new AmbiguousAnimationWindowException();
  29. var animationWindow = GetAnimationWindow();
  30. var animEditor = animationWindow.GetAnimEditor();
  31. return animEditor;
  32. }
  33. public static Keyframe[] GetSelectedKeyframes()
  34. {
  35. var curveEditor = GetAnimEditor().CurveEditor;
  36. return curveEditor.SelectedCurves.Select(c => curveEditor.GetKeyframe(c)).ToArray();
  37. }
  38. }
  39. public class AnimationWindowNotFoundException : Exception { }
  40. public class AmbiguousAnimationWindowException : Exception { }
  41. }