瀏覽代碼

增加按钮上操作:单击和双击以及长按判断。。。

xyh1985 1 年之前
父節點
當前提交
e7832fc5ba

+ 0 - 2
Unity/Assets/Scripts/Codes/Hotfix/Client/Login/LoginHelper.cs

@@ -15,8 +15,6 @@ namespace ET.Client
                 clientScene.RemoveComponent<NetClientComponent>();
                 clientScene.RemoveComponent<SessionComponent>();
 
-                GameUtil.Instance.SetSceneComponent(clientScene);
-
                 // 获取路由跟realmDispatcher地址
                 RouterAddressComponent routerAddressComponent = clientScene.AddComponent<RouterAddressComponent, string, int>(ip, port);
                 await routerAddressComponent.Init();

+ 1 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UILogin/UILoginComponentSystem.cs

@@ -50,6 +50,7 @@ namespace ET.Client
             //每次登陆 重置声音
             GameSetting.Instance.SetFloat(MusicSets.Mute_BG, 1);
             GameSetting.Instance.SetFloat(MusicSets.Mute_Music, 1);
+            GameUtil.Instance.SetSceneComponent(self.DomainScene());
         }
 
 		public static async void OnLogin(this UILoginComponent self)

+ 2 - 1
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UIMain/UIMainComponentSystem.cs

@@ -25,7 +25,8 @@ namespace ET.Client
                 self.vipVaule = rc.Get<GameObject>("vipVaule");
                 self.expVaule = rc.Get<GameObject>("expVaule");
 
-                self.createRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateRoom(); });
+                self.createRoomBtn.OnClick(() => { self.OnCreateRoom(); });
+                //self.createRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnCreateRoom(); });
                 self.joinRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnJoinRoom(); });
                 self.clubRoomBtn.GetComponent<Button>().onClick.AddListener(() => { self.OnClub(); });
                 self.playerIcon.GetComponent<Button>().onClick.AddListener(() => { self.OnPlayer(); });

+ 8 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 1d5b6630127feea439ac332f79fdf08a
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 116 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/BridgeBtn.cs

