JCLib.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. namespace JC.CS
  7. {
  8. public class Utility
  9. {
  10. public static long GetTimestamp()
  11. {
  12. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  13. return (long)ts.TotalMilliseconds;
  14. }
  15. }
  16. public class CountLocker
  17. {
  18. int count = 0;
  19. HashSet<object> objects = new HashSet<object>();
  20. public void Lock()
  21. {
  22. count++;
  23. }
  24. public void Lock(object obj)
  25. {
  26. objects.Add(obj);
  27. }
  28. public void Unlock()
  29. {
  30. count--;
  31. if (count < 0) count = 0;
  32. }
  33. public void Unlock(object obj)
  34. {
  35. objects.Remove(obj);
  36. }
  37. public bool IsLocked()
  38. {
  39. return count > 0 || objects.Count > 0;
  40. }
  41. public bool IsReleased()
  42. {
  43. return !IsLocked();
  44. }
  45. public void Clear()
  46. {
  47. count = 0;
  48. objects.Clear();
  49. }
  50. }
  51. //节流器
  52. public class Throttler {
  53. long lastPassTime = -1;
  54. long limitTime;
  55. public Throttler(long limitTime) {
  56. this.limitTime = limitTime;
  57. }
  58. public bool CanPass() {
  59. long curTime = Utility.GetTimestamp();
  60. if (lastPassTime < 0 || curTime - lastPassTime > limitTime) {
  61. lastPassTime = curTime;
  62. return true;
  63. }
  64. return false;
  65. }
  66. }
  67. //防抖器
  68. // public class Debouncer {}
  69. }
  70. namespace JC.Unity
  71. {
  72. public class AppRestartUtil {
  73. public void Restart()
  74. {
  75. if (Application.platform != RuntimePlatform.Android) return;
  76. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  77. {
  78. const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
  79. const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
  80. var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  81. var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  82. var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
  83. intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
  84. currentActivity.Call("startActivity", intent);
  85. currentActivity.Call("finish");
  86. var process = new AndroidJavaClass("android.os.Process");
  87. int pid = process.CallStatic<int>("myPid");
  88. process.CallStatic("killProcess", pid);
  89. }
  90. }
  91. }
  92. public class TouchChecker
  93. {
  94. bool hasTouch;
  95. int touchID;
  96. bool isTouchOnUI;
  97. public Action<Touch, bool> onBegan;
  98. public Action<Touch, bool> onMoved;
  99. public Action<Touch, bool> onEnded;
  100. public void Update()
  101. {
  102. if (Input.touchCount > 0)
  103. {
  104. if (!hasTouch)
  105. {
  106. Touch touch = Input.GetTouch(0);
  107. if (touch.phase == TouchPhase.Began)
  108. {
  109. hasTouch = true;
  110. touchID = touch.fingerId;
  111. }
  112. }
  113. if (hasTouch) {
  114. Touch touch = default;
  115. bool hasFindTouch = false;
  116. foreach (Touch t in Input.touches)
  117. {
  118. if (t.fingerId == touchID)
  119. {
  120. touch = t;
  121. hasFindTouch = true;
  122. break;
  123. }
  124. }
  125. if (!hasFindTouch)
  126. {
  127. hasTouch = false;
  128. return;
  129. }
  130. bool isOverUI = EventSystem.current.IsPointerOverGameObject(touchID);
  131. if (touch.phase == TouchPhase.Began)
  132. {
  133. isTouchOnUI = isOverUI;
  134. onBegan?.Invoke(touch, isTouchOnUI);
  135. }
  136. else if (touch.phase == TouchPhase.Moved)
  137. {
  138. isTouchOnUI = isOverUI;
  139. onMoved?.Invoke(touch, isTouchOnUI);
  140. }
  141. else if (touch.phase == TouchPhase.Stationary)
  142. {
  143. }
  144. else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  145. {
  146. hasTouch = false;
  147. //UnityBug,Ended阶段isOverUI总是返回false,因此通过新增变量touchOnUI来辅助识别触点是否在UI上
  148. onEnded?.Invoke(touch, isTouchOnUI);
  149. }
  150. }
  151. }
  152. else
  153. {
  154. hasTouch = false;
  155. }
  156. }
  157. }
  158. }