Skip to content

Commit feab545

Browse files
committed
feat: Added ability to assign the created asset after assembly recompilation
1 parent b23dc17 commit feab545

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

Editor/ScriptableObjects/GenericSOCreator.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace GenericUnityObjects.Editor.ScriptableObjects
22
{
33
using System;
4-
using System.Linq;
54
using Cysharp.Threading.Tasks;
65
using GenericUnityObjects;
76
using GenericUnityObjects.Util;
@@ -60,7 +59,15 @@ async UniTaskVoid CreateAssetImpl()
6059
(Type genericSOType, string path) = PersistentStorage.GetGenericSODetails();
6160
var concreteType = BehavioursDatabase.GetConcreteType(genericSOType);
6261
await WaitUntilEditorInitialized();
63-
CreateAssetFromConcreteType(concreteType, asset => AssetDatabase.CreateAsset(asset, path));
62+
var createdAsset = CreateAssetFromConcreteType(concreteType, asset => AssetDatabase.CreateAsset(asset, path));
63+
64+
var property = PersistentStorage.GetSavedProperty();
65+
66+
if (property == null)
67+
return;
68+
69+
property.objectReferenceValue = createdAsset;
70+
property.serializedObject.ApplyModifiedProperties();
6471
}
6572
finally
6673
{
@@ -99,18 +106,20 @@ public static void CreateAssetInteractively(Type genericTypeWithoutArgs, Type[]
99106
AssetDatabase.Refresh();
100107
}
101108

102-
public static ScriptableObject CreateAssetAtPath(Type genericType, string path)
109+
public static ScriptableObject CreateAssetAtPath(SerializedProperty property, Type genericType, string path)
103110
{
104111
if (ScriptableObjectsDatabase.TryGetConcreteType(genericType, out var concreteType))
105112
{
106113
return CreateAssetFromConcreteType(concreteType, asset => AssetDatabase.CreateAsset(asset, path));
107114
}
108115

109116
PersistentStorage.SaveForScriptsReload(genericType, path);
117+
PersistentStorage.SaveForScriptsReload(property);
110118
PersistentStorage.ExecuteOnScriptsReload(FinishSOCreationAtPath);
111119

112120
ConcreteClassCreator<GenericScriptableObject>.CreateConcreteClass(genericType.GetGenericTypeDefinition(), genericType.GenericTypeArguments);
113121
AssetDatabase.Refresh();
122+
// At this point the domain is reloaded, so the method doesn't return.
114123
return null;
115124
}
116125

Editor/Util/PersistentStorage.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ internal class PersistentStorage : EditorOnlySingletonSO<PersistentStorage>, ICa
2121
[SerializeField] private GameObject _gameObject;
2222
[SerializeField] private TypeReference _genericBehaviourType;
2323

24+
[SerializeField] private int _instanceID;
25+
[SerializeField] private string _propertyPath;
26+
2427
[SerializeField] private MenuItemMethod[] _menuItemMethods = { };
2528

2629
public static MenuItemMethod[] MenuItemMethods
@@ -76,6 +79,12 @@ public static void SaveForScriptsReload(Type genericTypeToCreate, string fileNam
7679
Instance._fileName = fileName;
7780
}
7881

82+
public static void SaveForScriptsReload(SerializedProperty property)
83+
{
84+
Instance._instanceID = property.serializedObject.targetObject.GetInstanceID();
85+
Instance._propertyPath = property.propertyPath;
86+
}
87+
7988
public static void SaveForScriptsReload(GameObject gameObject, Type genericType)
8089
{
8190
Instance._gameObject = gameObject;
@@ -92,12 +101,31 @@ public static (GameObject gameObject, Type genericBehaviourType) GetGenericBehav
92101
return (Instance._gameObject, Instance._genericBehaviourType);
93102
}
94103

104+
public static SerializedProperty GetSavedProperty()
105+
{
106+
var targetObject = EditorUtility.InstanceIDToObject(Instance._instanceID);
107+
108+
if (targetObject == null)
109+
return null;
110+
111+
var serializedObject = new SerializedObject(targetObject);
112+
return serializedObject.FindProperty(Instance._propertyPath);
113+
}
114+
115+
public static void DisableFirstCompilation()
116+
{
117+
Instance._firstCompilation = false;
118+
EditorUtility.SetDirty(Instance);
119+
}
120+
95121
public static void Clear()
96122
{
97123
Instance._genericSOType = null;
98124
Instance._fileName = null;
99125
Instance._gameObject = null;
100126
Instance._genericBehaviourType = null;
127+
Instance._instanceID = 0;
128+
Instance._propertyPath = null;
101129
}
102130

103131
public void Initialize() { }

0 commit comments

Comments
 (0)