@@ -0,0 +1,116 @@
+//单击按钮
+using System.Diagnostics;
+using UnityEngine.EventSystems;
+
+public class ClickButton : ClickEvent.ClickListener
+{
+    protected override void OnPointerDown(PointerEventData eventData)
+    {
+        isRuning = true;
+    }
+
+    protected override void OnPointerUp(PointerEventData eventData)
+    {
+        if (isRuning)
+        {
+            Reset();
+            callBack?.Invoke(true);
+        }
+    }
+
+    protected override void Reset()
+    {
+        isRuning = false;
+    }
+}
+
+//双击按钮
+public class DoubleClickButton : ClickEvent.ClickListener
+{
+    public int maxSpaceTime = 250;
+    private int clickCount = 0;
+    private Stopwatch stopWatch;
+    private float lastPointTime = 0;
+
+    public DoubleClickButton()
+    {
+        stopWatch = new Stopwatch();
+        Reset();
+    }
+
+    protected override void OnPointerDown(PointerEventData eventData)
+    {
+        isRuning = true;
+    }
+
+    protected override void OnPointerClick(PointerEventData eventData)
+    {
+        stopWatch.Start();
+        clickCount++;
+        if (clickCount == 2)
+        {
+            Reset();
+            callBack?.Invoke(true);
+        }
+    }
+
+    protected override void Update()
+    {
+        if (!stopWatch.IsRunning) return;
+        if (stopWatch.ElapsedMilliseconds > maxSpaceTime)
+        {
+            Reset();
+            callBack?.Invoke(false);
+        }
+    }
+
+    protected override void Reset()
+    {
+        isRuning = false;
+        if (stopWatch.IsRunning) stopWatch.Stop();
+        stopWatch.Reset();
+        clickCount = 0;
+    }
+}
+
+//长按按钮
+public class PressButton : ClickEvent.ClickListener
+{
+    public int pressMinTime = 2000;
+    private Stopwatch stopWatch;
+
+    public PressButton()
+    {
+        stopWatch = new Stopwatch();
+    }
+
+    protected override void OnPointerDown(PointerEventData eventData)
+    {
+        isRuning = true;
+        stopWatch.Start();
+    }
+
+    protected override void OnPointerUp(PointerEventData eventData)
+    {
+        Reset();
+        callBack?.Invoke(false);
+    }
+
+
+    protected override void Update()
+    {
+        if (!stopWatch.IsRunning) return;
+        if (stopWatch.ElapsedMilliseconds > pressMinTime)
+        {
+            Reset();
+            callBack?.Invoke(true);
+        }
+    }
+
+    protected override void Reset()
+    {
+        isRuning = false;
+        if (stopWatch.IsRunning) stopWatch.Stop();
+        stopWatch.Reset();
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/BridgeBtn.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 8d1951aa8a5b776469729eb47d691b5c
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 292 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/ClickEvent.cs

@@ -0,0 +1,292 @@
+using System.Collections.Generic;
+using System;
+using UnityEngine.EventSystems;
+using UnityEngine;
+
+public class ClickEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler
+{
+    private List<IClickListener> listenerList = new List<IClickListener>();
+    private Dictionary<IClickListener, Action> callBackDict = new Dictionary<IClickListener, Action>();
+
+    public void OnPointerDown(PointerEventData eventData)
+    {
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listenerList[i]?.OnPointerDown(eventData);
+        }
+    }
+
+    public void OnPointerUp(PointerEventData eventData)
+    {
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listenerList[i]?.OnPointerUp(eventData);
+        }
+    }
+
+    public void OnPointerClick(PointerEventData eventData)
+    {
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listenerList[i]?.OnPointerClick(eventData);
+        }
+    }
+
+    private void Update()
+    {
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listenerList[i]?.Update();
+        }
+    }
+
+    public T Add<T>() where T : ClickListener, new()
+    {
+        T t = Get<T>();
+        if (t == null)
+        {
+            t = new T();
+            (t as IClickListener).OnCallBack((success) => OnReceive(success, t));
+            if (listenerList.Count == 0 || t is ClickButton) listenerList.Add(t);
+            else listenerList.Insert(listenerList.Count - 1, t);
+        }
+
+        return t;
+    }
+
+    public void Remove<T>() where T : ClickListener
+    {
+        if (!Contains<T>()) return;
+        T t = Get<T>();
+        if (callBackDict.ContainsKey(t)) callBackDict.Remove(t);
+        listenerList.Remove(t);
+    }
+
+    public void RemoveAll()
+    {
+        listenerList.Clear();
+        callBackDict.Clear();
+    }
+
+    public T Get<T>() where T : ClickListener
+    {
+        if (listenerList.Count == 0) return default;
+        IClickListener listener = default;
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listener = listenerList[i];
+            if (listener is T) return (T)listener;
+        }
+
+        return default;
+    }
+
+    public bool Contains<T>() where T : ClickListener
+    {
+        if (listenerList.Count == 0) return false;
+        IClickListener listener = default;
+        for (int i = listenerList.Count - 1; i >= 0; i--)
+        {
+            listener = listenerList[i];
+            if (listener is T) return true;
+        }
+
+        return false;
+    }
+
+    public bool ContainsCallBack<T>() where T : ClickListener
+    {
+        T t = Get<T>();
+        if (t == null) return false;
+        if (callBackDict.ContainsKey(t))
+        {
+            if (callBackDict[t] == null)
+            {
+                callBackDict.Remove(t);
+                return false;
+            }
+
+            return true;
+        }
+        else return false;
+    }
+
+    public Action GetCallBack<T>() where T : ClickListener
+    {
+        T t = Get<T>();
+        if (t == null) return null;
+        if (callBackDict.ContainsKey(t))
+        {
+            Action callBack = callBackDict[t];
+            if (callBack == null)
+            {
+                callBackDict.Remove(t);
+                return null;
+            }
+
+            return callBack;
+        }
+        else return null;
+    }
+
+    public bool HasRuning()
+    {
+        bool isRuning = false;
+        foreach (var lis in listenerList)
+        {
+            if (lis.isRuning)
+            {
+                isRuning = true;
+                break;
+            }
+        }
+
+        return isRuning;
+    }
+
+    public T OnRegister<T>(Action callBack) where T : ClickListener, new()
+    {
+        T t = Add<T>();
+        if (callBackDict.ContainsKey(t)) callBackDict[t] = callBack;
+        else callBackDict.Add(t, callBack);
+        return t;
+    }
+
+    private Action clickCallBack = null;
+
+    void OnReceive(bool success, IClickListener listener)
+    {
+        if (success)
+        {
+            if (listener is ClickButton) //单击事件
+            {
+                Action cbk = GetCallBack<ClickButton>();
+                if (cbk == null) return;
+                if (HasRuning()) //有正在运行的事件检测单击事件滞后分发
+                {
+                    clickCallBack = cbk;
+                }
+                else
+                {
+                    clickCallBack = null;
+                    ResetOther();
+                    cbk.Invoke();
+                }
+            }
+            else //其他事件
+            {
+                clickCallBack = null;
+                ResetOther();
+                if (callBackDict.ContainsKey(listener)) callBackDict[listener]?.Invoke();
+            }
+        }
+        else
+        {
+            if (!HasRuning())
+            {
+                clickCallBack?.Invoke();
+                clickCallBack = null;
+            }
+        }
+
+        void ResetOther()
+        {
+            foreach (var btn in listenerList)
+            {
+                if (btn != listener) btn?.Reset();
+            }
+        }
+    }
+
+#if UNITY_EDITOR
+
+    [UnityEditor.CustomEditor(typeof(ClickEvent))]
+    class ClickEventInspector : UnityEditor.Editor
+    {
+        private ClickEvent clickEvent;
+
+        private void OnEnable()
+        {
+            clickEvent = target as ClickEvent;
+        }
+
+        public override void OnInspectorGUI()
+        {
+            base.OnInspectorGUI();
+            GUILayout.Label("ClickListeners: ");
+            IClickListener listener;
+            for (int i = clickEvent.listenerList.Count - 1; i >= 0; i--)
+            {
+                listener = clickEvent.listenerList[i];
+                GUILayout.Label("    " + listener);
+            }
+        }
+    }
+
+#endif
+
+    private interface IClickListener
+    {
+        bool isRuning { get; set; }
+        void OnPointerDown(PointerEventData eventData);
+        void OnPointerUp(PointerEventData eventData);
+
+        void OnPointerClick(PointerEventData eventData);
+
+        void Update();
+
+        void OnCallBack(Action<bool> callBack);
+
+        void Reset();
+
+        void Dispose();
+    }
+
+    public abstract class ClickListener : IClickListener
+    {
+        bool IClickListener.isRuning
+        {
+            get => isRuning;
+            set { isRuning = value; }
+        }
+
+        protected bool isRuning = false;
+
+        protected Action<bool> callBack { get; private set; }
+        void IClickListener.OnPointerDown(PointerEventData eventData) => OnPointerDown(eventData);
+        void IClickListener.OnPointerUp(PointerEventData eventData) => OnPointerUp(eventData);
+        void IClickListener.OnPointerClick(PointerEventData eventData) => OnPointerClick(eventData);
+        void IClickListener.Update() => Update();
+        void IClickListener.Reset() => Reset();
+        void IClickListener.Dispose() => Dispose();
+
+        void IClickListener.OnCallBack(Action<bool> callBack)
+        {
+            this.callBack = callBack;
+        }
+
+        protected virtual void OnPointerDown(PointerEventData eventData)
+        {
+        }
+
+        protected virtual void OnPointerUp(PointerEventData eventData)
+        {
+        }
+
+        protected virtual void OnPointerClick(PointerEventData eventData)
+        {
+        }
+
+        protected virtual void Update()
+        {
+        }
+
+        protected abstract void Reset();
+
+        protected virtual void Dispose()
+        {
+            callBack = null;
+        }
+    }
+}
+

