-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
175 lines (153 loc) · 5.63 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
namespace OxUE
{
/// <summary>
/// Useful functions
/// </summary>
public static class Helpers
{
public static Texture2D FlipTexture(Texture2D original)
{
if (original != null)
{
Texture2D flipped = new Texture2D(original.width, original.height);
int xN = original.width;
int yN = original.height;
for (int i = 0; i < xN; i++)
{
for (int j = 0; j < yN; j++)
{
flipped.SetPixel(i, yN - j - 1, original.GetPixel(i, j));
}
}
flipped.Apply();
return flipped;
}
else
{
return new Texture2D(2, 2);
}
}
public static IEnumerator AsIEnumerator(this Task task)
{
while (!task.IsCompleted)
{
yield return null;
}
if (!task.IsFaulted) yield break;
if (task.Exception != null) throw task.Exception;
}
public static string GetProjectDirectory(string url, bool createMissing = false)
{
url = Application.dataPath + "/../" + url;
if (createMissing)
CheckDirectory(url);
//Debug.Log(url);
return url;
}
public static void CheckDirectory(string dirName)
{
if (!Directory.Exists(dirName))
Directory.CreateDirectory(dirName);
}
public static string GetGameVersion()
{
return Application.version;
}
public static Color HexToColor(string hex)
{
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF
byte a = 255;//assume fully visible unless specified in hex
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
//Only use alpha if the string has enough characters
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
}
return new Color32(r, g, b, a);
}
public static string ColorToHex(Color32 color)
{
string hex = "#" + color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2") + color.a.ToString("X2");
return hex;
}
public static float DbToNormal(float bd)
{
return bd <= -80 ? 0 : (float)Math.Pow(Math.E, (bd / 20.0d));
}
public static float NormalToDb(float normal)
{
return normal > 0 ? (Mathf.Log(normal) * 20) : -80;
}
public static string RemoveHTMLTagsFromText(string inputString)
{
string HTML_TAG_PATTERN = "<.*?>";
return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
}
public static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
public static object CreateInstance(string strFullyQualifiedName, object[] paramArray = null)
{
Type type = Type.GetType(strFullyQualifiedName);
if (type != null)
{
if (paramArray != null && paramArray.Length > 0)
return Activator.CreateInstance(type, args: paramArray);
return Activator.CreateInstance(type);
}
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
type = asm.GetType(strFullyQualifiedName);
if (type != null)
{
if (paramArray != null && paramArray.Length > 0)
return Activator.CreateInstance(type, args: paramArray);
return Activator.CreateInstance(type);
}
}
return null;
}
public static Color ColorGradient(Color from, Color to, float progress)
{
Gradient g;
GradientColorKey[] gck;
GradientAlphaKey[] gak;
g = new Gradient();
gck = new GradientColorKey[2];
gck[0].color = to;
gck[0].time = 0.0F;
gck[1].color = from;
gck[1].time = 1.0F;
gak = new GradientAlphaKey[2];
gak[0].alpha = 1.0F;
gak[0].time = 0.0F;
gak[1].alpha = 1.0F;
gak[1].time = 1.0F;
g.SetKeys(gck, gak);
return g.Evaluate(progress);
}
}
}