Skip to content

Commit ffcd6f2

Browse files
authored
Merge pull request #24 from badawe/feature/fix-generation-of-indirect
Feature/fix generation of indirect
2 parents fcc98d5 + e7fa489 commit ffcd6f2

6 files changed

+105
-28
lines changed

CHANGELOG.MD

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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+
78
## [1.2.0]
89
## Changed
910
- Fixed issue with the wizzard that was not generating the target Scriptable Object
@@ -107,6 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
107108
## [Unreleased]
108109
- Add a setup wizzard for first time settings creation
109110

111+
[1.2.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.1
110112
[1.2.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.0
111113
[1.1.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.9
112114
[1.1.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.8

Scripts/Editor/CollectableIndirectReferenceDrawer.cs

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BrunoMikoski.ScriptableObjectCollections.Core;
21
using UnityEditor;
32
using UnityEngine;
43

@@ -7,31 +6,48 @@ namespace BrunoMikoski.ScriptableObjectCollections
76
[CustomPropertyDrawer(typeof(CollectableIndirectReference), true)]
87
public sealed class CollectableIndirectReferenceDrawer : PropertyDrawer
98
{
10-
private const string EDITOR_ASSET_PROPERTY_PATH = "editorAsset";
9+
private const string OBJECT_ASSET_PROPERTY_PATH = "editorAsset";
1110
private const string COLLECTABLE_GUID_PROPERTY_PATH = "collectableGUID";
1211
private const string COLLECTION_GUID_PROPERTY_PATh = "collectionGUID";
1312

1413

15-
private SerializedProperty editorAssetproperty;
14+
private SerializedProperty objectAssetProperty;
1615
private SerializedProperty collectableGUIDProperty;
1716
private SerializedProperty collectionGUIDProperty;
1817

18+
private CollectableScriptableObject collectableScriptableObject;
19+
1920
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
2021
{
21-
editorAssetproperty = property.FindPropertyRelative(EDITOR_ASSET_PROPERTY_PATH);
22+
objectAssetProperty = property.FindPropertyRelative(OBJECT_ASSET_PROPERTY_PATH);
2223
collectableGUIDProperty = property.FindPropertyRelative(COLLECTABLE_GUID_PROPERTY_PATH);
2324
collectionGUIDProperty = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);
2425

26+
if (objectAssetProperty.exposedReferenceValue == null
27+
&& !string.IsNullOrEmpty(collectableGUIDProperty.stringValue)
28+
&& !string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
29+
{
30+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDProperty.stringValue,
31+
out ScriptableObjectCollection collection))
32+
{
33+
if (collection.TryGetCollectableByGUID(collectableGUIDProperty.stringValue,
34+
out CollectableScriptableObject collectable))
35+
{
36+
objectAssetProperty.objectReferenceValue = collectable;
37+
}
38+
}
39+
}
40+
2541
using (EditorGUI.ChangeCheckScope scope = new EditorGUI.ChangeCheckScope())
2642
{
27-
EditorGUI.PropertyField(position, editorAssetproperty, label, true);
43+
EditorGUI.PropertyField(position, objectAssetProperty, label, true);
2844

2945
if (scope.changed)
3046
{
3147
string collectableGUID = string.Empty;
3248
string collectionGUID = string.Empty;
3349

34-
if (editorAssetproperty.objectReferenceValue != null && editorAssetproperty.objectReferenceValue is CollectableScriptableObject collectable)
50+
if (objectAssetProperty.objectReferenceValue != null && objectAssetProperty.objectReferenceValue is CollectableScriptableObject collectable)
3551
{
3652
collectableGUID = collectable.GUID;
3753
collectionGUID = collectable.Collection.GUID;

Scripts/Editor/CollectableScriptableObjectPropertyDrawer.cs

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ private void Initialize(SerializedProperty property)
145145
collectableType = arrayOrListType;
146146
else
147147
collectableType = fieldInfo.FieldType;
148-
149148

150149
if (!CollectionsRegistry.Instance.TryGetCollectionForType(collectableType,
151150
out ScriptableObjectCollection resultCollection))

Scripts/Editor/CreateCollectionWizzard.cs

+33-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Linq;
35
using UnityEditor;
46
using UnityEditor.Callbacks;
57
using UnityEditor.Compilation;
@@ -227,12 +229,37 @@ private void CreateIndirectAccess()
227229
if (createFoldForThisCollectionScripts)
228230
folderPath = Path.Combine(folderPath, $"{collectionName}");
229231

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");
232+
string fileName = $"{collectableName}IndirectReference";
233+
234+
AssetDatabaseUtils.CreatePathIfDontExist(folderPath);
235+
using (StreamWriter writer = new StreamWriter(Path.Combine(folderPath, $"{fileName}.cs")))
236+
{
237+
int indentation = 0;
238+
List<string> directives = new List<string>();
239+
directives.Add(typeof(CollectableScriptableObject).Namespace);
240+
directives.Add(TargetNameSpace);
241+
directives.Add("System");
242+
directives.Add("UnityEngine");
243+
244+
CodeGenerationUtility.AppendHeader(writer, ref indentation, TargetNameSpace, "[Serializable]",
245+
$"public sealed class {collectableName}IndirectReference : CollectableIndirectReference<{collectableName}>",
246+
directives.Distinct().ToArray());
247+
248+
CodeGenerationUtility.AppendLine(writer, 0,
249+
$"#if UNITY_EDITOR");
250+
251+
CodeGenerationUtility.AppendLine(writer, indentation,
252+
$"[SerializeField]");
253+
254+
CodeGenerationUtility.AppendLine(writer, indentation,
255+
$"private {collectableName} editorAsset;");
256+
257+
CodeGenerationUtility.AppendLine(writer, 0,
258+
$"#endif");
259+
260+
indentation--;
261+
CodeGenerationUtility.AppendFooter(writer, ref indentation, TargetNameSpace);
262+
}
236263
}
237264

238265
private string CreateCollectionObject()

Scripts/Editor/Utils/CodeGenerationUtility.cs

+42-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static string GetIndentation(int identation)
6565
return stringBuilder.ToString();
6666
}
6767

68-
public static void AppendHeader(StreamWriter writer, ref int identation, string nameSpace, string className,
68+
public static void AppendHeader(StreamWriter writer, ref int identation, string nameSpace, string classAttributes, string className,
6969
bool isPartial, bool isStatic, params string[] directives)
7070
{
7171
writer.WriteLine("// Automatically generated");
@@ -92,6 +92,9 @@ public static void AppendHeader(StreamWriter writer, ref int identation, string
9292
identation++;
9393
}
9494

95+
if (!string.IsNullOrEmpty(classAttributes))
96+
writer.WriteLine($"{GetIndentation(identation)}{classAttributes}");
97+
9598
string finalClassDeclaration = "";
9699
finalClassDeclaration += GetIndentation(identation);
97100
finalClassDeclaration += "public ";
@@ -109,6 +112,43 @@ public static void AppendHeader(StreamWriter writer, ref int identation, string
109112

110113
identation++;
111114
}
115+
116+
117+
public static void AppendHeader(StreamWriter writer, ref int identation, string nameSpace, string classAttributes, string classDeclaration, params string[] directives)
118+
{
119+
writer.WriteLine("// Automatically generated");
120+
writer.WriteLine("//");
121+
writer.WriteLine();
122+
for (int i = 0; i < directives.Length; i++)
123+
{
124+
string directive = directives[i];
125+
126+
if (string.IsNullOrEmpty(directive))
127+
continue;
128+
129+
writer.WriteLine($"using {directive};");
130+
}
131+
132+
writer.WriteLine();
133+
134+
bool hasNameSpace = !string.IsNullOrEmpty(nameSpace);
135+
if (hasNameSpace)
136+
{
137+
writer.WriteLine($"namespace {nameSpace}");
138+
writer.WriteLine("{");
139+
140+
identation++;
141+
}
142+
143+
if (!string.IsNullOrEmpty(classAttributes))
144+
writer.WriteLine($"{GetIndentation(identation)}{classAttributes}");
145+
146+
writer.WriteLine($"{GetIndentation(identation)}{classDeclaration}");
147+
writer.WriteLine(GetIndentation(identation) + "{");
148+
149+
identation++;
150+
}
151+
112152

113153
public static void AppendLine(StreamWriter writer, int identation, string input = "")
114154
{
@@ -157,7 +197,7 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
157197
directives.Add(collection.GetType().Namespace);
158198
directives.AddRange(GetCollectionDirectives(collection));
159199

160-
AppendHeader(writer, ref indentation, nameSpace,
200+
AppendHeader(writer, ref indentation, nameSpace,"",
161201
collection.GetCollectionType().Name, true, false, directives.Distinct().ToArray());
162202

163203
GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(collection);

Scripts/Runtime/CollectableIndirectReference.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@
44
namespace BrunoMikoski.ScriptableObjectCollections
55
{
66
[Serializable]
7-
public class CollectableIndirectReference
7+
public abstract class CollectableIndirectReference
88
{
99
#pragma warning disable 0649
1010
[SerializeField]
1111
private string name;
1212
#pragma warning restore 0649
1313

1414
[SerializeField]
15-
protected string collectableGUID = String.Empty;
15+
protected string collectableGUID;
1616

1717
[SerializeField]
1818
protected string collectionGUID;
19-
}
19+
}
2020

2121
[Serializable]
22-
public class CollectableIndirectReference<TObject> : CollectableIndirectReference
22+
public abstract class CollectableIndirectReference<TObject> : CollectableIndirectReference
2323
where TObject : CollectableScriptableObject
2424
{
25-
#if UNITY_EDITOR
26-
[SerializeField]
27-
private TObject editorAsset;
28-
#endif
2925
[NonSerialized]
3026
private TObject cachedRef;
3127
public TObject Ref
@@ -35,10 +31,10 @@ public TObject Ref
3531
if (cachedRef != null)
3632
return cachedRef;
3733

38-
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectableGUID,
34+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUID,
3935
out ScriptableObjectCollection<TObject> collection))
4036
{
41-
if (collection.TryGetCollectableByGUID(collectionGUID,
37+
if (collection.TryGetCollectableByGUID(collectableGUID,
4238
out CollectableScriptableObject collectable))
4339
{
4440
cachedRef = collectable as TObject;
@@ -53,9 +49,6 @@ public void FromCollectable(CollectableScriptableObject collectableScriptableObj
5349
{
5450
collectableGUID = collectableScriptableObject.GUID;
5551
collectionGUID = collectableScriptableObject.Collection.GUID;
56-
#if UNITY_EDITOR
57-
editorAsset = collectableScriptableObject as TObject;
58-
#endif
5952
}
6053
}
6154
}

0 commit comments

Comments
 (0)