|
|
@@ -1,161 +0,0 @@
|
|
|
-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;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-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;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|