From 52a8c7358a88e15e7b20cb1300d9457168d1b049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Thu, 17 Apr 2025 16:03:39 +0800 Subject: [PATCH] modify --- Editor/UIExtension/UIButtonSuperEditor.cs | 29 ---- Runtime/RecyclerView/ViewHolder/ViewHolder.cs | 8 +- Runtime/UGUIExtension/Button/UIButtonSuper.cs | 161 +----------------- Runtime/UGUIExtension/UX/IButton.cs | 9 + Runtime/UGUIExtension/UX/IButton.cs.meta | 3 + Runtime/UGUIExtension/UX/UXButton.cs | 40 +++-- 6 files changed, 44 insertions(+), 206 deletions(-) create mode 100644 Runtime/UGUIExtension/UX/IButton.cs create mode 100644 Runtime/UGUIExtension/UX/IButton.cs.meta diff --git a/Editor/UIExtension/UIButtonSuperEditor.cs b/Editor/UIExtension/UIButtonSuperEditor.cs index 5508a34..76cd3c2 100644 --- a/Editor/UIExtension/UIButtonSuperEditor.cs +++ b/Editor/UIExtension/UIButtonSuperEditor.cs @@ -9,30 +9,11 @@ public class UIButtonSuperEditor : ButtonEditor { private SerializedProperty m_ButtonUISounds; - private SerializedProperty m_CanClick; - private SerializedProperty m_CanDoubleClick; - private SerializedProperty m_DoubleClickIntervalTime; - private SerializedProperty onDoubleClick; - - private SerializedProperty m_CanLongPress; - private SerializedProperty m_ResponseOnceByPress; - private SerializedProperty m_LongPressDurationTime; - private SerializedProperty onPress; protected override void OnEnable() { base.OnEnable(); - m_ButtonUISounds = serializedObject.FindProperty("m_ButtonUISounds"); - m_CanClick = serializedObject.FindProperty("m_CanClick"); - m_CanDoubleClick = serializedObject.FindProperty("m_CanDoubleClick"); - m_DoubleClickIntervalTime = serializedObject.FindProperty("m_DoubleClickIntervalTime"); - onDoubleClick = serializedObject.FindProperty("onDoubleClick"); - - m_CanLongPress = serializedObject.FindProperty("m_CanLongPress"); - m_ResponseOnceByPress = serializedObject.FindProperty("m_ResponseOnceByPress"); - m_LongPressDurationTime = serializedObject.FindProperty("m_LongPressDurationTime"); - onPress = serializedObject.FindProperty("onPress"); } public override void OnInspectorGUI() @@ -40,16 +21,6 @@ base.OnInspectorGUI(); serializedObject.Update(); EditorGUILayout.PropertyField(m_ButtonUISounds); //显示我们创建的属性 - EditorGUILayout.PropertyField(m_CanClick); //显示我们创建的属性 - EditorGUILayout.Space(); //空行 - EditorGUILayout.PropertyField(m_CanDoubleClick); //显示我们创建的属性 - EditorGUILayout.PropertyField(m_DoubleClickIntervalTime); //显示我们创建的属性 - EditorGUILayout.PropertyField(onDoubleClick); //显示我们创建的属性 - EditorGUILayout.Space(); //空行 - EditorGUILayout.PropertyField(m_CanLongPress); //显示我们创建的属性 - EditorGUILayout.PropertyField(m_ResponseOnceByPress); //显示我们创建的属性 - EditorGUILayout.PropertyField(m_LongPressDurationTime); //显示我们创建的属性 - EditorGUILayout.PropertyField(onPress); //显示我们创建的属性 serializedObject.ApplyModifiedProperties(); } } diff --git a/Runtime/RecyclerView/ViewHolder/ViewHolder.cs b/Runtime/RecyclerView/ViewHolder/ViewHolder.cs index dcfb9ee..7ac6062 100644 --- a/Runtime/RecyclerView/ViewHolder/ViewHolder.cs +++ b/Runtime/RecyclerView/ViewHolder/ViewHolder.cs @@ -1,4 +1,5 @@ using System; +using AlicizaX.UI.Extension; using UnityEngine; using UnityEngine.UI; @@ -37,16 +38,11 @@ namespace AlicizaX.UI.RecyclerView public virtual void BindItemClick(T data, Action action) { - if (TryGetComponent(out Button button)) + if (TryGetComponent(out IButton button)) { button.onClick.RemoveAllListeners(); button.onClick.AddListener(() => action?.Invoke(data)); } - else if (TryGetComponent(out UXButton uxButton)) - { - uxButton.onClick.RemoveAllListeners(); - uxButton.onClick.AddListener(() => action?.Invoke(data)); - } } protected internal void BindChoiceState(bool state) diff --git a/Runtime/UGUIExtension/Button/UIButtonSuper.cs b/Runtime/UGUIExtension/Button/UIButtonSuper.cs index 68eca6b..645f3fb 100644 --- a/Runtime/UGUIExtension/Button/UIButtonSuper.cs +++ b/Runtime/UGUIExtension/Button/UIButtonSuper.cs @@ -13,8 +13,7 @@ namespace AlicizaX.UI.Extension Up, Click, Enter, - Exit, - Drag + Exit } [Serializable] @@ -24,131 +23,10 @@ namespace AlicizaX.UI.Extension public string ButtonUISoundName = ""; } - public delegate void ButtonBeginDragCallback(PointerEventData eventData); - - public delegate void ButtonDragCallback(PointerEventData eventData); - - public delegate void ButtonEndDragCallback(PointerEventData eventData); - - public class UIButtonSuper : Button, IBeginDragHandler, IDragHandler, IEndDragHandler + public class UIButtonSuper : Button,IButton { public List m_ButtonUISounds = new List(); - [Tooltip("是否可以点击")] public bool m_CanClick = true; - [Tooltip("是否可以双击")] public bool m_CanDoubleClick = false; - [Tooltip("双击间隔时长")] public float m_DoubleClickIntervalTime = 0.1f; - [Tooltip("双击事件")] public ButtonClickedEvent onDoubleClick; - [Tooltip("是否可以长按")] public bool m_CanLongPress = false; - [Tooltip("长按是否只响应一次")] public bool m_ResponseOnceByPress = false; - [Tooltip("长按满足间隔")] public float m_LongPressDurationTime = 1; - - [Tooltip("长按事件")] public ButtonClickedEvent onPress; - - //public ButtonClickedEvent onClick; - public ButtonBeginDragCallback onBeginDrag; - public ButtonDragCallback onDrag; - public ButtonEndDragCallback onEndDrag; - - private bool isDown = false; - private bool isPress = false; - private bool isDownExit = false; - private float downTime = 0; - - private int fingerId = int.MinValue; - - public bool IsDraging - { - get { return fingerId != int.MinValue; } - } //摇杆拖拽状态 - - public int FingerId - { - get { return fingerId; } - } - - private float clickIntervalTime = 0; - private int clickTimes = 0; - - void Update() - { - if (isDown) - { - if (!m_CanLongPress) - { - return; - } - - if (m_ResponseOnceByPress && isPress) - { - return; - } - - downTime += Time.deltaTime; - if (downTime > m_LongPressDurationTime) - { - isPress = true; - onPress.Invoke(); - } - } - - if (clickTimes >= 1) - { - if (!m_CanLongPress && !m_CanDoubleClick && m_CanClick) - { - onClick.Invoke(); - clickTimes = 0; - } - else - { - clickIntervalTime += Time.deltaTime; - if (clickIntervalTime >= m_DoubleClickIntervalTime) - { - if (clickTimes >= 2) - { - if (m_CanDoubleClick) - { - onDoubleClick.Invoke(); - } - } - else - { - if (m_CanClick) - { - onClick.Invoke(); - } - } - - clickTimes = 0; - clickIntervalTime = 0; - } - } - } - } - - /// - /// 是否按钮按下 - /// - public bool IsDown - { - get { return isDown; } - } - - /// - /// 是否按钮长按 - /// - public bool IsPress - { - get { return isPress; } - } - - /// - /// 是否按钮按下后离开按钮位置 - /// - public bool IsDownExit - { - get { return isDownExit; } - } - public ButtonSoundCell GetButtonSound(ButtonSoundType buttonSoundType) { foreach (var buttonSound in m_ButtonUISounds) @@ -182,59 +60,26 @@ namespace AlicizaX.UI.Extension public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); - if (eventData.pointerId < -1 || IsDraging) return; //适配 Touch:只响应一个Touch;适配鼠标:只响应左键 - fingerId = eventData.pointerId; - isDown = true; - isDownExit = false; - downTime = 0; PlayButtonSound(ButtonSoundType.Down); } public override void OnPointerUp(PointerEventData eventData) { base.OnPointerUp(eventData); - if (fingerId != eventData.pointerId) return; //正确的手指抬起时才会; - fingerId = int.MinValue; - isDown = false; - isDownExit = true; PlayButtonSound(ButtonSoundType.Up); } public override void OnPointerExit(PointerEventData eventData) { base.OnPointerExit(eventData); - if (fingerId != eventData.pointerId) return; //正确的手指抬起时才会; - isPress = false; - isDownExit = true; PlayButtonSound(ButtonSoundType.Exit); } public override void OnPointerClick(PointerEventData eventData) { - if (!isPress) - { - clickTimes += 1; - } - else - isPress = false; - + base.OnPointerClick(eventData); PlayButtonSound(ButtonSoundType.Click); } - public void OnBeginDrag(PointerEventData eventData) - { - onBeginDrag?.Invoke(eventData); - } - - public void OnDrag(PointerEventData eventData) - { - PlayButtonSound(ButtonSoundType.Drag); - onDrag?.Invoke(eventData); - } - - public void OnEndDrag(PointerEventData eventData) - { - onEndDrag?.Invoke(eventData); - } } } diff --git a/Runtime/UGUIExtension/UX/IButton.cs b/Runtime/UGUIExtension/UX/IButton.cs new file mode 100644 index 0000000..1c5347b --- /dev/null +++ b/Runtime/UGUIExtension/UX/IButton.cs @@ -0,0 +1,9 @@ +using UnityEngine.UI; + +namespace AlicizaX.UI.Extension +{ + public interface IButton + { + Button.ButtonClickedEvent onClick { get; set; } + } +} diff --git a/Runtime/UGUIExtension/UX/IButton.cs.meta b/Runtime/UGUIExtension/UX/IButton.cs.meta new file mode 100644 index 0000000..a541973 --- /dev/null +++ b/Runtime/UGUIExtension/UX/IButton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 06a9d41d8ef2435395bf49fd7463aba6 +timeCreated: 1744867171 \ No newline at end of file diff --git a/Runtime/UGUIExtension/UX/UXButton.cs b/Runtime/UGUIExtension/UX/UXButton.cs index 2e9d061..81893d0 100644 --- a/Runtime/UGUIExtension/UX/UXButton.cs +++ b/Runtime/UGUIExtension/UX/UXButton.cs @@ -63,14 +63,18 @@ internal enum SelectionState [DisallowMultipleComponent] [RequireComponent(typeof(Graphic))] -public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler +public class UXButton : UIBehaviour, IButton, + IPointerDownHandler, + IPointerUpHandler, + IPointerEnterHandler, + IPointerExitHandler, + IPointerClickHandler { [SerializeField] private bool m_Interactable = true; - [SerializeField] private ButtonModeType m_Mode; - [SerializeField] private UnityEvent m_OnClick = new UnityEvent(); + [SerializeField] private Button.ButtonClickedEvent m_OnClick = new Button.ButtonClickedEvent(); [SerializeField] private TransitionData m_TransitionData = new TransitionData(); @@ -80,7 +84,7 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo [SerializeField] private List m_ButtonUISounds = new List(); private SelectionState m_SelectionState = SelectionState.Normal; - private bool m_ExistUI; + private bool m_DownAndExistUI; private bool m_IsDown; private bool m_IsTogSelected; @@ -97,8 +101,7 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo } } - - public UnityEvent onClick + public Button.ButtonClickedEvent onClick { get { return m_OnClick; } set { m_OnClick = value; } @@ -125,6 +128,7 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo void IPointerDownHandler.OnPointerDown(PointerEventData eventData) { if (!m_Interactable) return; + if (eventData.button != PointerEventData.InputButton.Left) return; m_IsDown = true; m_SelectionState = SelectionState.Pressed; UpdateVisualState(m_SelectionState, false); @@ -134,11 +138,12 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo void IPointerUpHandler.OnPointerUp(PointerEventData eventData) { if (!m_Interactable) return; + m_IsDown = false; if (!m_IsTogSelected) { - m_SelectionState = m_ExistUI ? SelectionState.Normal : SelectionState.Highlighted; + m_SelectionState = m_DownAndExistUI ? SelectionState.Normal : SelectionState.Highlighted; UpdateVisualState(m_SelectionState, false); } else @@ -147,10 +152,8 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo UpdateVisualState(m_SelectionState, false); } - if (!m_ExistUI) - { - ProcessClick(); - } + if (eventData.button != PointerEventData.InputButton.Left) + return; PlayButtonSound(ButtonSoundType.Up); } @@ -158,8 +161,10 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) { if (!m_Interactable || CantTouch()) return; + m_SelectionState = SelectionState.Highlighted; - m_ExistUI = false; + m_DownAndExistUI = false; + if (m_IsDown) return; UpdateVisualState(m_SelectionState, false); PlayButtonSound(ButtonSoundType.Enter); } @@ -169,7 +174,7 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo if (!m_Interactable) return; if (m_IsDown) { - m_ExistUI = true; + m_DownAndExistUI = true; return; } @@ -202,6 +207,7 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo { if (m_Mode == ButtonModeType.Normal) { + UISystemProfilerApi.AddMarker("Button.onClick", this); onClick?.Invoke(); } else @@ -318,4 +324,12 @@ public class UXButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPo return null; } + + public void OnPointerClick(PointerEventData eventData) + { + if (eventData.button != PointerEventData.InputButton.Left) + return; + PlayButtonSound(ButtonSoundType.Click); + ProcessClick(); + } }