diff --git a/Editor/UX/UXButtonEditor.cs b/Editor/UX/UXButtonEditor.cs index 46c519c..d02d266 100644 --- a/Editor/UX/UXButtonEditor.cs +++ b/Editor/UX/UXButtonEditor.cs @@ -30,9 +30,13 @@ public class UXButtonEditor : Editor private Dictionary m_ChildTransitionFoldouts = new Dictionary(); private UXGroup group; private int m_ButtonMode; + private SerializedProperty m_SelectionState; + private UXButton mTarget; + private void OnEnable() { + mTarget = target as UXButton; m_Interactable = serializedObject.FindProperty("m_Interactable"); m_UXGroup = serializedObject.FindProperty("m_UXGroup"); m_Mode = serializedObject.FindProperty("m_Mode"); @@ -41,6 +45,7 @@ public class UXButtonEditor : Editor m_TransitionData = serializedObject.FindProperty("m_TransitionData"); m_ChildTransitions = serializedObject.FindProperty("m_ChildTransitions"); m_ButtonUISounds = serializedObject.FindProperty("m_ButtonUISounds"); + m_SelectionState = serializedObject.FindProperty("m_SelectionState"); m_ChildTransitionFoldouts.Clear(); group = (UXGroup)m_UXGroup.objectReferenceValue; m_ButtonMode = m_Mode.enumValueIndex; @@ -53,6 +58,7 @@ public class UXButtonEditor : Editor calls.arraySize = 0; property.serializedObject.ApplyModifiedProperties(); } + public override void OnInspectorGUI() { serializedObject.Update(); @@ -81,7 +87,7 @@ public class UXButtonEditor : Editor } else { - ResetEventProperty((m_OnClick)); + ResetEventProperty((m_OnClick)); } m_ButtonMode = m_Mode.enumValueIndex; @@ -194,7 +200,7 @@ public class UXButtonEditor : Editor SerializedProperty transition = transitionData.FindPropertyRelative("transition"); SerializedProperty colorBlock = transitionData.FindPropertyRelative("colors"); SerializedProperty spriteState = transitionData.FindPropertyRelative("spriteState"); - + SerializedProperty animationTriggers = transitionData.FindPropertyRelative("animationTriggers"); EditorGUI.indentLevel++; // 绘制目标图形 @@ -206,8 +212,12 @@ public class UXButtonEditor : Editor // 绘制transition类型 EditorGUILayout.PropertyField(transition); + // 显示警告信息 var graphic = targetGraphic.objectReferenceValue as Graphic; + var animation = graphic.GetComponent(); + + switch (currentTransition) { case Selectable.Transition.ColorTint: @@ -219,6 +229,10 @@ public class UXButtonEditor : Editor if (!(graphic is Image)) EditorGUILayout.HelpBox("需要Image组件来使用精灵切换", MessageType.Warning); break; + case Selectable.Transition.Animation: + if (animation == null) + EditorGUILayout.HelpBox("需要Animation || Animator组件来使用动画切换", MessageType.Warning); + break; } // 绘制对应类型的属性 @@ -232,6 +246,15 @@ public class UXButtonEditor : Editor case Selectable.Transition.SpriteSwap: EditorGUILayout.PropertyField(spriteState); break; + case Selectable.Transition.Animation: + EditorGUILayout.PropertyField(animationTriggers); + break; + } + + if ((int)currentTransition != transition.enumValueIndex) + { + graphic.canvasRenderer.SetColor(graphic.color); + graphic.color = graphic.color; } EditorGUI.indentLevel--; @@ -279,6 +302,11 @@ public class UXButtonEditor : Editor Color color = colorBlock.FindPropertyRelative("m_NormalColor").colorValue; graphic.canvasRenderer.SetColor(color); } + else if (m_SelectionState.enumValueIndex == 0) + { + Color color = colorBlock.FindPropertyRelative("m_NormalColor").colorValue; + graphic.canvasRenderer.SetColor(color); + } } } diff --git a/Runtime/UGUIExtension/UX/UXButton.cs b/Runtime/UGUIExtension/UX/UXButton.cs index 834d045..28fbf9b 100644 --- a/Runtime/UGUIExtension/UX/UXButton.cs +++ b/Runtime/UGUIExtension/UX/UXButton.cs @@ -40,6 +40,7 @@ public class TransitionData public Selectable.Transition transition = Selectable.Transition.ColorTint; public ColorBlock colors; public SpriteState spriteState; + public AnimationTriggers animationTriggers = new AnimationTriggers(); } [Serializable] @@ -82,12 +83,21 @@ public class UXButton : UIBehaviour, IButton, [SerializeField] private UXGroup m_UXGroup; [SerializeField] private List m_ButtonUISounds = new List(); - - private SelectionState m_SelectionState = SelectionState.Normal; + [SerializeField] private SelectionState m_SelectionState = SelectionState.Normal; private bool m_DownAndExistUI; private bool m_IsDown; private bool m_IsTogSelected; + private Animator _animator; + + internal Animator animator + { + get + { + _animator = _animator ?? GetComponent(); + return _animator; + } + } public bool IsSelected { @@ -122,7 +132,8 @@ public class UXButton : UIBehaviour, IButton, { onValueChanged?.Invoke(IsSelected); } - UpdateVisualState(m_SelectionState,true); + + UpdateVisualState(m_SelectionState, true); } @@ -239,31 +250,38 @@ public class UXButton : UIBehaviour, IButton, Color tintColor; Sprite transitionSprite; + string triggerName; switch (state) { case SelectionState.Normal: tintColor = transition.colors.normalColor; transitionSprite = null; + triggerName = transition.animationTriggers.normalTrigger; break; case SelectionState.Highlighted: tintColor = transition.colors.highlightedColor; transitionSprite = transition.spriteState.highlightedSprite; + triggerName = transition.animationTriggers.highlightedTrigger; break; case SelectionState.Pressed: tintColor = transition.colors.pressedColor; transitionSprite = transition.spriteState.pressedSprite; + triggerName = transition.animationTriggers.pressedTrigger; break; case SelectionState.Selected: tintColor = transition.colors.selectedColor; transitionSprite = transition.spriteState.selectedSprite; + triggerName = transition.animationTriggers.selectedTrigger; break; case SelectionState.Disabled: tintColor = transition.colors.disabledColor; transitionSprite = transition.spriteState.disabledSprite; + triggerName = transition.animationTriggers.disabledTrigger; break; default: tintColor = Color.black; transitionSprite = null; + triggerName = string.Empty; break; } @@ -275,6 +293,9 @@ public class UXButton : UIBehaviour, IButton, case Selectable.Transition.SpriteSwap: DoSpriteSwap(transition, transitionSprite); break; + case Selectable.Transition.Animation: + TriggerAnimation(transition.animationTriggers, triggerName); + break; } } @@ -302,6 +323,20 @@ public class UXButton : UIBehaviour, IButton, } } + void TriggerAnimation(AnimationTriggers animationTriggers, string triggername) + { + if (animator == null || !animator.isActiveAndEnabled || !animator.hasBoundPlayables || string.IsNullOrEmpty(triggername)) + return; + + animator.ResetTrigger(animationTriggers.normalTrigger); + animator.ResetTrigger(animationTriggers.highlightedTrigger); + animator.ResetTrigger(animationTriggers.pressedTrigger); + animator.ResetTrigger(animationTriggers.selectedTrigger); + animator.ResetTrigger(animationTriggers.disabledTrigger); + + animator.SetTrigger(triggername); + } + protected void PlayButtonSound(ButtonSoundType buttonSoundType) { ButtonSoundCell buttonSound = GetButtonSound(buttonSoundType);