JCLib.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. namespace JC.Unity
  53. {
  54. public class AppRestartUtil {
  55. public void Restart()
  56. {
  57. if (Application.platform != RuntimePlatform.Android) return;
  58. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  59. {
  60. const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
  61. const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
  62. var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  63. var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  64. var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
  65. intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
  66. currentActivity.Call("startActivity", intent);
  67. currentActivity.Call("finish");
  68. var process = new AndroidJavaClass("android.os.Process");
  69. int pid = process.CallStatic<int>("myPid");
  70. process.CallStatic("killProcess", pid);
  71. }
  72. }
  73. }
  74. public class TouchChecker
  75. {
  76. bool hasTouch;
  77. int touchID;
  78. bool isTouchOnUI;
  79. public Action<Touch, bool> onBegan;
  80. public Action<Touch, bool> onMoved;
  81. public Action<Touch, bool> onEnded;
  82. public void Update()
  83. {
  84. if (Input.touchCount > 0)
  85. {
  86. if (!hasTouch)
  87. {
  88. Touch touch = Input.GetTouch(0);
  89. if (touch.phase == TouchPhase.Began)
  90. {
  91. hasTouch = true;
  92. touchID = touch.fingerId;
  93. }
  94. }
  95. if (hasTouch) {
  96. Touch touch = default;
  97. bool hasFindTouch = false;
  98. foreach (Touch t in Input.touches)
  99. {
  100. if (t.fingerId == touchID)
  101. {
  102. touch = t;
  103. hasFindTouch = true;
  104. break;
  105. }
  106. }
  107. if (!hasFindTouch)
  108. {
  109. hasTouch = false;
  110. return;
  111. }
  112. bool isOverUI = EventSystem.current.IsPointerOverGameObject(touchID);
  113. if (touch.phase == TouchPhase.Began)
  114. {
  115. isTouchOnUI = isOverUI;
  116. onBegan?.Invoke(touch, isTouchOnUI);
  117. }
  118. else if (touch.phase == TouchPhase.Moved)
  119. {
  120. isTouchOnUI = isOverUI;
  121. onMoved?.Invoke(touch, isTouchOnUI);
  122. }
  123. else if (touch.phase == TouchPhase.Stationary)
  124. {
  125. }
  126. else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  127. {
  128. hasTouch = false;
  129. //UnityBug,Ended阶段isOverUI总是返回false,因此通过新增变量touchOnUI来辅助识别触点是否在UI上
  130. onEnded?.Invoke(touch, isTouchOnUI);
  131. }
  132. }
  133. }
  134. else
  135. {
  136. hasTouch = false;
  137. }
  138. }
  139. }
  140. }