com.alicizax.unity.ui.exten.../Editor/UX/UXButtonEditor.cs

617 lines
24 KiB
C#
Raw Normal View History

2025-04-11 17:26:28 +08:00
using System;
using System.Collections.Generic;
2025-07-25 20:08:46 +08:00
using AlicizaX.UI.Extension.Editor;
2025-04-11 17:26:28 +08:00
using UnityEditor;
2025-07-25 19:53:34 +08:00
using UnityEditorInternal;
2025-04-11 17:26:28 +08:00
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;
2025-07-29 11:05:15 +08:00
using AnimatorControllerParameterType = UnityEngine.AnimatorControllerParameterType;
2025-04-11 17:26:28 +08:00
[CanEditMultipleObjects]
[CustomEditor(typeof(UXButton), true)]
2025-07-28 13:04:49 +08:00
internal class UXButtonEditor : Editor
2025-04-11 17:26:28 +08:00
{
2025-07-25 19:53:34 +08:00
private enum TabType
{
Image,
Sound,
Event
}
2025-04-11 17:26:28 +08:00
private SerializedProperty m_Interactable;
private SerializedProperty m_Mode;
private SerializedProperty m_OnValueChanged;
private SerializedProperty m_OnClick;
private SerializedProperty m_UXGroup;
private SerializedProperty m_TransitionData;
private SerializedProperty m_ChildTransitions;
private UXGroup group;
private int m_ButtonMode;
2025-07-25 13:29:20 +08:00
private SerializedProperty m_SelectionState;
2025-07-25 19:53:34 +08:00
private ReorderableList m_ChildTransitionList;
private static Color darkZebraEven = new Color(0.22f, 0.22f, 0.22f);
private static Color darkZebraOdd = new Color(0.27f, 0.27f, 0.27f);
private TabType currentTab = TabType.Image;
private GUISkin customSkin;
private SerializedProperty hoverAudioClip;
private SerializedProperty clickAudioClip;
2025-04-11 17:26:28 +08:00
private void OnEnable()
{
2025-07-29 11:19:07 +08:00
customSkin = AssetDatabase.LoadAssetAtPath<GUISkin>("Packages/com.alicizax.unity.ui.extension/Editor/Res/GUISkin/UIExtensionGUISkin.guiskin");
2025-04-11 17:26:28 +08:00
m_Interactable = serializedObject.FindProperty("m_Interactable");
m_UXGroup = serializedObject.FindProperty("m_UXGroup");
m_Mode = serializedObject.FindProperty("m_Mode");
m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
m_OnClick = serializedObject.FindProperty("m_OnClick");
m_TransitionData = serializedObject.FindProperty("m_TransitionData");
m_ChildTransitions = serializedObject.FindProperty("m_ChildTransitions");
2025-07-25 13:29:20 +08:00
m_SelectionState = serializedObject.FindProperty("m_SelectionState");
2025-07-25 19:53:34 +08:00
2025-04-11 17:26:28 +08:00
group = (UXGroup)m_UXGroup.objectReferenceValue;
m_ButtonMode = m_Mode.enumValueIndex;
2025-07-25 19:53:34 +08:00
hoverAudioClip = serializedObject.FindProperty("hoverAudioClip");
clickAudioClip = serializedObject.FindProperty("clickAudioClip");
CreateChildTransitionList();
}
private void CreateChildTransitionList()
{
m_ChildTransitionList = new ReorderableList(serializedObject, m_ChildTransitions, true, false, true, true);
// m_ChildTransitionList.drawHeaderCallback = (rect) => { EditorGUI.LabelField(rect, "Other Transitions"); };
m_ChildTransitionList.drawElementBackgroundCallback = (rect, index, isActive, isFocused) =>
{
var background = index % 2 == 0 ? darkZebraEven : darkZebraOdd;
EditorGUI.DrawRect(rect, background);
};
m_ChildTransitionList.drawElementCallback = (rect, index, isActive, isFocused) =>
{
var element = m_ChildTransitionList.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
// 绘制折叠框标题(仅显示名称)
string elementTitle = $"Null Transition";
var targetProp = element.FindPropertyRelative("targetGraphic");
if (targetProp.objectReferenceValue != null)
elementTitle = targetProp.objectReferenceValue.name;
EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
elementTitle, EditorStyles.boldLabel);
// 直接绘制完整内容(无折叠状态)
rect.y += EditorGUIUtility.singleLineHeight + 2;
DrawTransitionData(new Rect(rect.x, rect.y, rect.width, 0), element, true);
};
// 设置元素高度
m_ChildTransitionList.elementHeightCallback = (index) =>
{
return EditorGUIUtility.singleLineHeight +
CalculateTransitionDataHeight(m_ChildTransitionList.serializedProperty.GetArrayElementAtIndex(index)) +
10;
};
m_ChildTransitionList.onAddCallback = (list) =>
{
list.serializedProperty.arraySize++;
serializedObject.ApplyModifiedProperties();
};
// 添加删除按钮
m_ChildTransitionList.onRemoveCallback = (list) => { ReorderableList.defaultBehaviours.DoRemoveButton(list); };
2025-04-11 17:26:28 +08:00
}
private void ResetEventProperty(SerializedProperty property)
{
SerializedProperty persistentCalls = property.FindPropertyRelative("m_PersistentCalls");
SerializedProperty calls = persistentCalls.FindPropertyRelative("m_Calls");
calls.arraySize = 0;
property.serializedObject.ApplyModifiedProperties();
}
2025-07-25 13:29:20 +08:00
2025-07-25 19:53:34 +08:00
private void DrawTabButton(TabType tabType, string label, string iconName)
{
bool isActive = currentTab == tabType;
var style = new GUIStyle(EditorStyles.toolbarButton)
{
fixedHeight = 25,
fontSize = 11,
fontStyle = isActive ? FontStyle.Bold : FontStyle.Normal
};
var content = new GUIContent(
EditorGUIUtility.IconContent(iconName).image,
label
);
if (GUILayout.Button(content, style))
{
currentTab = tabType;
}
// 高亮当前选中的标签页
if (isActive)
{
Rect rect = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(new Rect(rect.x, rect.yMax - 2, rect.width, 2),
new Color(0.1f, 0.5f, 0.9f));
}
}
2025-04-11 17:26:28 +08:00
public override void OnInspectorGUI()
{
serializedObject.Update();
2025-07-25 19:53:34 +08:00
EditorGUILayout.BeginHorizontal();
// 图像标签页按钮
DrawTabButton(TabType.Image, "Image", "d_Texture Icon");
// 声音标签页按钮
DrawTabButton(TabType.Sound, "Sound", "d_AudioSource Icon");
// 事件标签页按钮
DrawTabButton(TabType.Event, "Event", "EventTrigger Icon");
EditorGUILayout.EndHorizontal();
switch (currentTab)
{
case TabType.Image:
DrawGraphicsTab();
break;
case TabType.Sound:
DrawAudioTab();
break;
case TabType.Event:
DrawEventTab();
break;
}
serializedObject.ApplyModifiedProperties();
}
private void DrawGraphicsTab()
{
2025-04-11 17:26:28 +08:00
EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
EditorGUILayout.PropertyField(m_Mode);
EditorGUI.EndDisabledGroup();
2025-07-25 19:53:34 +08:00
2025-07-25 20:11:27 +08:00
var interactable = GUILayoutHelper.DrawToggle(m_Interactable.boolValue, customSkin, "Interactable");
2025-07-25 19:53:34 +08:00
m_Interactable.boolValue = interactable;
2025-04-11 17:26:28 +08:00
GUILayout.Space(1);
DrawSelfTransition();
GUILayout.Space(5);
DrawChildTransitions();
2025-07-25 19:53:34 +08:00
GUILayout.Space(1);
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
DrawBasicSettings();
}
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
private void DrawEventTab()
{
if (m_Mode.enumValueIndex == (int)ButtonModeType.Toggle)
{
EditorGUILayout.Space();
EditorGUILayout.PropertyField(m_OnValueChanged);
}
else
{
EditorGUILayout.Space();
EditorGUILayout.PropertyField(m_OnClick);
}
}
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
private void DrawAudioTab()
{
2025-07-25 20:11:27 +08:00
GUILayoutHelper.DrawProperty(hoverAudioClip, customSkin, "Hover Sound", "Play", () =>
2025-07-25 19:53:34 +08:00
{
if (hoverAudioClip.objectReferenceValue != null)
{
PlayAudio((AudioClip)hoverAudioClip.objectReferenceValue);
}
});
2025-07-25 20:11:27 +08:00
GUILayoutHelper.DrawProperty(clickAudioClip, customSkin, "Click Sound", "Play", () =>
2025-07-25 19:53:34 +08:00
{
if (hoverAudioClip.objectReferenceValue != null)
{
PlayAudio((AudioClip)hoverAudioClip.objectReferenceValue);
}
});
}
private void DrawBasicSettings()
{
2025-04-11 17:26:28 +08:00
if (m_Mode.enumValueIndex != m_ButtonMode)
{
if (m_ButtonMode == (int)ButtonModeType.Normal)
{
ResetEventProperty(m_OnValueChanged);
m_UXGroup.objectReferenceValue = null;
}
else
{
2025-07-25 19:53:34 +08:00
ResetEventProperty(m_OnClick);
2025-04-11 17:26:28 +08:00
}
m_ButtonMode = m_Mode.enumValueIndex;
}
if (m_Mode.enumValueIndex == (int)ButtonModeType.Toggle)
{
2025-07-25 20:11:27 +08:00
GUILayoutHelper.DrawProperty(m_UXGroup, customSkin, "UXGroup");
2025-07-25 19:53:34 +08:00
2025-04-11 17:26:28 +08:00
UXGroup newGroup = (UXGroup)m_UXGroup.objectReferenceValue;
if (newGroup != group)
{
UXButton self = target as UXButton;
if (group != null)
{
group.UnregisterButton(self);
}
group = newGroup;
if (newGroup != null)
{
newGroup.RegisterButton(self);
}
}
}
2025-07-25 19:53:34 +08:00
}
private void PlayAudio(AudioClip clip)
{
if (clip != null)
2025-04-11 17:26:28 +08:00
{
2025-07-25 19:53:34 +08:00
ExtensionHelper.PreviewAudioClip(clip);
2025-04-11 17:26:28 +08:00
}
}
private void DrawChildTransitions()
{
2025-07-25 19:53:34 +08:00
m_ChildTransitionList.DoLayoutList();
}
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
private float CalculateTransitionDataHeight(SerializedProperty transitionData)
{
float height = 0;
SerializedProperty transition = transitionData.FindPropertyRelative("transition");
var currentTransition = GetTransition(transition);
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
height += EditorGUIUtility.singleLineHeight * 1.5f;
height += EditorGUIUtility.singleLineHeight;
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
SerializedProperty targetGraphic = transitionData.FindPropertyRelative("targetGraphic");
var graphic = targetGraphic.objectReferenceValue as Graphic;
2025-07-29 11:05:15 +08:00
var animator = graphic != null ? graphic.GetComponent<Animator>() : null;
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
switch (currentTransition)
2025-04-11 17:26:28 +08:00
{
2025-07-25 19:53:34 +08:00
case Selectable.Transition.ColorTint:
if (graphic == null) height += EditorGUIUtility.singleLineHeight;
break;
case Selectable.Transition.SpriteSwap:
if (!(graphic is Image)) height += EditorGUIUtility.singleLineHeight;
break;
case Selectable.Transition.Animation:
2025-07-29 11:05:15 +08:00
if (animator == null) height += EditorGUIUtility.singleLineHeight;
2025-07-25 19:53:34 +08:00
break;
}
2025-04-11 17:26:28 +08:00
2025-07-25 19:53:34 +08:00
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
height += EditorGUI.GetPropertyHeight(transitionData.FindPropertyRelative("colors"));
break;
case Selectable.Transition.SpriteSwap:
height += EditorGUI.GetPropertyHeight(transitionData.FindPropertyRelative("spriteState"));
break;
case Selectable.Transition.Animation:
height += EditorGUI.GetPropertyHeight(transitionData.FindPropertyRelative("animationTriggers"));
break;
2025-04-11 17:26:28 +08:00
}
2025-07-25 19:53:34 +08:00
return height;
2025-04-11 17:26:28 +08:00
}
2025-07-25 19:53:34 +08:00
private void DrawTransitionData(Rect position, SerializedProperty transitionData, bool isChild = false)
2025-04-11 17:26:28 +08:00
{
SerializedProperty targetGraphic = transitionData.FindPropertyRelative("targetGraphic");
SerializedProperty transition = transitionData.FindPropertyRelative("transition");
SerializedProperty colorBlock = transitionData.FindPropertyRelative("colors");
SerializedProperty spriteState = transitionData.FindPropertyRelative("spriteState");
2025-07-25 13:29:20 +08:00
SerializedProperty animationTriggers = transitionData.FindPropertyRelative("animationTriggers");
2025-07-25 19:53:34 +08:00
2025-04-11 17:26:28 +08:00
EditorGUI.indentLevel++;
2025-07-25 19:53:34 +08:00
float lineHeight = EditorGUIUtility.singleLineHeight;
float spacing = 2f;
float y = position.y;
2025-04-11 17:26:28 +08:00
var currentTransition = GetTransition(transition);
2025-07-29 15:19:40 +08:00
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
case Selectable.Transition.SpriteSwap:
Rect targetRect = new Rect(position.x, y, position.width, lineHeight);
EditorGUI.PropertyField(targetRect, targetGraphic);
y += lineHeight + spacing;
break;
}
2025-07-25 19:53:34 +08:00
Rect transitionRect = new Rect(position.x, y, position.width, lineHeight);
EditorGUI.PropertyField(transitionRect, transition);
y += lineHeight + spacing;
2025-07-25 13:29:20 +08:00
2025-04-11 17:26:28 +08:00
var graphic = targetGraphic.objectReferenceValue as Graphic;
2025-07-29 11:05:15 +08:00
var animator = graphic != null ? graphic.GetComponent<Animator>() : null;
2025-07-25 13:29:20 +08:00
2025-04-11 17:26:28 +08:00
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
if (graphic == null)
2025-07-25 19:53:34 +08:00
{
Rect warningRect = new Rect(position.x, y, position.width, lineHeight);
EditorGUI.HelpBox(warningRect, "需要Graphic组件来使用颜色过渡", MessageType.Warning);
y += lineHeight + spacing;
}
2025-04-11 17:26:28 +08:00
break;
case Selectable.Transition.SpriteSwap:
if (!(graphic is Image))
2025-07-25 19:53:34 +08:00
{
Rect warningRect = new Rect(position.x, y, position.width, lineHeight);
EditorGUI.HelpBox(warningRect, "需要Image组件来使用精灵切换", MessageType.Warning);
y += lineHeight + spacing;
}
2025-04-11 17:26:28 +08:00
break;
2025-07-25 13:29:20 +08:00
case Selectable.Transition.Animation:
2025-07-29 11:05:15 +08:00
if (animator == null)
2025-07-25 19:53:34 +08:00
{
Rect warningRect = new Rect(position.x, y, position.width, lineHeight);
EditorGUI.HelpBox(warningRect, "需要Animation || Animator组件来使用动画切换", MessageType.Warning);
y += lineHeight + spacing;
}
2025-07-25 13:29:20 +08:00
break;
2025-04-11 17:26:28 +08:00
}
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
CheckAndSetColorDefaults(colorBlock, targetGraphic);
2025-07-25 19:53:34 +08:00
Rect colorRect = new Rect(position.x, y, position.width, EditorGUI.GetPropertyHeight(colorBlock));
EditorGUI.PropertyField(colorRect, colorBlock);
2025-04-11 17:26:28 +08:00
break;
case Selectable.Transition.SpriteSwap:
2025-07-29 15:38:36 +08:00
CheckAndSetColorDefaults(colorBlock, targetGraphic);
2025-07-25 19:53:34 +08:00
Rect spriteRect = new Rect(position.x, y, position.width, EditorGUI.GetPropertyHeight(spriteState));
EditorGUI.PropertyField(spriteRect, spriteState);
break;
case Selectable.Transition.Animation:
Rect animRect = new Rect(position.x, y, position.width, EditorGUI.GetPropertyHeight(animationTriggers));
EditorGUI.PropertyField(animRect, animationTriggers);
break;
}
2025-07-29 15:38:36 +08:00
if (graphic != null && currentTransition != (Selectable.Transition)transition.enumValueIndex &&
2025-07-29 15:43:47 +08:00
((Selectable.Transition)transition.enumValueIndex == Selectable.Transition.Animation ||
(Selectable.Transition)transition.enumValueIndex == Selectable.Transition.None))
2025-07-29 15:38:36 +08:00
{
graphic.canvasRenderer.SetColor(Color.white);
}
2025-07-25 19:53:34 +08:00
}
private void DrawSelfTransition()
{
GUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.LabelField("Main Transition", EditorStyles.boldLabel);
SerializedProperty targetGraphic = m_TransitionData.FindPropertyRelative("targetGraphic");
var graphic = targetGraphic.objectReferenceValue as Graphic;
if (graphic == null)
{
graphic = (target as UXButton).GetComponent<Graphic>();
targetGraphic.objectReferenceValue = graphic;
}
2025-07-29 15:19:40 +08:00
SerializedProperty transition = m_TransitionData.FindPropertyRelative("transition");
var currentTransition = GetTransition(transition);
2025-07-25 19:53:34 +08:00
2025-07-29 15:19:40 +08:00
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
case Selectable.Transition.SpriteSwap:
EditorGUILayout.PropertyField(targetGraphic);
break;
}
2025-07-25 19:53:34 +08:00
2025-07-29 15:38:36 +08:00
2025-07-25 19:53:34 +08:00
EditorGUILayout.PropertyField(transition);
2025-07-29 11:05:15 +08:00
var animator = graphic != null ? graphic.GetComponent<Animator>() : null;
2025-07-25 19:53:34 +08:00
switch (currentTransition)
{
case Selectable.Transition.ColorTint:
if (graphic == null)
EditorGUILayout.HelpBox("需要Graphic组件来使用颜色过渡", MessageType.Warning);
break;
case Selectable.Transition.SpriteSwap:
if (!(graphic is Image))
EditorGUILayout.HelpBox("需要Image组件来使用精灵切换", MessageType.Warning);
2025-04-11 17:26:28 +08:00
break;
2025-07-25 13:29:20 +08:00
case Selectable.Transition.Animation:
2025-07-29 11:05:15 +08:00
if (animator == null)
2025-07-25 19:53:34 +08:00
EditorGUILayout.HelpBox("需要Animation || Animator组件来使用动画切换", MessageType.Warning);
2025-07-25 13:29:20 +08:00
break;
}
2025-07-25 19:53:34 +08:00
switch (currentTransition)
2025-07-25 13:29:20 +08:00
{
2025-07-25 19:53:34 +08:00
case Selectable.Transition.ColorTint:
CheckAndSetColorDefaults(m_TransitionData.FindPropertyRelative("colors"), targetGraphic);
EditorGUILayout.PropertyField(m_TransitionData.FindPropertyRelative("colors"));
break;
case Selectable.Transition.SpriteSwap:
EditorGUILayout.PropertyField(m_TransitionData.FindPropertyRelative("spriteState"));
break;
case Selectable.Transition.Animation:
EditorGUILayout.PropertyField(m_TransitionData.FindPropertyRelative("animationTriggers"));
2025-07-29 11:05:15 +08:00
if (animator == null || animator.runtimeAnimatorController == null)
{
if (GUILayout.Button("Auto Generate Animation"))
{
2025-07-29 15:57:51 +08:00
var animationTrigger = m_TransitionData.FindPropertyRelative("animationTriggers");
var controller = GenerateSelectableAnimatorContoller(animationTrigger, target as UXButton);
2025-07-29 11:05:15 +08:00
if (controller != null)
{
if (animator == null)
animator = (target as Selectable).gameObject.AddComponent<Animator>();
UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
}
}
}
2025-07-25 19:53:34 +08:00
break;
2025-04-11 17:26:28 +08:00
}
2025-07-29 15:38:36 +08:00
if (graphic != null && currentTransition != (Selectable.Transition)transition.enumValueIndex &&
2025-07-29 15:43:47 +08:00
((Selectable.Transition)transition.enumValueIndex == Selectable.Transition.Animation ||
(Selectable.Transition)transition.enumValueIndex == Selectable.Transition.None))
2025-07-29 15:38:36 +08:00
{
graphic.canvasRenderer.SetColor(Color.white);
}
2025-04-11 17:26:28 +08:00
EditorGUI.indentLevel--;
2025-07-25 19:53:34 +08:00
EditorGUILayout.Space();
GUILayout.EndVertical();
2025-04-11 17:26:28 +08:00
}
2025-07-29 15:57:51 +08:00
private static UnityEditor.Animations.AnimatorController GenerateSelectableAnimatorContoller(SerializedProperty property, UXButton target)
2025-07-29 11:05:15 +08:00
{
if (target == null)
return null;
var path = GetSaveControllerPath(target);
if (string.IsNullOrEmpty(path))
return null;
2025-07-29 15:57:51 +08:00
SerializedProperty normalTrigger = property.FindPropertyRelative("normalTrigger");
SerializedProperty highlightedTrigger = property.FindPropertyRelative("highlightedTrigger");
SerializedProperty pressedTrigger = property.FindPropertyRelative("pressedTrigger");
SerializedProperty selectedTrigger = property.FindPropertyRelative("selectedTrigger");
SerializedProperty disabledTrigger = property.FindPropertyRelative("disabledTrigger");
var normalName = string.IsNullOrEmpty(normalTrigger.stringValue) ? "Normal" : normalTrigger.stringValue;
var highlightedName = string.IsNullOrEmpty(highlightedTrigger.stringValue) ? "Highlighted" : highlightedTrigger.stringValue;
var pressedName = string.IsNullOrEmpty(pressedTrigger.stringValue) ? "Pressed" : pressedTrigger.stringValue;
var selectedName = string.IsNullOrEmpty(selectedTrigger.stringValue) ? "Selected" : selectedTrigger.stringValue;
var disabledName = string.IsNullOrEmpty(disabledTrigger.stringValue) ? "Disabled" : disabledTrigger.stringValue;
2025-07-29 11:05:15 +08:00
var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(path);
GenerateTriggerableTransition(normalName, controller);
GenerateTriggerableTransition(highlightedName, controller);
GenerateTriggerableTransition(pressedName, controller);
GenerateTriggerableTransition(selectedName, controller);
GenerateTriggerableTransition(disabledName, controller);
AssetDatabase.ImportAsset(path);
return controller;
}
private static AnimationClip GenerateTriggerableTransition(string name, UnityEditor.Animations.AnimatorController controller)
{
// Create the clip
var clip = UnityEditor.Animations.AnimatorController.AllocateAnimatorClip(name);
AssetDatabase.AddObjectToAsset(clip, controller);
// Create a state in the animatior controller for this clip
var state = controller.AddMotion(clip);
// Add a transition property
controller.AddParameter(name, AnimatorControllerParameterType.Trigger);
// Add an any state transition
var stateMachine = controller.layers[0].stateMachine;
var transition = stateMachine.AddAnyStateTransition(state);
transition.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 0, name);
return clip;
}
2025-07-29 15:57:51 +08:00
private static string GetSaveControllerPath(UXButton target)
2025-07-29 11:05:15 +08:00
{
var defaultName = target.gameObject.name;
var message = string.Format("Create a new animator for the game object '{0}':", defaultName);
return EditorUtility.SaveFilePanelInProject("New Animation Contoller", defaultName, "controller", message);
}
2025-04-11 17:26:28 +08:00
void CheckAndSetColorDefaults(SerializedProperty colorBlock, SerializedProperty targetGraphic)
{
bool isDirty = false;
string[] colorProps = new string[] { "m_NormalColor", "m_HighlightedColor", "m_PressedColor", "m_SelectedColor", "m_DisabledColor" };
foreach (var propName in colorProps)
{
SerializedProperty prop = colorBlock.FindPropertyRelative(propName);
Color color = prop.colorValue;
if (color.r == 0 && color.g == 0 && color.b == 0 && color.a == 0)
{
isDirty = true;
if (prop.name == "m_PressedColor")
{
prop.colorValue = new Color(0.7843137f, 0.7843137f, 0.7843137f, 1.0f);
}
else if (prop.name == "m_DisabledColor")
{
prop.colorValue = new Color(0.7843137f, 0.7843137f, 0.7843137f, 0.5f);
}
else
{
prop.colorValue = Color.white;
}
}
}
SerializedProperty fadeDuration = colorBlock.FindPropertyRelative("m_FadeDuration");
SerializedProperty m_ColorMultiplier = colorBlock.FindPropertyRelative("m_ColorMultiplier");
if (isDirty)
{
m_ColorMultiplier.floatValue = 1f;
fadeDuration.floatValue = 0.1f;
}
var graphic = targetGraphic.objectReferenceValue as Graphic;
if (graphic != null)
{
2025-07-29 15:38:36 +08:00
if (!EditorApplication.isPlaying && (Selectable.Transition)m_SelectionState.enumValueIndex != Selectable.Transition.Animation)
2025-07-25 13:29:20 +08:00
{
Color color = colorBlock.FindPropertyRelative("m_NormalColor").colorValue;
graphic.canvasRenderer.SetColor(color);
}
2025-04-11 17:26:28 +08:00
}
}
static Selectable.Transition GetTransition(SerializedProperty transition)
{
return (Selectable.Transition)transition.enumValueIndex;
}
}