195 lines
5.8 KiB
C#
195 lines
5.8 KiB
C#
using System.Linq;
|
|
using AibisDream.Framework;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace AibisDream.SystemEditor
|
|
{
|
|
[CustomEditor(typeof(SpriteGroupKit))]
|
|
public class SpriteGroupEditor : Editor
|
|
{
|
|
private int _stateIdx;
|
|
private string _curState;
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
// 获取对象
|
|
var example = (SpriteGroupKit)target;
|
|
|
|
// 先绘制默认的 Inspector
|
|
DrawDefaultInspector();
|
|
|
|
#region 画UI
|
|
|
|
LoadUIPart(example); // 加载
|
|
EditorGUILayout.Separator();
|
|
SwitchStateUIPart(example); // 状态管理
|
|
EditorGUILayout.Separator();
|
|
ReorderLayer(example); // 重排序
|
|
EditorGUILayout.Separator();
|
|
SaveUIPart(example); // 保存
|
|
|
|
|
|
#endregion
|
|
}
|
|
|
|
private void LoadUIPart(SpriteGroupKit example)
|
|
{
|
|
EditorGUILayout.LabelField("加载图片集", EditorKit.BoldFont);
|
|
|
|
example.config.key = EditorGUILayout.TextField("唯一索引", example.config.key);
|
|
example.spriteGroup =
|
|
EditorGUILayout.ObjectField("PSD文件", example.spriteGroup, typeof(GameObject), true) as GameObject;
|
|
|
|
#region 加载按钮
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
if (GUILayout.Button("按PSD加载"))
|
|
{
|
|
if (example.spriteGroup)
|
|
{
|
|
var option = ShowWarningDialog();
|
|
if (option == 0)
|
|
{
|
|
example.InstantiateByPsd();
|
|
ReLoad();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("PSD没有设置");
|
|
}
|
|
}
|
|
|
|
if (GUILayout.Button("按索引加载"))
|
|
{
|
|
if (!string.IsNullOrEmpty(example.config.key))
|
|
{
|
|
var option = ShowWarningDialog();
|
|
if (option == 0)
|
|
{
|
|
example.InstantiateByConfig();
|
|
ReLoad();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("PSD没有设置");
|
|
}
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
#endregion
|
|
}
|
|
|
|
private void SwitchStateUIPart(SpriteGroupKit example)
|
|
{
|
|
EditorGUILayout.LabelField("状态切换", EditorKit.BoldFont);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
var stateArray = example.config.stateDict.Keys.ToArray();
|
|
var newIndex = EditorGUILayout.Popup(_stateIdx, stateArray);
|
|
_curState = EditorGUILayout.TextField(_curState);
|
|
|
|
if (newIndex != _stateIdx && newIndex >= 0 && newIndex < stateArray.Length)
|
|
{
|
|
_stateIdx = newIndex;
|
|
_curState = stateArray[newIndex];
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("保存为状态"))
|
|
{
|
|
OnSaveStateClick(example);
|
|
}
|
|
|
|
if (GUILayout.Button("加载状态"))
|
|
{
|
|
OnLoadStateClick(example);
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
}
|
|
|
|
private void OnSaveStateClick(SpriteGroupKit example)
|
|
{
|
|
if (!string.IsNullOrEmpty(_curState))
|
|
{
|
|
example.SaveState(_curState);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("状态名不能为空");
|
|
}
|
|
}
|
|
|
|
private void OnLoadStateClick(SpriteGroupKit example)
|
|
{
|
|
if (!string.IsNullOrEmpty(_curState) && example.config.stateDict.ContainsKey(_curState))
|
|
{
|
|
EditorKit.RecordObjectAndChildren(example.gameObject, "加载状态");
|
|
example.SwitchState(_curState);
|
|
EditorUtility.SetDirty(example);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("状态名不能为空");
|
|
}
|
|
}
|
|
|
|
private void ReorderLayer(SpriteGroupKit example)
|
|
{
|
|
EditorGUILayout.LabelField("显示层级", EditorKit.BoldFont);
|
|
example.config.layerId = EditorKit.SortingLayerField("目标层级", example.config.layerId);
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
example.config.maxLayer = EditorGUILayout.IntField("max", example.config.maxLayer);
|
|
example.config.minLayer = EditorGUILayout.IntField("min", example.config.minLayer);
|
|
EditorGUILayout.EndHorizontal();
|
|
if (GUILayout.Button("重排序"))
|
|
{
|
|
EditorKit.RecordObjectAndChildren(example.gameObject, "重新排序");
|
|
example.Reorder();
|
|
EditorUtility.SetDirty(example);
|
|
}
|
|
}
|
|
|
|
private void SaveUIPart(SpriteGroupKit example)
|
|
{
|
|
EditorGUILayout.LabelField("保存图片集", EditorKit.BoldFont);
|
|
if (GUILayout.Button("保存"))
|
|
{
|
|
example.SaveSpriteGroup();
|
|
}
|
|
|
|
if (GUILayout.Button("刷新"))
|
|
{
|
|
Repaint();
|
|
}
|
|
}
|
|
|
|
private void ReLoad()
|
|
{
|
|
// 清空编辑器数据
|
|
_stateIdx = -1;
|
|
_curState = "";
|
|
}
|
|
|
|
private int ShowWarningDialog()
|
|
{
|
|
return EditorUtility.DisplayDialogComplex(
|
|
"Warning",
|
|
"点击后会清除当前的图集,请提前保存",
|
|
"OK",
|
|
"Cancel",
|
|
string.Empty
|
|
);
|
|
}
|
|
}
|
|
} |