using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace JC.CS { public class Utility { public static long GetTimestamp() { TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1); return (long)ts.TotalMilliseconds; } } public class CountLocker { int count = 0; HashSet objects = new HashSet(); public void Lock() { count++; } public void Lock(object obj) { objects.Add(obj); } public void Unlock() { count--; if (count < 0) count = 0; } public void Unlock(object obj) { objects.Remove(obj); } public bool IsLocked() { return count > 0 || objects.Count > 0; } public bool IsReleased() { return !IsLocked(); } public void Clear() { count = 0; objects.Clear(); } } //节流器 public class Throttler { long lastPassTime = -1; long limitTime; public Throttler(long limitTime) { this.limitTime = limitTime; } public bool CanPass() { long curTime = Utility.GetTimestamp(); if (lastPassTime < 0 || curTime - lastPassTime > limitTime) { lastPassTime = curTime; return true; } return false; } } //防抖器 // public class Debouncer {} } namespace JC.Unity { public class AppRestartUtil { public void Restart() { if (Application.platform != RuntimePlatform.Android) return; using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000; const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000; var currentActivity = unityPlayer.GetStatic("currentActivity"); var pm = currentActivity.Call("getPackageManager"); var intent = pm.Call("getLaunchIntentForPackage", Application.identifier); intent.Call("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK); currentActivity.Call("startActivity", intent); currentActivity.Call("finish"); var process = new AndroidJavaClass("android.os.Process"); int pid = process.CallStatic("myPid"); process.CallStatic("killProcess", pid); } } } public class TouchChecker { bool hasTouch; int touchID; bool isTouchOnUI; public Action onBegan; public Action onMoved; public Action 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; } } } }