123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- #if UNITY_EDITOR
- using UnityEditor;
- using System.Threading.Tasks;
- using UnityEngine.UI;
- #endif
- namespace JTSystems
- {
- public static class Utils
- {
- public static Vector2 GetScreenCenter()
- {
- return new Vector2((float)Screen.width / 2, (float)Screen.height / 2);
- }
- public static Transform GetClosestTransformInArray(Transform source, Transform[] objectsArray,
- float limitDistance = 50000)
- {
- int closestIndex = -1;
- float closestDistance = limitDistance;
- for (int i = 0; i < objectsArray.Length; i++)
- {
- float distance = Vector3.Distance(source.position, objectsArray[i].position);
- if (distance < closestDistance)
- {
- closestDistance = distance;
- closestIndex = i;
- }
- }
- if (closestIndex == -1) return null;
- return objectsArray[closestIndex];
- }
- public static int GetClosestVectorIndexInArray(Vector3 source, Vector3[] objectsArray,
- float limitDistance = 50000)
- {
- int closestIndex = -1;
- float closestDistance = limitDistance;
- for (int i = 0; i < objectsArray.Length; i++)
- {
- float distance = Vector3.Distance(source, objectsArray[i]);
- if (distance < closestDistance)
- {
- closestDistance = distance;
- closestIndex = i;
- }
- }
- if (closestIndex == -1) return -1;
- return closestIndex;
- }
- public static Transform[] ColliderToTransformArray(Collider[] colliders)
- {
- Transform[] transforms = new Transform[colliders.Length];
- for (int i = 0; i < colliders.Length; i++)
- {
- transforms[i] = colliders[i].transform;
- }
- return transforms;
- }
- public static Texture2D LoadTexture(string path)
- {
- if (!File.Exists(path))
- {
- Debug.Log("File does not exist ");
- return null;
- }
- byte[] bytes = File.ReadAllBytes(path);
- Texture2D result = new Texture2D(1, 1);
- FileInfo finfo = new FileInfo(path);
- result.name = finfo.Name;
- result.LoadImage(bytes);
- return result;
- }
- #if UNITY_EDITOR
- public static void SaveTexture(Texture2D tex, string path)
- {
- byte[] bytes = tex.EncodeToPNG();
- var dirPath = Application.dataPath + path;
- if (!Directory.Exists(dirPath))
- {
- Directory.CreateDirectory(dirPath);
- }
- File.WriteAllBytes(dirPath + "Stamped_Image_" + Directory.GetFiles(dirPath).Length / 2 + ".png", bytes);
- AssetDatabase.Refresh();
- }
- #endif
- // Returns a random gameobject from an array
- public static GameObject GetRandomGameobjectFromArray(GameObject[] objectsArray)
- {
- // Initialize the random gameobject
- GameObject randomGameobject;
- // Get a random index
- int randomIndex = Random.Range(0, objectsArray.Length);
- // Store the random gameobject
- randomGameobject = objectsArray[randomIndex];
- // Return it
- return randomGameobject;
- }
- // Returns a random gameobject from a list
- public static GameObject GetRandomGameobjectFromList(List<GameObject> objectsArray)
- {
- // Initialize the random gameobject
- GameObject randomGameobject;
- // Get a random index
- int randomIndex = Random.Range(0, objectsArray.Count);
- // Store the random gameobject
- randomGameobject = objectsArray[randomIndex];
- // Return it
- return randomGameobject;
- }
- // Format an int to string with a space each thousand
- public static string FormatAmountString(int amount)
- {
- string formattedAmount = "";
- if (amount < 1000)
- {
- formattedAmount = amount.ToString();
- }
- else if (amount >= 1000 && amount < 1000000)
- {
- formattedAmount = (amount / 1000).ToString() + " " +
- amount.ToString().Substring(amount.ToString().Length - 3, 3);
- }
- return formattedAmount;
- }
- // Returns the screen ratio
- public static float GetScreenRatio()
- {
- float w = Screen.currentResolution.width;
- float h = Screen.currentResolution.height;
- float ratio = w / h;
- return ratio;
- }
- public static void EnableCG(CanvasGroup cg)
- {
- cg.alpha = 1;
- cg.interactable = true;
- cg.blocksRaycasts = true;
- }
- public static void DisableCG(CanvasGroup cg)
- {
- cg.alpha = 0;
- cg.interactable = false;
- cg.blocksRaycasts = false;
- }
- public static void HideAllCGs(CanvasGroup[] cgs, CanvasGroup toShow = null)
- {
- for (int i = 0; i < cgs.Length; i++)
- {
- if (cgs[i] == toShow)
- EnableCG(toShow);
- else
- DisableCG(cgs[i]);
- }
- }
- #if UNITY_EDITOR
- public static void CategoryHeader(string categoryTitle, int spacing = 20, GUIStyle style = null)
- {
- // Configure the styles
- if (style == null)
- {
- style = new GUIStyle();
- style.alignment = TextAnchor.MiddleCenter;
- style.fontSize = 14;
- style.fontStyle = FontStyle.BoldAndItalic;
- }
- GUILayout.Space(spacing);
- EditorGUILayout.LabelField(categoryTitle, style);
- GUILayout.Space(spacing);
- }
- public static void ShowSerializedField(SerializedObject serializedObject, string fieldName, string label = "")
- {
- if (label == "")
- label = serializedObject.FindProperty(fieldName).displayName;
- EditorGUILayout.PropertyField(serializedObject.FindProperty(fieldName), new GUIContent(label));
- }
- #endif
- public static Texture2D GetColoredTexture(Color color)
- {
- int width = 100;
- int height = 100;
- Color[] colors = new Color[width * height];
- for (int i = 0; i < colors.Length; i++)
- colors[i] = color;
- Texture2D tex = new Texture2D(width, height);
- tex.SetPixels(colors);
- return tex;
- }
- public static void Rate()
- {
- #if UNITY_IOS
- UnityEngine.iOS.Device.RequestStoreReview();
- #elif UNITY_ANDROID
- string packageName = Application.identifier;
- Application.OpenURL("https://play.google.com/store/apps/details?id=" + packageName);
- #endif
- }
- }
- }
|