AnimationEditorWindows.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace ChivaXR
  8. {
  9. /// <summary>
  10. /// Animation组件编辑状态(Scene运行)
  11. /// </summary>
  12. public class AnimationEditorWindows : EditorWindow
  13. {
  14. [MenuItem("ChivaTool/AniEditor& &#I")]
  15. public static void ShowWindows()
  16. {
  17. EditorWindow.GetWindow<AnimationEditorWindows>().Show();
  18. }
  19. public Animation _animation; //片段
  20. public float AnimationMaxFrame
  21. {
  22. get
  23. {
  24. if (!_animation) return 0;
  25. return _animation.clip.length * _animation.clip.frameRate;
  26. }
  27. }
  28. public float _minValue = 0; //区间最小值
  29. public float _maxValue = 1; //区间最大值
  30. public int _minFrame; //区间最小值帧数
  31. public int _maxFrame; //区间最大值帧数
  32. public float _value; //Clip片段指定Value
  33. public bool _isStartAni; //是否开启动画控制
  34. List<string> animationClipNames = new List<string>();
  35. private void OnEnable()
  36. {
  37. EditorApplication.update += EditorStateUpdate;
  38. }
  39. private void OnDisable()
  40. {
  41. EditorApplication.update -= EditorStateUpdate;
  42. }
  43. public void OnGUI()
  44. {
  45. EditorGUILayout.Space();
  46. this._animation = EditorGUILayout.ObjectField("", this._animation, typeof(Animation), true) as Animation;
  47. _minFrame = Mathf.Clamp(EditorGUILayout.IntField("StartFrame:", _minFrame), 0, Mathf.FloorToInt(AnimationMaxFrame));
  48. _maxFrame = Mathf.Clamp(EditorGUILayout.IntField("EndFrame:", _maxFrame), 0, Mathf.FloorToInt(AnimationMaxFrame));
  49. UpdateFrameToValue();
  50. EditorGUILayout.MinMaxSlider("动画区间选择", ref _minValue, ref _maxValue, 0, 1);
  51. UpdateValueToFrame();
  52. _value = EditorGUILayout.Slider("动画区间进度", _value, _minValue, _maxValue);
  53. EditorGUILayout.TextField("动画名称:" + GetCurrentClipName() + "----当前帧:" + GetCurrentFrame() );
  54. GUILayout.BeginHorizontal();
  55. if (GUILayout.Button("上一片段"))
  56. {
  57. if (CheckClipNames() && animationClipNames.IndexOf(this._animation.clip.name) > 0)
  58. {
  59. this._animation.clip = this._animation[animationClipNames[animationClipNames.IndexOf(this._animation.clip.name) - 1]].clip;
  60. _minFrame = 0;
  61. _maxFrame = Mathf.FloorToInt(AnimationMaxFrame);
  62. }
  63. }
  64. if (GUILayout.Button("下一片段"))
  65. {
  66. if (CheckClipNames() && animationClipNames.IndexOf(this._animation.clip.name) < animationClipNames.Count - 1)
  67. {
  68. this._animation.clip = this._animation[animationClipNames[animationClipNames.IndexOf(this._animation.clip.name) + 1]].clip;
  69. _minFrame = 0;
  70. _maxFrame = Mathf.FloorToInt(AnimationMaxFrame);
  71. }
  72. }
  73. GUILayout.EndHorizontal();
  74. if (!_isStartAni)
  75. {
  76. if (GUILayout.Button("开启动画控制器"))
  77. {
  78. _isStartAni = true;
  79. }
  80. }
  81. else
  82. {
  83. if (GUILayout.Button("关闭动画控制器"))
  84. {
  85. _isStartAni = false;
  86. }
  87. }
  88. }
  89. public void UpdateValueToFrame()
  90. {
  91. if (!_animation) return;
  92. _minFrame = Mathf.RoundToInt(AnimationMaxFrame * _minValue);
  93. _maxFrame = Mathf.RoundToInt(AnimationMaxFrame * _maxValue);
  94. }
  95. public bool CheckClipNames()
  96. {
  97. if (this._animation == null) return false;
  98. if (this._animation && this.animationClipNames.Count > 0) return true;
  99. animationClipNames.Clear();
  100. foreach (AnimationState state in this._animation)
  101. {
  102. animationClipNames.Add(state.name);
  103. }
  104. animationClipNames = animationClipNames.OrderBy(
  105. x => float.Parse(Regex.Match(x, "\\d+(\\.?\\d?)").Value)).ToList();
  106. return true;
  107. }
  108. public void UpdateFrameToValue()
  109. {
  110. if (!_animation) return;
  111. _minValue = _minFrame / AnimationMaxFrame;
  112. _maxValue = _maxFrame / AnimationMaxFrame;
  113. }
  114. private void EditorStateUpdate()
  115. {
  116. SetCurrentAni();
  117. }
  118. public void SetCurrentAni()
  119. {
  120. if (_animation == null || !_isStartAni) return;
  121. AnimationState aniState = _animation[_animation.clip.name];
  122. aniState.enabled = true;
  123. aniState.speed = 0;
  124. aniState.weight = 1;
  125. aniState.normalizedTime = _value;
  126. _animation.Sample();
  127. aniState.enabled = false;
  128. }
  129. public int GetCurrentFrame()
  130. {
  131. if (!_animation)
  132. {
  133. return 0;
  134. }
  135. return Mathf.RoundToInt(AnimationMaxFrame * _value);
  136. }
  137. public string GetCurrentClipName()
  138. {
  139. if(!_animation)
  140. {
  141. return "";
  142. }
  143. return _animation.clip.name;
  144. }
  145. }
  146. }