Skip to content

Commit ccd0fc7

Browse files
committed
adding helpers and serializables
1 parent 1716534 commit ccd0fc7

File tree

2 files changed

+444
-0
lines changed

2 files changed

+444
-0
lines changed

Helpers/Helpers.cs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
using SerializableTypes;
7+
using Random = System.Random;
8+
using System.Linq;
9+
10+
namespace Helpers {
11+
[System.Serializable]
12+
public static class HelperMethods {
13+
//public static Random rng = new Random();
14+
15+
public static bool HasComponent<T> (GameObject obj, out T toReturn) {
16+
toReturn = obj.GetComponent<T>();
17+
return toReturn != null;
18+
}
19+
public static bool HasComponent<T> (GameObject obj) {
20+
return obj.GetComponent<T>() != null;
21+
}
22+
public static bool HasComponent<T> (Transform t, out T toReturn) {
23+
return HasComponent<T>(t.gameObject, out toReturn);
24+
}
25+
public static bool HasComponent<T> (Transform t) {
26+
return HasComponent<T>(t.gameObject);
27+
}
28+
29+
public static float Map(float value, float from1, float to1, float from2, float to2) {
30+
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
31+
}
32+
33+
// Adapted from Fisher-Yates Shuffle, code adopted from https://stackoverflow.com/questions/273313/randomize-a-listt
34+
public static List<T> Shuffle<T>(this List<T> list) {
35+
Random rng = new Random();
36+
List<T> newList = new List<T>(list);
37+
int n = newList.Count;
38+
while (n > 1) {
39+
n--;
40+
int k = rng.Next(n + 1);
41+
T value = newList[k];
42+
newList[k] = newList[n];
43+
newList[n] = value;
44+
}
45+
return newList;
46+
}
47+
public static List<T> Flatten2D<T>(this List<List<T>> list) {
48+
List<T> flatten = new List<T>();
49+
foreach(List<T> nested in list) {
50+
foreach(T item in nested) {
51+
flatten.Add(item);
52+
}
53+
}
54+
return flatten;
55+
}
56+
public static List<T2> Flatten2D<T1,T2>(this Dictionary<T1, List<T2>> dict) {
57+
List<T2> flatten = new List<T2>();
58+
foreach(KeyValuePair<T1, List<T2>> kvp in dict) {
59+
foreach(T2 item in kvp.Value) {
60+
flatten.Add(item);
61+
}
62+
}
63+
return flatten;
64+
}
65+
66+
// Adapted from: https://stackoverflow.com/questions/19141259/how-to-enqueue-a-list-of-items-in-c
67+
public static void AddRange<T>(this Queue<T> queue, IEnumerable<T> enu) {
68+
foreach (T obj in enu)
69+
queue.Enqueue(obj);
70+
}
71+
72+
public static bool Compare(this SVector3 original, SVector3 other) {
73+
return original.x == other.x && original.y == other.y && original.z == other.z;
74+
}
75+
public static bool Compare(this SVector3 original, Vector3 other) {
76+
return original.x == other.x && original.y == other.y && original.z == other.z;
77+
}
78+
public static bool Compare(this SVector4 original, SVector4 other) {
79+
return original.x == other.x && original.y == other.y && original.z == other.z && original.w == other.w;
80+
}
81+
public static bool Compare(this SVector4 original, Vector4 other) {
82+
return original.x == other.x && original.y == other.y && original.z == other.z && original.w == other.w;
83+
}
84+
85+
// Adapted from: https://answers.unity.com/questions/288338/how-do-i-compare-quaternions.html
86+
public static bool Compare(this Quaternion original, Quaternion other, float acceptableRange) {
87+
return 1 - Mathf.Abs(Quaternion.Dot(original, other)) < acceptableRange;
88+
}
89+
90+
// Adapted from: https://stackoverflow.com/questions/1792470/subset-of-array-in-c-sharp
91+
public static T[] RangeSubset<T>(this T[] array, int startIndex, int length) {
92+
T[] subset = new T[length];
93+
Array.Copy(array, startIndex, subset, 0, length);
94+
return subset;
95+
}
96+
}
97+
98+
[System.Serializable]
99+
public class SaveSystemMethods {
100+
public static string GetSaveLoadDirectory(string path = "") {
101+
return (path != null && path.Length > 0)
102+
? (path.EndsWith("/"))
103+
? Application.dataPath + "/" + path
104+
: Application.dataPath + "/" + path + "/"
105+
: Application.dataPath + "/";
106+
}
107+
public static bool CheckDirectoryExists(string dirPath) {
108+
return Directory.Exists(dirPath);
109+
}
110+
public static bool CheckOrCreateDirectory(string dirPath) {
111+
if (!CheckDirectoryExists(dirPath)) Directory.CreateDirectory(dirPath);
112+
return true;
113+
}
114+
public static bool CheckFileExists(string filePath) {
115+
return File.Exists(filePath);
116+
}
117+
118+
public static string ConvertToJSON<T>(T data) {
119+
return JsonUtility.ToJson(data, true);
120+
}
121+
public static T ConvertFromJSON<T>(string data) {
122+
return JsonUtility.FromJson<T>(data);
123+
}
124+
public static bool SaveJSON(string filePath, string json) {
125+
if (filePath.EndsWith(".json")) File.WriteAllText(filePath, json);
126+
else File.WriteAllText(filePath + ".json", json);
127+
return true;
128+
}
129+
public static bool LoadJSON<T>(string filePath, out T output) {
130+
string actualFilePath = (filePath.EndsWith(".json")) ? filePath : filePath+".json";
131+
if (CheckFileExists(actualFilePath)) {
132+
string fileContents = File.ReadAllText(actualFilePath);
133+
output = ConvertFromJSON<T>(fileContents);
134+
return true;
135+
} else {
136+
output = default(T);
137+
return false;
138+
}
139+
}
140+
public static bool SaveCSV<T>(string filePath, List<string> header, List<T> data) {
141+
string p = (filePath.EndsWith(".csv")) ? filePath : filePath+".csv";
142+
StreamWriter writer = new StreamWriter(p);
143+
writer.WriteLine(string.Join(",", header));
144+
foreach(T line in data) {
145+
List<string> lineContent = new List<string>();
146+
foreach(string headerVal in header) {
147+
lineContent.Add(line.GetType().GetField(headerVal).GetValue(line).ToString());
148+
}
149+
writer.WriteLine(string.Join(",",lineContent));
150+
}
151+
writer.Flush();
152+
writer.Close();
153+
return true;
154+
}
155+
156+
public static string[] ReadCSV(TextAsset asset) {
157+
return asset.text.Split(new string[] {",","\n"}, StringSplitOptions.None);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)