- Home /
How to control class variables from an Editor Tool or external file?
So I have been scratching my head a bunch on this issue I'm having...
Basically, I have a class called "BikeUpgrades" which does not extend MonoBehavior. That class has all the base values for each upgrade defined in it.
Now I would like to be able to change those values from either an Editor Tool, or an external file, because I don't want to have to open my BikeUpgrades class just to change some default values, I would rather be able to change them from somewhere else.
Let's say there were 3 other ppl working on my project, and they need access to those base values in order to tweak upgrade settings, but I don't want them to mess around in the code, so I would write an editor script that they can use, which updates the base values defined in the BikeUpgrades class. Or maybe I give them an external .ini or .json file that they can edit to change the base values of the BikeUpgrades class.
What would be the best way to do that? I really like the idea of using an editor script, but I do not have the PRO License for Unity so I can't use Asset Bundles (if I did, I would have the editor script save those values in an Asset Bundle).
Any help/tips will be greatly appreciated! As always, thanks for your time :) Stephane
Answer by StephanK · Sep 07, 2011 at 07:17 PM
There are several ways to do this. These are the ones I already tried out and that worked:
Instead of a static class use a singleton MonoBehaviour. Make a prefab of that and now you can change the values directly on the prefab.
Write a custom editor/inspector script that exposes static members. (This is possible using reflection but not the easiest thing to do)
Use a text based format of your (and your colleagues) liking. Store this in Resources and parse it at runtime.
We tried several formats (xml,json,csv), each has advantages and disadvantages. At the moment we are using a solution that uses Google spreadsheets which are auto-converted to csv or json depending on the content.
Thanks for your answer spree! The Singleton $$anonymous$$onoBehaviour would be the easiest for sure, but I don't want to carry around a gameObject for the base upgrade settings - although there's nothing wrong with that - because I'm trying to $$anonymous$$imize having to rely on existing GameObjects for stuff like this.
I think using a text based format like json might be the best solution for me. I could write a editor script that allows changing/retrieving the values from a json file, so we wouldn't even have to edit the actual json file itself.
When you say "parse the file from Resources at runtime", do you mean using "Resources.Load("$$anonymous$$yFile")" or actually using WWW or File.IO to load the file?
Answer by StephanK · Sep 07, 2011 at 08:48 PM
I'd put them into the Resources folder and load them as TextAsset with Resources.Load("MyFile", typeof(TextAsset));
That's what I thought...I tried loading it with File.IO and WWW, which both worked on the computer, but not on the iPhone. For some reason, I cannot find a file in the Resources folder on the iPhone by doing:
WWW www = new WWW("file://" + Application.dataPath + "/Resources/myFile.xml");
the above works fine on the PC or $$anonymous$$ac, but I never got it to work on the iPhone...so Resources.Load it is :)