2using System.Collections.Generic;
3using System.Diagnostics;
6using System.Reflection;
17 alphaMap =
new Color[256];
18 for (
int i = 0; i < 256; i++)
20 alphaMap[i] =
new Color(1f, 1f, 1f, 1f * (
float)i / 255f);
24 public static int Distance(
int pX1,
int pY1,
int pX2,
int pY2)
26 return (
int)Math.Sqrt((pX1 - pX2) * (pX1 - pX2) + (pY1 - pY2) * (pY1 - pY2));
29 public static T CopyComponent<T>(T original, GameObject destination) where T : Component
31 Type type = original.GetType();
32 T val = destination.GetComponent(type) as T;
35 val = destination.AddComponent(type) as T;
37 FieldInfo[] fields = type.GetFields();
38 foreach (FieldInfo fieldInfo
in fields)
40 if (!fieldInfo.IsStatic)
42 fieldInfo.SetValue(val, fieldInfo.GetValue(original));
45 PropertyInfo[] properties = type.GetProperties();
46 foreach (PropertyInfo propertyInfo
in properties)
48 if (propertyInfo.CanWrite && propertyInfo.CanWrite && !(propertyInfo.Name ==
"name") && !(propertyInfo.Name ==
"usedByComposite") && !(propertyInfo.Name ==
"density"))
50 propertyInfo.SetValue(val, propertyInfo.GetValue(original,
null),
null);
56 public static T Instantiate<T>(T t, Component parent =
null) where T : Component
58 T val = UnityEngine.Object.Instantiate(t);
59 val.transform.SetParent(parent ? parent.transform :
null, worldPositionStays:
false);
60 if (!val.gameObject.activeSelf)
62 val.gameObject.SetActive(value:
true);
67 public static Transform
Instantiate(
string path, Component parent =
null)
69 return Instantiate<Transform>(path, parent);
72 public static T Instantiate<T>(
string path, Component parent =
null) where T : Component
74 T val = Resources.Load<T>(path);
84 for (
int num = trans.childCount - 1; num >= 0; num--)
86 UnityEngine.Object.Destroy(trans.GetChild(num).gameObject);
92 Transform transform = c.transform;
93 transform.RebuildLayout();
94 Transform parent = transform.parent;
95 if ((
bool)parent && !(parent == root.transform))
103 rect.anchorMin = Vector2.zero;
104 rect.anchorMax = Vector2.one;
105 rect.localPosition = Vector3.zero;
106 rect.anchoredPosition = Vector2.zero;
107 rect.sizeDelta = Vector2.zero;
113 Vector3 localPosition = rect.localPosition;
114 Vector3 vector = rectTransform.rect.min - rect.rect.min;
115 Vector3 vector2 = rectTransform.rect.max - rect.rect.max;
116 localPosition.x = Mathf.Clamp(localPosition.x, vector.x + (
float)margin, vector2.x - (
float)margin);
117 localPosition.y = Mathf.Clamp(localPosition.y, vector.y + (
float)margin, vector2.y - (
float)margin);
118 rect.localPosition = localPosition;
121 public static float GetAngle(Vector3
self, Vector3 target)
123 Vector3 vector = target -
self;
124 return Mathf.Atan2(vector.y * -1f, vector.x) * 57.29578f + 90f;
129 return Mathf.Atan2(0f - y, x) * 57.29578f + 90f;
134 if (Math.Abs(normal.y - 1f) < 0.1f)
138 if (Math.Abs(normal.x - 1f) < 0.1f)
142 if (Math.Abs(normal.x - -1f) < 0.1f)
146 if (Math.Abs(normal.z - 1f) < 0.1f)
150 if (Math.Abs(normal.z - -1f) < 0.1f)
165 return new Vector2(-1f, 1f);
169 return new Vector2(0f, 1f);
173 return new Vector2(-1f, 0f);
184 return new Vector2(1f, 1f);
188 return new Vector2(-1f, -1f);
195 return new Vector2(1f, -1f);
199 return new Vector2(1f, 0f);
203 return new Vector2(0f, -1f);
209 public static Vector2
WorldToUIPos(Vector3 worldPoint, RectTransform container)
211 Vector2 localPoint = Vector2.zero;
212 Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, worldPoint);
213 RectTransformUtility.ScreenPointToLocalPointInRectangle(container, screenPoint, canvas.worldCamera, out localPoint);
217 public static T RandomEnum<T>()
219 Array values = Enum.GetValues(typeof(T));
220 return (T)values.GetValue(
new System.Random().Next(values.Length));
223 public static List<T> EnumToList<T>() where T : Enum
225 return Enum.GetValues(typeof(T)).Cast<T>().ToList();
228 public static void ShowExplorer(
string itemPath,
bool selectFirstFile =
false)
232 FileInfo[] files =
new DirectoryInfo(itemPath).GetFiles();
233 if (files.Length != 0)
235 itemPath = itemPath +
"/" + files[0].Name;
238 itemPath = itemPath.Replace(
"/",
"\\");
239 Process.Start(
"explorer.exe",
"/select," + itemPath);
242 public static void Run(
string itemPath)
244 Process.Start(itemPath);
247 public static T[,] ResizeArray<T>(T[,] original,
int x,
int y, Func<int, int, T> func)
249 T[,] array =
new T[x, y];
250 int num = Math.Min(x, original.GetLength(0));
251 int num2 = Math.Min(y, original.GetLength(1));
252 for (
int i = 0; i < x; i++)
254 for (
int j = 0; j < y; j++)
256 if (i >= num || j >= num2)
258 array[i, j] = func(i, j);
262 array[i, j] = original[i, j];
static Vector2 ConvertAxis(Vector2 v)
static void Run(string itemPath)
static Vector2 WorldToUIPos(Vector3 worldPoint, RectTransform container)
static void DestroyChildren(Transform trans)
static VectorDir GetVectorDir(Vector3 normal)
static void RebuildLayoutInParents(Component c, Component root)
static int Distance(int pX1, int pY1, int pX2, int pY2)
static float GetAngle(float x, float y)
static float GetAngle(Vector3 self, Vector3 target)
static Transform Instantiate(string path, Component parent=null)
static void SetRectToFitScreen(RectTransform rect)
static void ShowExplorer(string itemPath, bool selectFirstFile=false)
static void ClampToScreen(RectTransform rect, int margin=0)