TowerDefenseGame/Assets/Scripts/Editor/AnimationTester.cs

66 lines
1.9 KiB
C#

using UnityEngine;
using UnityEditor;
public class AnimationTesterWindow : EditorWindow {
private GameObject targetObject;
private Animator animator;
private string[] animationClips;
private int selectedClipIndex = 0;
[MenuItem("Tools/Animation Tester")]
public static void ShowWindow() {
GetWindow<AnimationTesterWindow>("Animation Tester");
}
private void OnGUI() {
GUILayout.Label("Animation Tester", EditorStyles.boldLabel);
targetObject = (GameObject)EditorGUILayout.ObjectField("Target GameObject", targetObject, typeof(GameObject), true);
if (targetObject != null) {
if (animator == null || animator.gameObject != targetObject) {
animator = targetObject.GetComponent<Animator>();
LoadAnimationClips();
}
} else {
animator = null;
}
if (animator == null) {
EditorGUILayout.HelpBox("Selected GameObject must have an Animator.", MessageType.Warning);
return;
}
if (animationClips == null || animationClips.Length == 0) {
EditorGUILayout.HelpBox("No animation clips found in Animator.", MessageType.Info);
return;
}
selectedClipIndex = EditorGUILayout.Popup("Animation Clip", selectedClipIndex, animationClips);
if (GUILayout.Button("Play Animation")) {
PlaySelectedAnimation();
}
}
private void LoadAnimationClips() {
if (animator.runtimeAnimatorController != null) {
var clips = animator.runtimeAnimatorController.animationClips;
animationClips = new string[clips.Length];
for (int i = 0; i < clips.Length; i++) {
animationClips[i] = clips[i].name;
}
} else {
animationClips = new string[0];
}
}
private void PlaySelectedAnimation() {
if (Application.isPlaying) {
animator.Play(animationClips[selectedClipIndex]);
} else {
Debug.LogWarning("You must be in Play Mode to preview animations.");
}
}
}