- Home /
Script generation.
Hi, I am thinking of writing an extension for unity Editor. I want to create a 'setup' window, which will do some work for me and setup any new project based on settings I choose. For example: I check 'google play', 'save files' and 'achievements' checkboxes and hit 'Setup' button. Now, based on these settings the script would generate necessary generic classes for me.
Now for the questions:
How can I store code template files so my extention would read it? What type? Xml? Other? the extension needs to save those templates as .cs files
How can I specify what to read? Reading from line X to line Y would be ok., but ideally I need something like this: look for a text "JFHDDI" (for example) -> read until you come across this "HFIDDN" -> then build a .cs file from what you have read.
Can I edit .cs scripts with another script (not runtime of course)?
Although, I could store my code templates as xml files I still can't find solution to my second question.
Any help much appreciated.
You can store all your templates in .txt files which you can read and manage easily, create saperate folder for each functionality you want to add and import whole folder inside project and rename files.
you can keep all the variable values (google play IDs and all) inside resources folder and read them at runtime.
Yes, you can edit files in editor, in fact script can also edit script itself.
Answer by Fire_Cube · Jul 05, 2016 at 12:01 PM
I did something of this kind, it first depend if you need to have 'arguments' of generation or not. I think the best way for what you say is to keep the classes in txt files (online or not), with eventually a file that contain a register of all files, and just use File.Copy(from, to); to generate the files. If you need arguments, it's getting harder, but you can develop a pseudo programming language for it, like when reading, if you go through a line that start with ~$: it mean it's a special code line, and you need to see what it mean eg:
public class Foo
{
~$:if(USE_CONSTRUCTOR)
public Foo()
{
//stuff
}
~$:endif
}
Maybe try something like that. If you want, I can help you create a pseudo code like this, i think it's interesting ;)
good luck :)
Hmm. I suppose if I keep each functionality in a separate txt file and then read what I need and store it in string variables I could then use File.AppendAllText and create a script file out of what I gather... Ugh seems like a lot of tedious work but it's doable. Thanks for the hint!
Well, once the code to read the files and eventually modify them, it's easy. Do you really need to do file modification? If not, then just templates files are quite enough.
If you want I can do you a code for the syntax up there, since it's more an iteration of all lines than anything else.
Thanks but I found a piece of code that searches for a block of text I want and copies it into a variable. I can build my script files from there. I just need to write the templates with modularity in $$anonymous$$d. I think that if I think this through I will be able to make this extension.
Here is the find: http://stackoverflow.com/questions/11195827/reading-block-of-text-file
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Undo.RecordObjects is not working on 2018.3??? 0 Answers
how to subdivide and smooth a cube in unity 1 Answer
How to add a component on a GameObject in Custom Inspector 1 Answer