Misc.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. #if !ARGPS_USE_VUFORIA
  5. using UnityEngine.XR.ARFoundation;
  6. using UnityEngine.XR.ARSubsystems;
  7. #endif
  8. namespace ARLocation.Utils
  9. {
  10. public class Misc
  11. {
  12. public static bool IsARDevice()
  13. {
  14. return (
  15. Application.platform == RuntimePlatform.Android ||
  16. Application.platform == RuntimePlatform.IPhonePlayer
  17. );
  18. }
  19. public static bool WebRequestResultIsError(UnityWebRequest request)
  20. {
  21. #if UNITY_2020_3_OR_NEWER
  22. return (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError);
  23. #else
  24. return (request.isNetworkError || request.isHttpError);
  25. #endif
  26. }
  27. #if !ARGPS_USE_VUFORIA
  28. public static void RequestPlaneDetectionMode(ARPlaneManager manager, PlaneDetectionMode mode)
  29. {
  30. var managerType = manager.GetType();
  31. var requestedDetectionModeProp = managerType.GetProperty("requestedDetectionMode");
  32. if (requestedDetectionModeProp != null)
  33. {
  34. requestedDetectionModeProp.SetValue(manager, mode);
  35. }
  36. else
  37. {
  38. var detectionModeProp = managerType.GetProperty("detectionMode");
  39. if (detectionModeProp != null)
  40. {
  41. detectionModeProp.SetValue(manager, mode);
  42. }
  43. else
  44. {
  45. throw new System.Exception("[ARGPS][RequestPlaneDetectionMode]: Failed to set detection mode!");
  46. }
  47. }
  48. }
  49. #endif
  50. public static float FloatListAverage(List<float> list)
  51. {
  52. var average = 0.0f;
  53. foreach (var value in list)
  54. {
  55. average += value;
  56. }
  57. return average / list.Count;
  58. }
  59. public static float GetNormalizedDegrees(float value)
  60. {
  61. if (value < 0)
  62. {
  63. return (360 + (value % 360));
  64. }
  65. return value % 360;
  66. }
  67. public static T FindAndGetComponent<T>(string name)
  68. {
  69. var gameObject = GameObject.Find(name);
  70. if (gameObject == null)
  71. {
  72. return default(T);
  73. }
  74. return gameObject.GetComponent<T>();
  75. }
  76. public static T FindAndGetComponentAndLogError<T>(string name, string message)
  77. {
  78. var result = FindAndGetComponent<T>(name);
  79. if (EqualityComparer<T>.Default.Equals(result, default(T)))
  80. {
  81. Debug.LogError(message);
  82. }
  83. return result;
  84. }
  85. public static GameObject FindAndLogError(string name, string message)
  86. {
  87. var go = GameObject.Find(name);
  88. if (go == null)
  89. {
  90. Debug.LogError(message);
  91. }
  92. return go;
  93. }
  94. public static Spline BuildSpline(SplineType type, Vector3[] points, int n, float alpha)
  95. {
  96. if (type == SplineType.CatmullromSpline)
  97. {
  98. return new CatmullRomSpline(points, n, alpha);
  99. }
  100. else
  101. {
  102. return new LinearSpline(points);
  103. }
  104. }
  105. public static void SetActiveOnAllChildren(GameObject go, bool value)
  106. {
  107. foreach (Transform child in go.transform)
  108. {
  109. child.gameObject.SetActive(value);
  110. }
  111. }
  112. public static void SetGameObjectVisible(GameObject go, bool value)
  113. {
  114. var meshRenderer = go.GetComponent<MeshRenderer>();
  115. var skinnedMeshRenderer = go.GetComponent<SkinnedMeshRenderer>();
  116. if (meshRenderer)
  117. {
  118. meshRenderer.enabled = value;
  119. }
  120. if (skinnedMeshRenderer)
  121. {
  122. skinnedMeshRenderer.enabled = value;
  123. }
  124. SetActiveOnAllChildren(go, value);
  125. }
  126. public static void HideGameObject(GameObject go)
  127. {
  128. SetGameObjectVisible(go, false);
  129. }
  130. public static void ShowGameObject(GameObject go)
  131. {
  132. SetGameObjectVisible(go, true);
  133. }
  134. public static void SetTransformPositionY(Transform t, float y)
  135. {
  136. var p = t.position;
  137. p.y = y;
  138. t.position = p;
  139. }
  140. }
  141. }