Skip to content

Commit 6aaaa18

Browse files
committed
added content
1 parent 6423864 commit 6aaaa18

51 files changed

Lines changed: 957 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LICENSE.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SCRIPTABLE_OBJECTS.asset

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 554306acff7d34a16901ca6754dfbd82, type: 3}
13+
m_Name: SCRIPTABLE_OBJECTS
14+
m_EditorClassIdentifier:

SCRIPTABLE_OBJECTS.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/AssetInstance.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace DuskModules.ScriptableObjects {
6+
7+
/// <summary> Asset Instance is a scriptable object which is linked to a BehaviourInstance in the scene.
8+
/// By calling the AssetInstance within the AssetDatabase, code can find the scene instance belonging to it and call upon it's code. </summary>
9+
public class AssetInstance<A, B> : ScriptableObject where A : AssetInstance<A, B> where B : BehaviourInstance<B, A> {
10+
11+
/// <summary> Dictionary of all auto instances. Key = prefab, value = instance of that prefab. </summary>
12+
protected static Dictionary<A, B> _instances;
13+
/// <summary> Dictionary of all auto instances. Key = prefab, value = instance of that prefab. </summary>
14+
public static Dictionary<A, B> instances {
15+
get {
16+
if (_instances == null) _instances = new Dictionary<A, B>();
17+
return _instances;
18+
}
19+
}
20+
21+
/// <summary> Upon getting the instance </summary>
22+
public B instance => CreateInstance();
23+
24+
/// <summary> Locates or creates the instance </summary>
25+
public B CreateInstance() {
26+
B instance = null;
27+
A self = (A)this;
28+
if (instances.ContainsKey(self)) {
29+
instance = instances[self];
30+
if (instance == null || instance.gameObject == null) instances.Remove(self);
31+
else return instance;
32+
}
33+
34+
// Not present within dictionary, find or create it.
35+
instance = GetSceneInstance();
36+
if (instance == null)
37+
instance = CreateSceneInstance();
38+
39+
if (instance != null)
40+
instances.Add(self, instance);
41+
42+
return instance;
43+
}
44+
45+
/// <summary> Finds and returns the BehaviourInstance present in the scene </summary>
46+
/// <returns> The scene BehaviourInstance </returns>
47+
protected B GetSceneInstance() {
48+
B[] foundInstances = FindObjectsOfType<B>();
49+
for (int i = 0; i < foundInstances.Length; i++) {
50+
if (foundInstances[i].asset == this) {
51+
return foundInstances[i];
52+
}
53+
}
54+
return null;
55+
}
56+
57+
/// <summary> Creates a new BehaviourInstance within the current scene </summary>
58+
/// <returns> The scene BehaviourInstance </returns>
59+
protected B CreateSceneInstance() {
60+
if (CreationAllowed()) {
61+
B instance = InstantiateInstance();
62+
instance.SetAsset((A)this);
63+
return instance;
64+
}
65+
return null;
66+
}
67+
68+
/// <summary> Actually creates the BehaviourInstance. Can be modified by inheriting code. </summary>
69+
/// <returns> The scene BehaviourInstance </returns>
70+
protected virtual B InstantiateInstance() {
71+
GameObject createdObject = new GameObject(name);
72+
return createdObject.AddComponent<B>();
73+
}
74+
75+
/// <summary> Called when the instance has been destroyed. </summary>
76+
/// <param name="instance"> The instance to remove </param>
77+
public void Remove(B removeInstance) {
78+
if (instance == removeInstance) {
79+
instances.Remove((A)this);
80+
}
81+
}
82+
83+
/// <summary> Whether creation is allowed. If it returns true, a new instance is created if none is found. </summary>
84+
/// <returns> Whether creation is allowed. </returns>
85+
protected virtual bool CreationAllowed() { return true; }
86+
87+
}
88+
}

Scripts/AssetInstance.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/AssetInstancePrefab.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace DuskModules.ScriptableObjects {
6+
7+
/// <summary> Asset Instance Prefab is an Asset Instance that spawns a set prefab containing the required BehaviourInstance. </summary>
8+
public class AssetInstancePrefab<A, B> : AssetInstance<A,B> where A : AssetInstancePrefab<A, B> where B : BehaviourInstance<B, A> {
9+
10+
/// <summary> Prefab to instantiate. </summary>
11+
public B prefab;
12+
13+
/// <summary> Actually creates the BehaviourInstance. Can be modified by inheriting code. </summary>
14+
/// <returns> The scene BehaviourInstance </returns>
15+
protected override B InstantiateInstance() {
16+
return Instantiate(prefab);
17+
}
18+
19+
}
20+
}

Scripts/AssetInstancePrefab.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/AssetObject.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace DuskModules.ScriptableObjects {
6+
7+
/// <summary> Interface for any object that can be cached for AssetObjects </summary>
8+
public interface IAssetCache { }
9+
10+
/// <summary> A scriptable object capable of referencing a single cached data object, so it can keep runtime variables in memory to work with. </summary>
11+
/// <typeparam name="C"> Type of cached data </typeparam>
12+
public class AssetObject<C> : ScriptableObject where C : IAssetCache, new() {
13+
14+
/// <summary> Cache of this asset object type </summary>
15+
private static Dictionary<AssetObject<C>, C> caches;
16+
17+
/// <summary> Retrieves the cached data of this AssetObject </summary>
18+
public C cache {
19+
get {
20+
if (caches == null)
21+
caches = new Dictionary<AssetObject<C>, C>();
22+
23+
if (caches.ContainsKey(this)) {
24+
return caches[this];
25+
}
26+
else {
27+
C c = new C();
28+
caches.Add(this, c);
29+
CacheSetup();
30+
return c;
31+
}
32+
}
33+
}
34+
35+
/// <summary> Called when the cache has been accessed for the first time. </summary>
36+
protected virtual void CacheSetup() {
37+
38+
}
39+
40+
}
41+
}

Scripts/AssetObject.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)