-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSORefer.cs
More file actions
73 lines (57 loc) · 2.06 KB
/
SORefer.cs
File metadata and controls
73 lines (57 loc) · 2.06 KB
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
using System.Collections.Generic;
using UnityEngine;
public class SORefer : MonoBehaviour {
private static SORefer control;
[SerializeField]
private ScriptableObject[] allObjects;
private Dictionary<int, ScriptableObject> objects;
private void Awake() {
control = this;
#if UNITY_EDITOR
UpdateList();
#endif
BuildDictionary();
}
#if UNITY_EDITOR
[ContextMenu("Update List")]
private void UpdateList() {
allObjects = null;
string[] guids = UnityEditor.AssetDatabase.FindAssets($"t:ScriptableObject", new string[] { "Assets/Scriptable Objects" });
allObjects = new ScriptableObject[guids.Length];
for (int i = 0; i < guids.Length; i++)
allObjects[i] = UnityEditor.AssetDatabase.LoadAssetAtPath<ScriptableObject>(UnityEditor.AssetDatabase.GUIDToAssetPath(guids[i]));
}
[UnityEditor.MenuItem("NOÉ'S FUCK SHIT/Update SORefer")]
private static void UpdateSORefer() => GameObject.FindObjectOfType<SORefer>().UpdateList();
#endif
public static int GetCode<T>(T scriptableObject)where T : ScriptableObject {
return HashFromNameAndType(scriptableObject.name, typeof(T));
}
public static int[] GetCodes<T>(T[] scriptableObjects)where T : ScriptableObject {
if (scriptableObjects == null)
return null;
int[] results = new int[scriptableObjects.Length];
for (int i = 0; i < results.Length; i++)
results[i] = GetCode<T>(scriptableObjects[i]);
return results;
}
public static T GetObject<T>(int code)where T : ScriptableObject {
return (T)control.objects[code];
}
public static T[] GetObjects<T>(int[] codes)where T : ScriptableObject {
if (codes == null)
return null;
T[] results = new T[codes.Length];
for (int i = 0; i < results.Length; i++)
results[i] = GetObject<T>(codes[i]);
return results;
}
private void BuildDictionary() {
objects = new Dictionary<int, ScriptableObject>();
foreach (ScriptableObject so in allObjects)
objects.Add(HashFromNameAndType(so.name, so.GetType()), so);
}
private static int HashFromNameAndType(string name, System.Type type) {
return (name + type.ToString()).GetHashCode();
}
}