I’ve been digging around for a possible solution to this but cannot find anything that I can make work. I’ve referenced this UnityAnswer, but cannot get anything to work for myself.
What I’m trying to do is create a custom editor that creates a script that inherits from a base class, and then attach that script to a generated game object that is a prefab. Getting the script and gameobject generated separately isn’t a problem. I just cannot seem to get the editor script to wait for any length of time and wait for the Unity Editor to compile the newly created script before it gets added to the game object using ‘AddComponent’. When I’m using ‘Type.GetType’ it is returning null, so I’m assuming it is the editor compiling and my editor script doesn’t have access to the newly created script when this is called.
Thank you for your time!
using System;
using System.Collections;
using System.IO;
using UnityEditor;
using Unity.EditorCoroutines.Editor;
using UnityEngine;
public class PlunderersRunUnityDevTools : ScriptableWizard
{
[SerializeField] private int _itemID;
[SerializeField] private string _displayName;
[TextArea] [SerializeField] private string _description;
[SerializeField] private Sprite _icon;
[SerializeField] private PirateType _pirateType;
[SerializeField] private bool _isPassive;
[MenuItem("Plunderer's Run/Create New Skill")]
private static void CreateNewSkillPrefab()
{
var window = ScriptableWizard.DisplayWizard<PlunderersRunUnityDevTools>("Create Skill");
window.minSize = new Vector2(150, 150);
}
private void OnWizardCreate()
{
EditorCoroutineUtility.StartCoroutine(GenerateSkillScript(), this);
AssetDatabase.Refresh();
Debug.Log("Created prefab object.");
}
private IEnumerator GenerateSkillScript()
{
GameObject skillObject = new GameObject("New Skill");
string fileName = _displayName.Replace(" ", "_");
fileName = fileName.Replace("-", "_");
string filePath = "Assets/01_Scripts/Skills/Custom/" + fileName + ".cs";
if (File.Exists(filePath) == false)
{
Debug.Log("Creating script file.");
using (StreamWriter outfile = new
StreamWriter(filePath))
{
outfile.WriteLine("using UnityEngine;");
outfile.WriteLine("using System.Collections;");
//outfile.WriteLine("namespace BluSurface.RPG.Skills");
//outfile.WriteLine("{");
outfile.WriteLine(" public class " + fileName + " : Skill");
outfile.WriteLine(" {");
outfile.WriteLine("");
outfile.WriteLine(" }");
//outfile.WriteLine("}");
}
yield return EditorApplication.isCompiling;
Debug.Log("Script Created");
AssetDatabase.Refresh();
skillObject.AddComponent(Type.GetType(fileName));
PrefabUtility.CreatePrefab("Assets/Resources/Skills/GameObjects/" + fileName + ".prefab", skillObject);
AssetDatabase.Refresh();
DestroyImmediate(skillObject);
}
}
}