WebPlayer and file access

I’m really frustrated right now, maybe I’m not getting something or this is the way it’s and I’m hoping for a workaround.

I have some editor scripts. They handle data that I write/read to and from files to make them persistent as recompilation deletes everything. For that I use several System.IO.File methods.

So when I work for a WebPlayer build, all those functions stop working, as the WebPlayer platform doesn’t allow that namespace, the problem is I still need them to work on my editor scripts.

Is there any workaround ? a place where I can save my data to make them persistent but not using System.IO classes at all ?

Currently I’m not getting errors because I use #if UNITY_WEBPLAYER …, but I still want to have the functionality to work on all my editor scripts.

Please answer! please!

What and how much data exactly are you storing?

Unity itself is built around the gameobject/monobehaviour concept which allows you to store data through the serialization methods described here.
Easiest way (and the way I found unity to work best) is design your code around unity’s serialization system (If you can). This works best for smaller amount of non complex data that won’t need to be changed after your project is deployed.
So the first question is why is this system not enought for you (I personally know several cases so this is a honest question if it isn’t simply enough to trust the unity serialization system).

Some classes are available in the webplayer (Like the FileStream class) but cannot be used. Others like File.WriteAllText simply don’t compile.
If you write your editor scripts to use only methods that are available in the webplayer you can at least compile a webplayer build (they won’t run though).

Best way is to separate your loading into two steps

  • Loading the raw data (either as bytes or as string)
  • Processing that data into something useful

This way you can change the how the data is loaded without having to rewrite the whole loading routine for editor and the game.

To load external files in the webplayer you would need to load the data using the WWW class (which provides text and bytes from the loaded file). This will also work for a standalone build using file:// if you want to load the files locally, but the Editor will need a separate implementation then.

If the data you want doesn’t have to change after you built the game (e.g. Items in a singleplayer rpg) but you cannot use the unity serialization system, the easiest route from my perspective is to use textassets and Resource.Load. Just save all the files you create in your resources folder with a .txt extension and load them through Resource.Load, after that you can use something like StringReader or Encoding.UTF8.GetBytes() to read from them.

Thanks for the answer, a little misleading though.

The problem is not serialization but writing it to a file, to save the state.

They only need to work on the Editor not in the WebPlayer, but if I have WebPlayer as the active platform they don’t work, even if it’s for Editor routines only!

It’s the Editor itself that becomes a WebPlayer (with it’s restrictions of file reading/writing).

I managed to save a read-only copy of my data in the EditorPrefs while a WebPlayer platform is active, so problem solved.

Not a great way to make it work though.

You should report it as a bug. Editor scripts should work in the editor, regardless of the target build. And they have access to the entire c# namespace.

Unless you’re using it during runtime, which is different.

hmmm, not sure about that,
I think the security model drops in place as soon as you change the build target.
Maybe UT can confirm this ?