+ 11 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/ClickEvent.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: e8f47fd23410cb74d9fc5274ece7f9bd
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 36 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/GameObjectExpand.cs

@@ -0,0 +1,36 @@
+using System;
+using UnityEngine;
+
+public static class GameObjectExpand
+{
+    public static ClickButton OnClick(this GameObject obj, Action callBack)
+    {
+        if (obj == null) return null;
+        ClickEvent clickEvent = GetClickEvent(obj);
+        ClickButton button = clickEvent.OnRegister<ClickButton>(callBack);
+        return button;
+    }
+
+    public static DoubleClickButton OnDoubleClick(this GameObject obj, Action callBack)
+    {
+        if (obj == null) return null;
+        ClickEvent clickEvent = GetClickEvent(obj);
+        DoubleClickButton button = clickEvent.OnRegister<DoubleClickButton>(callBack);
+        return button;
+    }
+
+    public static PressButton OnPress(this GameObject obj, Action callBack)
+    {
+        if (obj == null) return null;
+        ClickEvent clickEvent = GetClickEvent(obj);
+        PressButton button = clickEvent.OnRegister<PressButton>(callBack);
+        return button;
+    }
+
+    static ClickEvent GetClickEvent(GameObject obj)
+    {
+        ClickEvent clickEvent = obj.GetComponent<ClickEvent>();
+        if (clickEvent == null) clickEvent = obj.AddComponent<ClickEvent>();
+        return clickEvent;
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Module/UIBridge/GameObjectExpand.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: bdc4bcc4ec2b9a247a0693cad7fa9220
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: