-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetInstance.cs
More file actions
88 lines (74 loc) · 3.18 KB
/
AssetInstance.cs
File metadata and controls
88 lines (74 loc) · 3.18 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DuskModules.ScriptableObjects {
/// <summary> Asset Instance is a scriptable object which is linked to a BehaviourInstance in the scene.
/// By calling the AssetInstance within the AssetDatabase, code can find the scene instance belonging to it and call upon it's code. </summary>
public class AssetInstance<A, B> : ScriptableObject where A : AssetInstance<A, B> where B : BehaviourInstance<B, A> {
/// <summary> Dictionary of all auto instances. Key = prefab, value = instance of that prefab. </summary>
protected static Dictionary<A, B> _instances;
/// <summary> Dictionary of all auto instances. Key = prefab, value = instance of that prefab. </summary>
public static Dictionary<A, B> instances {
get {
if (_instances == null) _instances = new Dictionary<A, B>();
return _instances;
}
}
/// <summary> Upon getting the instance </summary>
public B instance => CreateInstance();
/// <summary> Locates or creates the instance </summary>
public B CreateInstance() {
B instance = null;
A self = (A)this;
if (instances.ContainsKey(self)) {
instance = instances[self];
if (instance == null || instance.gameObject == null) instances.Remove(self);
else return instance;
}
// Not present within dictionary, find or create it.
instance = GetSceneInstance();
if (instance == null)
instance = CreateSceneInstance();
if (instance != null)
instances.Add(self, instance);
return instance;
}
/// <summary> Finds and returns the BehaviourInstance present in the scene </summary>
/// <returns> The scene BehaviourInstance </returns>
protected B GetSceneInstance() {
B[] foundInstances = FindObjectsOfType<B>();
for (int i = 0; i < foundInstances.Length; i++) {
if (foundInstances[i].asset == this) {
return foundInstances[i];
}
}
return null;
}
/// <summary> Creates a new BehaviourInstance within the current scene </summary>
/// <returns> The scene BehaviourInstance </returns>
protected B CreateSceneInstance() {
if (CreationAllowed()) {
B instance = InstantiateInstance();
instance.SetAsset((A)this);
return instance;
}
return null;
}
/// <summary> Actually creates the BehaviourInstance. Can be modified by inheriting code. </summary>
/// <returns> The scene BehaviourInstance </returns>
protected virtual B InstantiateInstance() {
GameObject createdObject = new GameObject(name);
return createdObject.AddComponent<B>();
}
/// <summary> Called when the instance has been destroyed. </summary>
/// <param name="instance"> The instance to remove </param>
public void Remove(B removeInstance) {
if (instance == removeInstance) {
instances.Remove((A)this);
}
}
/// <summary> Whether creation is allowed. If it returns true, a new instance is created if none is found. </summary>
/// <returns> Whether creation is allowed. </returns>
protected virtual bool CreationAllowed() { return true; }
}
}