Utils.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. using System.Threading.Tasks;
  8. using UnityEngine.UI;
  9. #endif
  10. namespace JTSystems
  11. {
  12. public static class Utils
  13. {
  14. public static Vector2 GetScreenCenter()
  15. {
  16. return new Vector2((float)Screen.width / 2, (float)Screen.height / 2);
  17. }
  18. public static Transform GetClosestTransformInArray(Transform source, Transform[] objectsArray,
  19. float limitDistance = 50000)
  20. {
  21. int closestIndex = -1;
  22. float closestDistance = limitDistance;
  23. for (int i = 0; i < objectsArray.Length; i++)
  24. {
  25. float distance = Vector3.Distance(source.position, objectsArray[i].position);
  26. if (distance < closestDistance)
  27. {
  28. closestDistance = distance;
  29. closestIndex = i;
  30. }
  31. }
  32. if (closestIndex == -1) return null;
  33. return objectsArray[closestIndex];
  34. }
  35. public static int GetClosestVectorIndexInArray(Vector3 source, Vector3[] objectsArray,
  36. float limitDistance = 50000)
  37. {
  38. int closestIndex = -1;
  39. float closestDistance = limitDistance;
  40. for (int i = 0; i < objectsArray.Length; i++)
  41. {
  42. float distance = Vector3.Distance(source, objectsArray[i]);
  43. if (distance < closestDistance)
  44. {
  45. closestDistance = distance;
  46. closestIndex = i;
  47. }
  48. }
  49. if (closestIndex == -1) return -1;
  50. return closestIndex;
  51. }
  52. public static Transform[] ColliderToTransformArray(Collider[] colliders)
  53. {
  54. Transform[] transforms = new Transform[colliders.Length];
  55. for (int i = 0; i < colliders.Length; i++)
  56. {
  57. transforms[i] = colliders[i].transform;
  58. }
  59. return transforms;
  60. }
  61. public static Texture2D LoadTexture(string path)
  62. {
  63. if (!File.Exists(path))
  64. {
  65. Debug.Log("File does not exist ");
  66. return null;
  67. }
  68. byte[] bytes = File.ReadAllBytes(path);
  69. Texture2D result = new Texture2D(1, 1);
  70. FileInfo finfo = new FileInfo(path);
  71. result.name = finfo.Name;
  72. result.LoadImage(bytes);
  73. return result;
  74. }
  75. #if UNITY_EDITOR
  76. public static void SaveTexture(Texture2D tex, string path)
  77. {
  78. byte[] bytes = tex.EncodeToPNG();
  79. var dirPath = Application.dataPath + path;
  80. if (!Directory.Exists(dirPath))
  81. {
  82. Directory.CreateDirectory(dirPath);
  83. }
  84. File.WriteAllBytes(dirPath + "Stamped_Image_" + Directory.GetFiles(dirPath).Length / 2 + ".png", bytes);
  85. AssetDatabase.Refresh();
  86. }
  87. #endif
  88. // Returns a random gameobject from an array
  89. public static GameObject GetRandomGameobjectFromArray(GameObject[] objectsArray)
  90. {
  91. // Initialize the random gameobject
  92. GameObject randomGameobject;
  93. // Get a random index
  94. int randomIndex = Random.Range(0, objectsArray.Length);
  95. // Store the random gameobject
  96. randomGameobject = objectsArray[randomIndex];
  97. // Return it
  98. return randomGameobject;
  99. }
  100. // Returns a random gameobject from a list
  101. public static GameObject GetRandomGameobjectFromList(List<GameObject> objectsArray)
  102. {
  103. // Initialize the random gameobject
  104. GameObject randomGameobject;
  105. // Get a random index
  106. int randomIndex = Random.Range(0, objectsArray.Count);
  107. // Store the random gameobject
  108. randomGameobject = objectsArray[randomIndex];
  109. // Return it
  110. return randomGameobject;
  111. }
  112. // Format an int to string with a space each thousand
  113. public static string FormatAmountString(int amount)
  114. {
  115. string formattedAmount = "";
  116. if (amount < 1000)
  117. {
  118. formattedAmount = amount.ToString();
  119. }
  120. else if (amount >= 1000 && amount < 1000000)
  121. {
  122. formattedAmount = (amount / 1000).ToString() + " " +
  123. amount.ToString().Substring(amount.ToString().Length - 3, 3);
  124. }
  125. return formattedAmount;
  126. }
  127. // Returns the screen ratio
  128. public static float GetScreenRatio()
  129. {
  130. float w = Screen.currentResolution.width;
  131. float h = Screen.currentResolution.height;
  132. float ratio = w / h;
  133. return ratio;
  134. }
  135. public static void EnableCG(CanvasGroup cg)
  136. {
  137. cg.alpha = 1;
  138. cg.interactable = true;
  139. cg.blocksRaycasts = true;
  140. }
  141. public static void DisableCG(CanvasGroup cg)
  142. {
  143. cg.alpha = 0;
  144. cg.interactable = false;
  145. cg.blocksRaycasts = false;
  146. }
  147. public static void HideAllCGs(CanvasGroup[] cgs, CanvasGroup toShow = null)
  148. {
  149. for (int i = 0; i < cgs.Length; i++)
  150. {
  151. if (cgs[i] == toShow)
  152. EnableCG(toShow);
  153. else
  154. DisableCG(cgs[i]);
  155. }
  156. }
  157. #if UNITY_EDITOR
  158. public static void CategoryHeader(string categoryTitle, int spacing = 20, GUIStyle style = null)
  159. {
  160. // Configure the styles
  161. if (style == null)
  162. {
  163. style = new GUIStyle();
  164. style.alignment = TextAnchor.MiddleCenter;
  165. style.fontSize = 14;
  166. style.fontStyle = FontStyle.BoldAndItalic;
  167. }
  168. GUILayout.Space(spacing);
  169. EditorGUILayout.LabelField(categoryTitle, style);
  170. GUILayout.Space(spacing);
  171. }
  172. public static void ShowSerializedField(SerializedObject serializedObject, string fieldName, string label = "")
  173. {
  174. if (label == "")
  175. label = serializedObject.FindProperty(fieldName).displayName;
  176. EditorGUILayout.PropertyField(serializedObject.FindProperty(fieldName), new GUIContent(label));
  177. }
  178. #endif
  179. public static Texture2D GetColoredTexture(Color color)
  180. {
  181. int width = 100;
  182. int height = 100;
  183. Color[] colors = new Color[width * height];
  184. for (int i = 0; i < colors.Length; i++)
  185. colors[i] = color;
  186. Texture2D tex = new Texture2D(width, height);
  187. tex.SetPixels(colors);
  188. return tex;
  189. }
  190. public static void Rate()
  191. {
  192. #if UNITY_IOS
  193. UnityEngine.iOS.Device.RequestStoreReview();
  194. #elif UNITY_ANDROID
  195. string packageName = Application.identifier;
  196. Application.OpenURL("https://play.google.com/store/apps/details?id=" + packageName);
  197. #endif
  198. }
  199. }
  200. }