Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
Util.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.Linq;
6using System.Reflection;
7using UnityEngine;
8
9public class Util
10{
11 public static Color[] alphaMap;
12
13 public static Canvas canvas;
14
15 static Util()
16 {
17 alphaMap = new Color[256];
18 for (int i = 0; i < 256; i++)
19 {
20 alphaMap[i] = new Color(1f, 1f, 1f, 1f * (float)i / 255f);
21 }
22 }
23
24 public static int Distance(int pX1, int pY1, int pX2, int pY2)
25 {
26 return (int)Math.Sqrt((pX1 - pX2) * (pX1 - pX2) + (pY1 - pY2) * (pY1 - pY2));
27 }
28
29 public static T CopyComponent<T>(T original, GameObject destination) where T : Component
30 {
31 Type type = original.GetType();
32 T val = destination.GetComponent(type) as T;
33 if (!val)
34 {
35 val = destination.AddComponent(type) as T;
36 }
37 FieldInfo[] fields = type.GetFields();
38 foreach (FieldInfo fieldInfo in fields)
39 {
40 if (!fieldInfo.IsStatic)
41 {
42 fieldInfo.SetValue(val, fieldInfo.GetValue(original));
43 }
44 }
45 PropertyInfo[] properties = type.GetProperties();
46 foreach (PropertyInfo propertyInfo in properties)
47 {
48 if (propertyInfo.CanWrite && propertyInfo.CanWrite && !(propertyInfo.Name == "name") && !(propertyInfo.Name == "usedByComposite") && !(propertyInfo.Name == "density"))
49 {
50 propertyInfo.SetValue(val, propertyInfo.GetValue(original, null), null);
51 }
52 }
53 return val;
54 }
55
56 public static T Instantiate<T>(T t, Component parent = null) where T : Component
57 {
58 T val = UnityEngine.Object.Instantiate(t);
59 val.transform.SetParent(parent ? parent.transform : null, worldPositionStays: false);
60 if (!val.gameObject.activeSelf)
61 {
62 val.gameObject.SetActive(value: true);
63 }
64 return val;
65 }
66
67 public static Transform Instantiate(string path, Component parent = null)
68 {
69 return Instantiate<Transform>(path, parent);
70 }
71
72 public static T Instantiate<T>(string path, Component parent = null) where T : Component
73 {
74 T val = Resources.Load<T>(path);
75 if (val == null)
76 {
77 return null;
78 }
79 return Instantiate(val, parent);
80 }
81
82 public static void DestroyChildren(Transform trans)
83 {
84 for (int num = trans.childCount - 1; num >= 0; num--)
85 {
86 UnityEngine.Object.Destroy(trans.GetChild(num).gameObject);
87 }
88 }
89
90 public static void RebuildLayoutInParents(Component c, Component root)
91 {
92 Transform transform = c.transform;
93 transform.RebuildLayout();
94 Transform parent = transform.parent;
95 if ((bool)parent && !(parent == root.transform))
96 {
97 RebuildLayoutInParents(parent, root);
98 }
99 }
100
101 public static void SetRectToFitScreen(RectTransform rect)
102 {
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;
108 }
109
110 public static void ClampToScreen(RectTransform rect, int margin = 0)
111 {
112 RectTransform rectTransform = BaseCore.Instance.canvas.Rect();
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;
119 }
120
121 public static float GetAngle(Vector3 self, Vector3 target)
122 {
123 Vector3 vector = target - self;
124 return Mathf.Atan2(vector.y * -1f, vector.x) * 57.29578f + 90f;
125 }
126
127 public static float GetAngle(float x, float y)
128 {
129 return Mathf.Atan2(0f - y, x) * 57.29578f + 90f;
130 }
131
132 public static VectorDir GetVectorDir(Vector3 normal)
133 {
134 if (Math.Abs(normal.y - 1f) < 0.1f)
135 {
136 return VectorDir.down;
137 }
138 if (Math.Abs(normal.x - 1f) < 0.1f)
139 {
140 return VectorDir.left;
141 }
142 if (Math.Abs(normal.x - -1f) < 0.1f)
143 {
144 return VectorDir.right;
145 }
146 if (Math.Abs(normal.z - 1f) < 0.1f)
147 {
148 return VectorDir.back;
149 }
150 if (Math.Abs(normal.z - -1f) < 0.1f)
151 {
152 return VectorDir.forward;
153 }
154 return VectorDir.up;
155 }
156
157 public static Vector2 ConvertAxis(Vector2 v)
158 {
159 float x = v.x;
160 float y = v.y;
161 if (y == 1f)
162 {
163 if (x == 0f)
164 {
165 return new Vector2(-1f, 1f);
166 }
167 if (x == 1f)
168 {
169 return new Vector2(0f, 1f);
170 }
171 if (x == -1f)
172 {
173 return new Vector2(-1f, 0f);
174 }
175 }
176 if (y == 0f)
177 {
178 if (x == 0f)
179 {
180 return Vector2.zero;
181 }
182 if (x == 1f)
183 {
184 return new Vector2(1f, 1f);
185 }
186 if (x == -1f)
187 {
188 return new Vector2(-1f, -1f);
189 }
190 }
191 if (y == -1f)
192 {
193 if (x == 0f)
194 {
195 return new Vector2(1f, -1f);
196 }
197 if (x == 1f)
198 {
199 return new Vector2(1f, 0f);
200 }
201 if (x == -1f)
202 {
203 return new Vector2(0f, -1f);
204 }
205 }
206 return Vector3.zero;
207 }
208
209 public static Vector2 WorldToUIPos(Vector3 worldPoint, RectTransform container)
210 {
211 Vector2 localPoint = Vector2.zero;
212 Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, worldPoint);
213 RectTransformUtility.ScreenPointToLocalPointInRectangle(container, screenPoint, canvas.worldCamera, out localPoint);
214 return localPoint;
215 }
216
217 public static T RandomEnum<T>()
218 {
219 Array values = Enum.GetValues(typeof(T));
220 return (T)values.GetValue(new System.Random().Next(values.Length));
221 }
222
223 public static List<T> EnumToList<T>() where T : Enum
224 {
225 return Enum.GetValues(typeof(T)).Cast<T>().ToList();
226 }
227
228 public static void ShowExplorer(string itemPath, bool selectFirstFile = false)
229 {
230 if (selectFirstFile)
231 {
232 FileInfo[] files = new DirectoryInfo(itemPath).GetFiles();
233 if (files.Length != 0)
234 {
235 itemPath = itemPath + "/" + files[0].Name;
236 }
237 }
238 itemPath = itemPath.Replace("/", "\\");
239 Process.Start("explorer.exe", "/select," + itemPath);
240 }
241
242 public static void Run(string itemPath)
243 {
244 Process.Start(itemPath);
245 }
246
247 public static T[,] ResizeArray<T>(T[,] original, int x, int y, Func<int, int, T> func)
248 {
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++)
253 {
254 for (int j = 0; j < y; j++)
255 {
256 if (i >= num || j >= num2)
257 {
258 array[i, j] = func(i, j);
259 }
260 else
261 {
262 array[i, j] = original[i, j];
263 }
264 }
265 }
266 return array;
267 }
268}
VectorDir
Definition: VectorDir.cs:2
Canvas canvas
Definition: BaseCore.cs:34
static BaseCore Instance
Definition: BaseCore.cs:11
Definition: Util.cs:10
static Vector2 ConvertAxis(Vector2 v)
Definition: Util.cs:157
static void Run(string itemPath)
Definition: Util.cs:242
static Util()
Definition: Util.cs:15
static Vector2 WorldToUIPos(Vector3 worldPoint, RectTransform container)
Definition: Util.cs:209
static void DestroyChildren(Transform trans)
Definition: Util.cs:82
static VectorDir GetVectorDir(Vector3 normal)
Definition: Util.cs:132
static void RebuildLayoutInParents(Component c, Component root)
Definition: Util.cs:90
static int Distance(int pX1, int pY1, int pX2, int pY2)
Definition: Util.cs:24
static float GetAngle(float x, float y)
Definition: Util.cs:127
static Canvas canvas
Definition: Util.cs:13
static float GetAngle(Vector3 self, Vector3 target)
Definition: Util.cs:121
static Transform Instantiate(string path, Component parent=null)
Definition: Util.cs:67
static Color[] alphaMap
Definition: Util.cs:11
static void SetRectToFitScreen(RectTransform rect)
Definition: Util.cs:101
static void ShowExplorer(string itemPath, bool selectFirstFile=false)
Definition: Util.cs:228
static void ClampToScreen(RectTransform rect, int margin=0)
Definition: Util.cs:110