Skip to content

Commit fcc98d5

Browse files
authored
Merge pull request #23 from badawe/feature/add-indirect-reference
add: indirect reference to the objects
2 parents f3ddd86 + 87ccc17 commit fcc98d5

9 files changed

+157
-4
lines changed

CHANGELOG.MD

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.2.0]
8+
## Changed
9+
- Fixed issue with the wizzard that was not generating the target Scriptable Object
10+
11+
## Added
12+
- New type of indirect access to collectable items, this allows you use the regular editor reference, but without storing the reference, so without creating a relationship between the items, and can be lazy loaded when is needed, like addressables references for isntance.
13+
- Also added an option on the wizzard to automatically create the IndirectReference for every collectable item
714

815
## [1.1.9]
916
## Changed
@@ -100,6 +107,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
100107
## [Unreleased]
101108
- Add a setup wizzard for first time settings creation
102109

110+
[1.2.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.0
103111
[1.1.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.9
104112
[1.1.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.8
105113
[1.1.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using BrunoMikoski.ScriptableObjectCollections.Core;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace BrunoMikoski.ScriptableObjectCollections
6+
{
7+
[CustomPropertyDrawer(typeof(CollectableIndirectReference), true)]
8+
public sealed class CollectableIndirectReferenceDrawer : PropertyDrawer
9+
{
10+
private const string EDITOR_ASSET_PROPERTY_PATH = "editorAsset";
11+
private const string COLLECTABLE_GUID_PROPERTY_PATH = "collectableGUID";
12+
private const string COLLECTION_GUID_PROPERTY_PATh = "collectionGUID";
13+
14+
15+
private SerializedProperty editorAssetproperty;
16+
private SerializedProperty collectableGUIDProperty;
17+
private SerializedProperty collectionGUIDProperty;
18+
19+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
20+
{
21+
editorAssetproperty = property.FindPropertyRelative(EDITOR_ASSET_PROPERTY_PATH);
22+
collectableGUIDProperty = property.FindPropertyRelative(COLLECTABLE_GUID_PROPERTY_PATH);
23+
collectionGUIDProperty = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);
24+
25+
using (EditorGUI.ChangeCheckScope scope = new EditorGUI.ChangeCheckScope())
26+
{
27+
EditorGUI.PropertyField(position, editorAssetproperty, label, true);
28+
29+
if (scope.changed)
30+
{
31+
string collectableGUID = string.Empty;
32+
string collectionGUID = string.Empty;
33+
34+
if (editorAssetproperty.objectReferenceValue != null && editorAssetproperty.objectReferenceValue is CollectableScriptableObject collectable)
35+
{
36+
collectableGUID = collectable.GUID;
37+
collectionGUID = collectable.Collection.GUID;
38+
}
39+
collectableGUIDProperty.stringValue = collectableGUID;
40+
collectionGUIDProperty.stringValue = collectionGUID;
41+
}
42+
}
43+
}
44+
}
45+
}

Scripts/Editor/CollectableIndirectReferenceDrawer.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/CreateCollectionWizzard.cs

+21
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private static string LastGeneratedCollectionScriptPath
124124
private string collectionName = "Collection";
125125
private string collectableName = "Collectable";
126126
private static string targetFolder;
127+
private bool generateIndirectAccess = true;
127128

128129
public static CreateCollectionWizzard GetWindowInstance()
129130
{
@@ -151,6 +152,8 @@ private void OnGUI()
151152
collectableName = EditorGUILayout.TextField("Collectable Name", collectableName);
152153

153154
collectionName = EditorGUILayout.TextField("Collection Name", collectionName);
155+
156+
generateIndirectAccess = EditorGUILayout.Toggle("Generate Indirect Access", generateIndirectAccess);
154157
}
155158
using (new EditorGUILayout.VerticalScope("Box"))
156159
{
@@ -206,14 +209,32 @@ private void CreateNewCollection()
206209
scriptsGenerated |= CreateCollectableScript();
207210
scriptsGenerated |= CreateCollectionScript();
208211

212+
if (generateIndirectAccess)
213+
CreateIndirectAccess();
214+
WaitingRecompileForContinue = true;
209215

216+
LastCollectionScriptableObjectPath = CreateCollectionObject();
210217
AssetDatabase.SaveAssets();
211218
AssetDatabase.Refresh();
212219

213220
if (!scriptsGenerated)
214221
OnAfterScriptsReloading();
215222
}
216223

224+
private void CreateIndirectAccess()
225+
{
226+
string folderPath = AssetDatabase.GetAssetPath(ScriptsFolder);
227+
if (createFoldForThisCollectionScripts)
228+
folderPath = Path.Combine(folderPath, $"{collectionName}");
229+
230+
CodeGenerationUtility.CreateNewEmptyScript($"{collectableName}IndirectReference",
231+
folderPath,
232+
TargetNameSpace,
233+
"[Serializable]",
234+
$"public sealed class {collectableName}IndirectReference : CollectableIndirectReference<{collectableName}>",
235+
typeof(CollectableScriptableObject).Namespace, TargetNameSpace, "System");
236+
}
237+
217238
private string CreateCollectionObject()
218239
{
219240
ScriptableObjectCollection targetCollection = CreateInstance<ScriptableObjectCollection>();

Scripts/Editor/Utils/CodeGenerationUtility.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace BrunoMikoski.ScriptableObjectCollections
1111
{
1212
public static class CodeGenerationUtility
1313
{
14-
public static bool CreateNewEmptyScript(string fileName, string parentFolder, string nameSpace, string createAssetMenuInput, string classDeclarationString, params string[] directives)
14+
public static bool CreateNewEmptyScript(string fileName, string parentFolder, string nameSpace, string classAttributes, string classDeclarationString, params string[] directives)
1515
{
1616
AssetDatabaseUtils.CreatePathIfDontExist(parentFolder);
1717
string finalFilePath = Path.Combine(parentFolder, $"{fileName}.cs");
@@ -37,8 +37,8 @@ public static bool CreateNewEmptyScript(string fileName, string parentFolder, st
3737
indentation++;
3838
}
3939

40-
if (!string.IsNullOrEmpty(createAssetMenuInput))
41-
writer.WriteLine($"{GetIndentation(indentation)}{createAssetMenuInput}");
40+
if (!string.IsNullOrEmpty(classAttributes))
41+
writer.WriteLine($"{GetIndentation(indentation)}{classAttributes}");
4242

4343
writer.WriteLine($"{GetIndentation(indentation)}{classDeclarationString}");
4444
writer.WriteLine(GetIndentation(indentation)+"{");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace BrunoMikoski.ScriptableObjectCollections
5+
{
6+
[Serializable]
7+
public class CollectableIndirectReference
8+
{
9+
#pragma warning disable 0649
10+
[SerializeField]
11+
private string name;
12+
#pragma warning restore 0649
13+
14+
[SerializeField]
15+
protected string collectableGUID = String.Empty;
16+
17+
[SerializeField]
18+
protected string collectionGUID;
19+
}
20+
21+
[Serializable]
22+
public class CollectableIndirectReference<TObject> : CollectableIndirectReference
23+
where TObject : CollectableScriptableObject
24+
{
25+
#if UNITY_EDITOR
26+
[SerializeField]
27+
private TObject editorAsset;
28+
#endif
29+
[NonSerialized]
30+
private TObject cachedRef;
31+
public TObject Ref
32+
{
33+
get
34+
{
35+
if (cachedRef != null)
36+
return cachedRef;
37+
38+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectableGUID,
39+
out ScriptableObjectCollection<TObject> collection))
40+
{
41+
if (collection.TryGetCollectableByGUID(collectionGUID,
42+
out CollectableScriptableObject collectable))
43+
{
44+
cachedRef = collectable as TObject;
45+
}
46+
}
47+
48+
return cachedRef;
49+
}
50+
}
51+
52+
public void FromCollectable(CollectableScriptableObject collectableScriptableObject)
53+
{
54+
collectableGUID = collectableScriptableObject.GUID;
55+
collectionGUID = collectableScriptableObject.Collection.GUID;
56+
#if UNITY_EDITOR
57+
editorAsset = collectableScriptableObject as TObject;
58+
#endif
59+
}
60+
}
61+
}

Scripts/Runtime/CollectableIndirectReference.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Runtime/CollectionsRegistry.cs

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ public bool TryGetCollectionByGUID(string targetGUID, out ScriptableObjectCollec
129129
return false;
130130
}
131131

132+
public bool TryGetCollectionByGUID<T>(string targetGUID, out ScriptableObjectCollection<T> resultCollection) where T : CollectableScriptableObject
133+
{
134+
if (TryGetCollectionByGUID(targetGUID, out ScriptableObjectCollection foundCollection))
135+
{
136+
resultCollection = foundCollection as ScriptableObjectCollection<T>;
137+
return true;
138+
}
139+
140+
resultCollection = null;
141+
return false;
142+
}
143+
132144
public void DeleteCollection(ScriptableObjectCollection collection)
133145
{
134146
if (Application.isPlaying)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.brunomikoski.scriptableobjectcollection",
33
"displayName": "Scriptable Object Collection",
4-
"version": "1.1.9",
4+
"version": "1.2.0",
55
"unity": "2018.4",
66
"description": "Scriptable Object Collection",
77
"keywords": [

0 commit comments

Comments
 (0)