Creating Scripts via Custom Editor

Hey there! I’m trying to create script asset files via a Custom Editor but am not having much luck. I’ve tried writing a ScriptableObject to a file but it just seems to write YAML and I can’t apply that to an existing object.

Has anyone been able to create Script asset files via a custom editor and, if so, what’s the best way to accomplish this? Could it be as simple as just writing to a file with a .cs extension?

Thanks in advance!

5198795–516920–CurrentMethod.txt (1.82 KB)

Please post the code in code tags rather that attaching a file to your post.

The most straightforwards way is to write the script text to a file with the .cs extension, and then calling AssetDatabase.Refresh to trigger the file getting imported and compiled.

1 Like

Got it. Will do going forward.

Much appreciated!

Why do you want to create it via ScriptableObject? Just writing text to file should be enough for Unity to add script to project, like this:

File.WriteAllText("Assets/MyScript.cs", programText);