Przeglądaj źródła

编写库代码

lvjincheng 4 lat temu
rodzic
commit
e06b1e3587

+ 1 - 1
Assets/BowArrow/Scripts/Game/ArrowCamera.cs

@@ -154,8 +154,8 @@ class ArrowCameraTemplate1 : ArrowCameraTemplate
             }
         } else {
             cameraPosition = Vector3.Lerp(cameraPosition, cameraToRunPosition, Time.deltaTime * 6);
-            cameraT.LookAt(this.arrowCamera.arrow.Head());
         }
+        cameraT.LookAt(this.arrowCamera.arrow.Head());
         cameraT.localPosition = cameraPosition;
     }
 }

+ 16 - 43
Assets/BowArrow/Scripts/Game/BowCamera.cs

@@ -22,6 +22,8 @@ public class BowCamera : MonoBehaviour
     }
     //本地欧拉角记录值
     Vector3 localEulerAngles;
+    //触摸检测器
+    JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
     //触摸模式开关
     public static bool isTouchMode = true;
     public static BowCamera ins;
@@ -31,6 +33,19 @@ public class BowCamera : MonoBehaviour
         localEulerAngles = transform.localEulerAngles;
     }
 
+    void Start() {
+        touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
+            if (isOnUI) return;
+            //触摸控制镜头和拉弓射箭
+            this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, -36, 36);
+            this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, -180, 180);   
+            this.transform.localEulerAngles = this.localEulerAngles; 
+        };
+        touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
+            if (!isOnUI) armBow.ADS_fire();
+        };
+    }
+
     void Update()
     {   
         //更新相机视野
@@ -54,55 +69,13 @@ public class BowCamera : MonoBehaviour
             }
         }
         else if (isTouchMode) {
-            if (Input.touchCount > 0) {
-                if (!hasTouch) {
-                    Touch touch = Input.GetTouch(0);
-                    if (touch.phase == TouchPhase.Began) {
-                        hasTouch = true;
-                        touchID = touch.fingerId;
-                    }
-                }
-                if (hasTouch) {
-                    Touch touch = default;
-                    foreach (var t in Input.touches)
-                    {
-                        if (t.fingerId == touchID) touch = t;
-                    }
-                    bool isOverUI = EventSystem.current.IsPointerOverGameObject(touchID);
-                    if (touch.phase == TouchPhase.Began) {
-                        touchOnUI = isOverUI;
-                    }
-                    else if (touch.phase == TouchPhase.Moved) {
-                        touchOnUI = isOverUI;
-                        if (!isOverUI) {
-                            //触摸控制镜头和拉弓射箭
-                            this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - Input.touches[0].deltaPosition.y * Time.deltaTime * 5, -36, 36);
-                            this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + Input.touches[0].deltaPosition.x * Time.deltaTime * 5, -180, 180);   
-                            this.transform.localEulerAngles = this.localEulerAngles; 
-                        }
-                    }
-                    else if (touch.phase == TouchPhase.Ended) {
-                        hasTouch = false;
-                        //UnityBug,Ended阶段isOverUI总是返回false,因此通过新增变量touchOnUI来辅助识别触点是否在UI上
-                        if (touchOnUI) touchOnUI = false;
-                        else armBow.ADS_fire();
-                    }
-                }
-            } else {
-                hasTouch = false;
-            }
+            touchChecker.Update();
         } else {
-            hasTouch = false;
             //镜头看向九轴姿态虚拟节点
             this.transform.LookAt(CameraToLook.ins.point);
         }
     }
 
-    //触摸模式下的有效触点信息记录
-    int touchID;
-    bool hasTouch;
-    bool touchOnUI;
-
     //---------------相机视野的相关操作---------------------
 
     //视野值记录

+ 72 - 0
Assets/DependAsset/JC/JCLib.cs

@@ -1,6 +1,8 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.EventSystems;
 
 namespace JC.CS 
 {
@@ -40,3 +42,73 @@ namespace JC.CS
         }
     }
 }
+namespace JC.Unity
+{
+    public class TouchChecker
+    {   
+        bool hasTouch;
+        int touchID;
+        bool isTouchOnUI;
+        public Action<Touch, bool> onBegan;
+        public Action<Touch, bool> onMoved;
+        public Action<Touch, bool> onEnded;
+        public void Update()
+        {
+            if (Input.touchCount > 0) 
+            {
+                if (!hasTouch) 
+                {
+                    Touch touch = Input.GetTouch(0);
+                    if (touch.phase == TouchPhase.Began) 
+                    {
+                        hasTouch = true;
+                        touchID = touch.fingerId;
+                    }
+                }
+                if (hasTouch) {
+                    Touch touch = default;
+                    bool hasFindTouch = false;
+                    foreach (Touch t in Input.touches)
+                    {
+                        if (t.fingerId == touchID) 
+                        {
+                            touch = t;
+                            hasFindTouch = true;
+                            break;
+                        }
+                    }
+                    if (!hasFindTouch) 
+                    {
+                        hasTouch = false;
+                        return;
+                    }
+                    bool isOverUI = EventSystem.current.IsPointerOverGameObject(touchID);
+                    if (touch.phase == TouchPhase.Began) 
+                    {
+                        isTouchOnUI = isOverUI;
+                        onBegan?.Invoke(touch, isTouchOnUI);
+                    }
+                    else if (touch.phase == TouchPhase.Moved) 
+                    {
+                        isTouchOnUI = isOverUI;
+                        onMoved?.Invoke(touch, isTouchOnUI);
+                    }
+                    else if (touch.phase == TouchPhase.Stationary) 
+                    {
+
+                    }
+                    else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) 
+                    {
+                        hasTouch = false;
+                        //UnityBug,Ended阶段isOverUI总是返回false,因此通过新增变量touchOnUI来辅助识别触点是否在UI上
+                        onEnded?.Invoke(touch, isTouchOnUI);
+                    }
+                }
+            } 
+            else 
+            {
+                hasTouch = false;
+            }
+        }
+    }
+}