| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<object> objects = new HashSet<object>();
- 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<AndroidJavaObject>("currentActivity");
- var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
- var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
- intent.Call<AndroidJavaObject>("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<int>("myPid");
- process.CallStatic("killProcess", pid);
- }
- }
- }
- 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;
- }
- }
- }
- }
|