JCLib.cs 4.4 KB

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