Editor Script : Change a line of code

I have a static class with a bool which I would like to set to true/false based on an Editor Script…

i.e. I have this in a class…

public static bool Toggle = true;

I want to be able to use an Editor script to add a menu to toggle it to true or false depending on which is clicked… I can do the menu part that’s easy, it’s just the changing the actual code part…

Not quite sure where to look for how to inject code into a file… Is this even possible?

Sure, just edit your .cs / .js file with regular System.IO.File.* methods.

Read using System.IO.File.ReadAllLines(), modify string on desired line and write back using System.IO.File.WriteAllLines().

You may use System.IO.File.ReadAllText() / System.IO.File.WriteAllText() as well.

Examples:

http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx

http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx

To locate your file just look for it at the Application.dataPath recursively, or use relative to Application.dataPath path if file location is constant.