Utils.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, int imageIndex = 1)
  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. File.WriteAllBytes(dirPath + "" + imageIndex + ".png", bytes);
  86. AssetDatabase.Refresh();
  87. }
  88. #endif
  89. // Returns a random gameobject from an array
  90. public static GameObject GetRandomGameobjectFromArray(GameObject[] objectsArray)
  91. {
  92. // Initialize the random gameobject
  93. GameObject randomGameobject;
  94. // Get a random index
  95. int randomIndex = Random.Range(0, objectsArray.Length);
  96. // Store the random gameobject
  97. randomGameobject = objectsArray[randomIndex];
  98. // Return it
  99. return randomGameobject;
  100. }
  101. // Returns a random gameobject from a list
  102. public static GameObject GetRandomGameobjectFromList(List<GameObject> objectsArray)
  103. {
  104. // Initialize the random gameobject
  105. GameObject randomGameobject;
  106. // Get a random index
  107. int randomIndex = Random.Range(0, objectsArray.Count);
  108. // Store the random gameobject
  109. randomGameobject = objectsArray[randomIndex];
  110. // Return it
  111. return randomGameobject;
  112. }
  113. // Format an int to string with a space each thousand
  114. public static string FormatAmountString(int amount)
  115. {
  116. string formattedAmount = "";
  117. if (amount < 1000)
  118. {
  119. formattedAmount = amount.ToString();
  120. }
  121. else if (amount >= 1000 && amount < 1000000)
  122. {
  123. formattedAmount = (amount / 1000).ToString() + " " +
  124. amount.ToString().Substring(amount.ToString().Length - 3, 3);
  125. }
  126. return formattedAmount;
  127. }
  128. // Returns the screen ratio
  129. public static float GetScreenRatio()
  130. {
  131. float w = Screen.currentResolution.width;
  132. float h = Screen.currentResolution.height;
  133. float ratio = w / h;
  134. return ratio;
  135. }
  136. public static void EnableCG(CanvasGroup cg)
  137. {
  138. cg.alpha = 1;
  139. cg.interactable = true;
  140. cg.blocksRaycasts = true;
  141. }
  142. public static void DisableCG(CanvasGroup cg)
  143. {
  144. cg.alpha = 0;
  145. cg.interactable = false;
  146. cg.blocksRaycasts = false;
  147. }
  148. public static void HideAllCGs(CanvasGroup[] cgs, CanvasGroup toShow = null)
  149. {
  150. for (int i = 0; i < cgs.Length; i++)
  151. {
  152. if (cgs[i] == toShow)
  153. EnableCG(toShow);
  154. else
  155. DisableCG(cgs[i]);
  156. }
  157. }
  158. #if UNITY_EDITOR
  159. public static void CategoryHeader(string categoryTitle, int spacing = 20, GUIStyle style = null)
  160. {
  161. // Configure the styles
  162. if (style == null)
  163. {
  164. style = new GUIStyle();
  165. style.alignment = TextAnchor.MiddleCenter;
  166. style.fontSize = 14;
  167. style.fontStyle = FontStyle.BoldAndItalic;
  168. }
  169. GUILayout.Space(spacing);
  170. EditorGUILayout.LabelField(categoryTitle, style);
  171. GUILayout.Space(spacing);
  172. }
  173. public static void ShowSerializedField(SerializedObject serializedObject, string fieldName, string label = "")
  174. {
  175. if (label == "")
  176. label = serializedObject.FindProperty(fieldName).displayName;
  177. EditorGUILayout.PropertyField(serializedObject.FindProperty(fieldName), new GUIContent(label));
  178. }
  179. #endif
  180. public static Texture2D GetColoredTexture(Color color)
  181. {
  182. int width = 100;
  183. int height = 100;
  184. Color[] colors = new Color[width * height];
  185. for (int i = 0; i < colors.Length; i++)
  186. colors[i] = color;
  187. Texture2D tex = new Texture2D(width, height);
  188. tex.SetPixels(colors);
  189. return tex;
  190. }
  191. public static void Rate()
  192. {
  193. #if UNITY_IOS
  194. UnityEngine.iOS.Device.RequestStoreReview();
  195. #elif UNITY_ANDROID
  196. string packageName = Application.identifier;
  197. Application.OpenURL("https://play.google.com/store/apps/details?id=" + packageName);
  198. #endif
  199. }
  200. }
  201. }