Unity - Refresh .asset after Destroying a Subasset

Viewed 15

So some context:
I'm currently working on an EditorWindow in Unity to make a custom Rule System. This Rule System is composed of a bunch of classes all deriving from ScriptableObject, so I can create Instances of them and still access them in that one window. (took me forever to get this to work, since I started without ScriptableObjects)
The Asset that holds the whole Rule System is called Ruleset and itself is a ScriptableObject too. I basicly just replaced the Inspector of the Ruleset ScriptableObject to give me a Button to open the EditorWindow where I then can edit the whole Ruleset at once. To organize the whole thing, I'm doing this to create a new Rule in that Ruleset:

Rule newRule = CreateInstance<Rule>();
// currentRuleset is the SerializedObject of the Ruleset, so it's targetObject is the ruleset ScriptableObject
AssetDatabase.AddObjectToAsset(newRule, currentRuleset.targetObject);
// Reimport the new Asset so it shows up in the UnityEditor
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newRule));

This part works great. All Rules show up as Sub-Assets of the Ruleset-ScriptableObject. I then add it to a list in the Ruleset and apply the modified properties, so it all gets saved.

Now to my problem:
When I want to remove a Rule from that Ruleset, the only ways to achieve that I've found are removing the object from the Asset and destroying it, so I do the following:

// ruleList is the SerializedProperty of a List<Rule> in the ruleset, so accessing values inside it is a bit fidilly
// Remove the last rule SubAsset from the ruleset mainAsset
AssetDatabase.RemoveObjectFromAsset(ruleList.GetArrayElementAtIndex(ruleList.arraySize - 1).objectReferenceValue);
// Destroy the rule ScriptableObject so it is actually deleted
DestroyImmediate(ruleList.GetArrayElementAtIndex(ruleList.arraySize - 1).objectReferenceValue, true);
currentRuleset.ApplyModifiedProperties(); // <- currentRuleset is the SerializedObject of the ruleset
// Reimport the mainAsset so the changes show up in the UnityEditor
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(currentRuleset.targetObject));

This also works, BUT, Unity throws me a Warning each time I remove a Rule from the Ruleset because of the ImportAsset-Command:

Importer(NativeFormatImporter) generated inconsistent result for asset(guid:e23e75a89de672045aeaa42eeb54b26e)

Is there any better way to do this? If I remove the import command, changes will not show up in Unity until I reimport or compile again. (I also recieve this warning when I manually reimport the asset)

0 Answers
Related