Skip to content

Commit 960152c

Browse files
author
Bruno Mikoski
committed
fix: generation of indirect access
1 parent eb7973e commit 960152c

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

Scripts/Editor/CreateCollectionWizzard.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ private void CreateIndirectAccess()
230230
CodeGenerationUtility.CreateNewEmptyScript($"{collectableName}IndirectReference",
231231
folderPath,
232232
TargetNameSpace,
233-
string.Empty,
234-
$"public sealed class {collectableName}IndirectReference : CollectableIndirectReference<{collectableName}>",
235-
new []{typeof(CollectableScriptableObject).Namespace, TargetNameSpace});
233+
"[Serializable]",
234+
$"public sealed class {collectableName}IndirectReference : CollectableIndirectReference<{collectableName}>",
235+
typeof(CollectableScriptableObject).Namespace, TargetNameSpace, "System");
236236
}
237237

238238
private string CreateCollectionObject()

Scripts/Editor/Utils/CodeGenerationUtility.cs

Lines changed: 3 additions & 3 deletions
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)+"{");

Scripts/Runtime/CollectableIndirectReference.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace BrunoMikoski.ScriptableObjectCollections
55
{
66
[Serializable]
7-
public abstract class CollectableIndirectReference
7+
public class CollectableIndirectReference
88
{
99
#pragma warning disable 0649
1010
[SerializeField]
@@ -16,6 +16,12 @@ public abstract class CollectableIndirectReference
1616

1717
[SerializeField]
1818
protected string collectionGUID;
19+
20+
public virtual void FromCollectable(CollectableScriptableObject collectableScriptableObject)
21+
{
22+
collectableGUID = collectableScriptableObject.GUID;
23+
collectionGUID = collectableScriptableObject.Collection.GUID;
24+
}
1925
}
2026

2127
[Serializable]
@@ -27,26 +33,34 @@ public class CollectableIndirectReference<TObject> : CollectableIndirectReferenc
2733
private TObject editorAsset;
2834
#endif
2935
[NonSerialized]
30-
private TObject cachedITem;
31-
public new TObject Item
36+
private TObject cachedRef;
37+
public TObject Ref
3238
{
3339
get
3440
{
35-
if (cachedITem != null)
36-
return cachedITem;
41+
if (cachedRef != null)
42+
return cachedRef;
3743

3844
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectableGUID,
3945
out ScriptableObjectCollection<TObject> collection))
4046
{
4147
if (collection.TryGetCollectableByGUID(collectionGUID,
4248
out CollectableScriptableObject collectable))
4349
{
44-
cachedITem = collectable as TObject;
50+
cachedRef = collectable as TObject;
4551
}
4652
}
4753

48-
return cachedITem;
54+
return cachedRef;
4955
}
5056
}
57+
58+
public override void FromCollectable(CollectableScriptableObject collectableScriptableObject)
59+
{
60+
base.FromCollectable(collectableScriptableObject);
61+
#if UNITY_EDITOR
62+
editorAsset = collectableScriptableObject as TObject;
63+
#endif
64+
}
5165
}
5266
}

0 commit comments

Comments
 (0)