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:
/*
The following license supersedes all notices in the source code.
Copyright (c) 2019 Kurt Dekker/PLBM Games All rights reserved.
http://www.twitter.com/kurtdekker
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the Kurt Dekker/PLBM Games nor the names of its
contributors may be used to endorse or promote products derived from
This file has been truncated. show original
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.
Kurt-Dekker:
This works awesomely! Here is an example in my Datasacks package:
https://github.com/kurtdekker/datasacks/blob/master/datasacks/Assets/Datasack/Core/DatasackInspector.cs
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?