Built-in, readonly database for single-player game in a webplayer.

First things first: I’m well-aware of the limitation of the Unity Webplayer that File I/O cannot occur. Webplayer does not allow it, for security reasons.

I’m not looking to flout this, so much as an alternative. I read somewhere that Text Assets in a Unity solution are compiled with the resulting game. What I’m wondering is, if it is possible to specify a built-in text file that contains information (for grins and giggles, say, XML information) that is read at runtime from within the solution.

The point isn’t to implement any form of File I/O; it’s to prevent coding data. If I can have a datafile that compiles with my project, that would be far preferable to making a million little scripts that return new instances of something like an item or piece of equipment. My web search has failed me (though, it is possible I didn’t try hard enough) on this one, as I haven’t found a clear answer to this question.

Short Version:
Can I use code to read a text asset that is built with my solution, from a webplayer?

Yes, for example using XmlSerializer:

public DataClass LoadData(string resourceName) {
		TextAsset asset = Resources.Load(resourceName) as TextAsset;
		if (asset == null) return null;
		Stream stream = new MemoryStream(asset.bytes);
		XmlSerializer serializer = new XmlSerializer(typeof(DataClass));
		DataClass data = (DataClass) serializer.Deserialize(stream);
		return data;
}

It you couldn’t, the text asset would be mighty pointless now wouldn’t it.

http://docs.unity3d.com/Documentation/ScriptReference/TextAsset.html

You’ll need to write something to parse the data, depending on the format you want to use. A while back a made a pure C# XML parse (basic tokenizer).

http://forum.unity3d.com/threads/77383-Free-lightweight-XML-Reader-(needs-road-testing)

It reads data out of an XML formatted text asset into a basic object hierarchy. It hasn’t been maintained as I abandoned the project I was using it for long ago and when I first posted it no one seemed to be interested. I think someone posted a bugfix for it, been meaning to OS it and put it on github along with an asset store package - one day…

Thank you, you two. I wasn’t entirely sure if “no webplayer file I/O” was just restricted to outside of the Unity sandbox.

Thanks also for the links, I’ll do more research with those. Problem solved!

Well, the file I/O is to restrict you from accessing a user’s file system from code downloaded from the Internet (imagine the nastiness dodgy people could do otherwise). Stuff you place in a Resources folder is packaged with your game’s data, not accessed from the user’s file system, so the restriction doesn’t apply.