I am creating a new script from a template. However, this new script type is not accessible until the Assembly has had a chance to re-compile. I am trying to create an instance of the new ScriptableObject, in order to generate it through code.
public override void Def()
{
NewAssetName = EditorGUILayout.TextField(NewAssetName);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add"))
{
TemplateLines = File.ReadAllLines("E:/Unity/Editor/Data/Resources/ScriptTemplates/NewScriptableObject.cs.txt");
for (int i = 0; i < TemplateLines.Length; i++)
{
if (TemplateLines[i].Contains("#SCRIPTNAME#"))
{
TemplateLines[i] = TemplateLines[i].Replace("#SCRIPTNAME#", NewAssetName);
}
}
NewFilePath = "Assets/" + NewAssetName + '/' + NewAssetName + ".cs";
NewSOPath = "Assets/" + NewAssetName + '/' + NewAssetName + ".asset";
File.WriteAllLines(NewFilePath, TemplateLines);
ScriptableObject NewSO = CreateInstance(TypeName);
AssetDatabase.CreateAsset(NewSO, NewSOPath);
}
Everything works fine, up until I use CreateInstance(); At this point, the type does not exist yet. I have to wait for the Assembly to re-compile and capitulate the type...
I have Googled the concept of refreshing the Assembly, but have not found anything.
I have also Googled using async/await in order to delay calling CreateInstance(), until AFTER the Assembly, for sure, has the new type... So far, these attempts have either locked up the Editor, or do not work as intended... (I am new to this async style in C#)
I am open to either an Assembly solution, or an async solution, for solving this problem.