Generate procedural scripts with custom editor.

Hi, so im making a tool for unity with custom editor.

The thing is i would want to create scripts given some parameters, and those to be included in the script that we are creating.

I’ve been looking arround but i can’t find anything. Should i just create a .cs file and with system.io add code by strings?

This works awesomely! Here is an example in my Datasacks package:

Look around the GenerateCode() method, specifically the block beginning line 243. The code is assembled above that in the string.

Indirectly related to producing code, block beginning line 253 creates a ScriptableObject with references to the Datasacks.

And then the final magic is line 288:

AssetDatabase.Refresh();

It’s also handy to inject a comment header such as what you see in line 121 above warning future users NOT to modify the file by hand.

I’ve been looking into your solution. It looks interesting and ill definetly look more into it. Thanks m8.

1 Like

C# and the .NET Framework provide CodeDOM as one way for source code generation. If you are interested in the topic, I asked about this myself and got some helpful responses: Generating class definitions from code?

Thanks!