Files

52 lines
2.5 KiB
C#

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace AibisDream.FrameAnimation.Editor
{
[CustomEditor(typeof(FrameClipPlayer))]
public sealed class FrameClipPlayerEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("clip"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("playOnEnable"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("speed"));
DrawWarnings();
serializedObject.ApplyModifiedProperties();
if (!Application.isPlaying) return;
var player = (FrameClipPlayer)target;
EditorGUILayout.Space();
EditorGUILayout.LabelField("Runtime State", EditorStyles.boldLabel);
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.EnumPopup("State", player.State);
EditorGUILayout.ObjectField("Clip", player.CurrentClip, typeof(FrameClip), false);
EditorGUILayout.TextField("Clip ID", player.CurrentClipId);
EditorGUILayout.IntField("Frame", player.CurrentFrameIndex);
}
Repaint();
}
private void DrawWarnings()
{
var player = (FrameClipPlayer)target;
var renderer = player.GetComponent<SpriteRenderer>();
var image = player.GetComponent<Image>();
if (player.GetComponent<FrameAnimationPlayer>() != null)
EditorGUILayout.HelpBox("同一 GameObject 不能同时包含 FrameAnimationPlayer 和 FrameClipPlayer。", MessageType.Error);
if (renderer == null && image == null)
EditorGUILayout.HelpBox("同一 GameObject 上必须存在一个 SpriteRenderer 或 Image。", MessageType.Error);
else if (renderer != null && image != null)
EditorGUILayout.HelpBox("同一 GameObject 不能同时存在 SpriteRenderer 和 Image。", MessageType.Error);
if (serializedObject.FindProperty("clip").objectReferenceValue == null)
EditorGUILayout.HelpBox("尚未绑定默认 FrameClip;仍可通过 Play(FrameClip) 临时播放。", MessageType.Info);
var value = serializedObject.FindProperty("speed").floatValue;
if (!FrameAnimationValueUtility.IsValidSpeed(value))
EditorGUILayout.HelpBox("Speed 必须是有限且不小于 0 的数值。", MessageType.Error);
}
}
}