XML and Unity 3

Hi,

I use a XML file to store the player’s data this way:

void SavePlayerData() 
   { 
		StreamWriter writer; 
		FileInfo info = new FileInfo(fileName); 
		if(!info.Exists)
		{ 
			writer = info.CreateText(); 
		} 
		else 
		{ 
			info.Delete(); 
			writer = info.CreateText(); 
		} 
		writer.Write(dataBuffer); 
		writer.Close(); 
   }

but after upgrading to Unity 3.0 I get an error message complaining about ‘System.IO.FileInfo’ that doesn’t contain a definition for ‘Delete’
error CS1061: Type System.IO.FileInfo' does not contain a definition for Delete’ and no extension method Delete' of type System.IO.FileInfo’ could be found (are you missing a using directive or an assembly reference?)

Testing with Unity 2.6 the same script works fine. Does anyone know what may cause the problem or a better solution to store the player’s data for the web?

Thanks

Nevermind, moved on to PlayerPrefs

I am also having this problem, after upgrading to Unity 3.0 Pro I am getting:

error CS1061: Type System.IO.FileInfo' does not contain a definition for Delete’ and no extension method Delete' of type System.IO.FileInfo’ could be found (are you missing a using directive or an assembly reference?)

and also:

error CS1061: Type System.IO.FileInfo' does not contain a definition for OpenText’ and no extension method OpenText' of type System.IO.FileInfo’ could be found (are you missing a using directive or an assembly reference?)

And the reason is because my project was able to compile either as a standalone PC app or as a Web Player app. As a standalone it saved data locally and as a web app it saved to a database using WWW.

Now with Untiy 3.0, It will not compile a Web Player version with System.IO code, even though I only call that function if it is to run by a web player. Is there any way to have both in one build?

According to one source (http://forum.unity3d.com/threads/30077-Unity-C-preprocessor?p=195832#post195832) there should be preprocessor directives for you to include platform specific code into your build as of version 3. Not sure where the documentation for it though.

EDIT: found it: http://unity3d.com/support/documentation/Manual/Platform Dependent Compilation.html

(but I’m having trouble loading the data from that page. If you are too, here’s the google-cache entry: http://webcache.googleusercontent.com/search?q=cache:w3hP55QZqVoJ:unity3d.com/support/documentation/Manual/Platform%2520Dependent%2520Compilation.html+Unity+platform+dependent+compilation&cd=1&hl=en&ct=clnk&gl=ca

And it copy-pasted here:[quote]
Platform Dependent Compilation
Unity includes a feature named “Platform Dependent Compilation”. This consists of some preprocessor directives that let you divide your scripts to compile and execute a section of code exclusively for one of the supported platforms.

Furthermore, you can run this code within the Editor, so you can compile the code specifically for your mobile/console and test it in the Editor!

Platform Defines
The platform defines that Unity supports for your scripts are:

UNITY_EDITOR Define for calling Unity Editor scripts from your game code.
UNITY_STANDALONE_OSX Platform define for compiling/executing code specifically for Mac OS (This includes Universal, PPC and Intel architectures).
UNITY_DASHBOARD_WIDGET Platform define when creating code for Mac OS dashboard widgets.
UNITY_STANDALONE_WIN Use this when you want to compile/execute code for Windows stand alone applications.
UNITY_WEBPLAYER Platform define for web player content (this includes Windows and Mac Web player executables).
UNITY_WII Platform define for compiling/executing code for the Wii console.
UNITY_IPHONE Platform define for compiling/executing code for the iPhone platform.
UNITY_ANDROID Platform define for the Android platform.
UNITY_PS3 Platform define for running Play Station 3 code.
UNITY_XBOX360 Platform define for executing XBbox 360 code.
UNITY_WINGLESEMU Platform define for OpenGL ES 1.1 Emulator platform (can only be built from UnityEditor developer version on Windows) .
UNITY_WINGLES20EMU Platform define for OpenGL ES 2.0 Emulator platform (can only be built from UnityEditor developer version on Windows) .

Note: These defines where introduced at version 3.0.

Also you can compile code selectively depending on the version of the engine you are working on. Currently the supported ones are:

UNITY_2_6 Platform define for the major version of Unity 2.6.
UNITY_2_6_1 Platform define for specific version 1 from the major release 2.6.
UNITY_3_0 Platform define for the major version of Unity 3.0.
UNITY_3_0_0 Platform define for the specific version 0 of Unity 3.0.

Note: For versions before 2.6.0 there are no platform defines as this feature was first introduced in that version.

Testing precompiled code.
We are going to show a small example of how to use the precompiled code. This will simply print a message that depends on the platform you have selected to build your target.

First of all, select the platform you want to test your code against by clicking on File → Build Settings. This will bring the build settings window to select your target platform.

Build Settings window with the WebPlayer Selected as Target platform.
Select the platform you want to test your precompiled code against and press the Switch Editor button to tell Unity which platform you are targeting.

Create a script and copy/paste this code:

JavaScript Example:

function Awake() {
#if UNITY_EDITOR
Debug.Log(“Unity Editor”);
#endif

#if UNITY_IPHONE
Debug.Log(“Iphone”);
#endif

#if UNITY_STANDALONE_OSX
Debug.Log(“Stand Alone OSX”);
#endif

#if UNITY_STANDALONE_WIN
Debug.Log(“Stand Alone Windows”);
#endif
}

C# Example:

using UnityEngine;
using System.Collections;

public class PlatformDefines : MonoBehaviour {
void Start () {

#if UNITY_EDITOR
Debug.Log(“Unity Editor”);
#endif

#if UNITY_IPHONE
Debug.Log(“Iphone”);
#endif

#if UNITY_STANDALONE_OSX
Debug.Log(“Stand Alone OSX”);
#endif

#if UNITY_STANDALONE_WIN
Debug.Log(“Stand Alone Windows”);
#endif

}
}

Boo Example:

import UnityEngine

class PlatformDefines (MonoBehaviour):

def Start ():
ifdef UNITY_EDITOR:
Debug.Log(“Unity Editor”)

ifdef UNITY_IPHONE:
Debug.Log(“IPhone”)

ifdef UNITY_STANDALONE_OSX:
Debug.Log(“Stand Alone OSX”)

ifdef not UNITY_IPHONE:
Debug.Log(“not an iPhone”)

Then, depending on which platform you selected, one of the messages will get printed on the Unity console when you press play.
[/quote]

Thanks FizixMan, that will take care of my issue.

Hi,
seen this but i dont have the slightest idea how it will help me to publish to webplayer setting.
i want this >> http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML to work in webplayer setting…

that can not work.
Webplayers can’t write files or access the local system in any way

If you want to save XML as a webplayer you must use WWW, add it as data to the WWWForm and pass that along to a webeservice and have the webservice store and offer it

You might be able to save the XML String in a Player pref, depending on the Size of the string
(not sure what the size limit for player prefs is from the top of my head)

Have a look at the documentation.

sorry coz I just steps into unity3d world about a month but are you saying its possible but just in other way?
ok i found this

are you talking about this?
any guide on how to do this?

Hi all,

I am confused here. I’m getting the same error as Voideo following an upgrade to 3.0 from 2.6. When I build as a “PC and Windows standalone” there are no problems. However, when I build as “Webplayer” I get:

error CS1061: Type System.IO.FileInfo' does not contain a definition for Delete’ and no extension method Delete' of type System.IO.FileInfo’ could be found (are you missing a using directive or an assembly reference?)

It is referencing the line “t.Delete()” in the CreateXML() procedure from _GameSaveLoad.cs which I borrowed from http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML which Zumwalt wrote a couple years ago.

I understand that I need to use a preprocessor directive. However, I cannot find any documentation on HOW to implement this within my code. Can anybody give me specific advice on what I need to do?

Thank you!

http://msdn.microsoft.com/en-us/library/ed8yd1ha(v=vs.71).aspx
That link has some Preprocessor directives, if you click on the directives you will see examples.

mostly just #if() and #endif and #else for what you need.

Webplayers cannot access the File System, and so File system related classes are all stripped and non-functional (to avoid bloating the build with unusable classes).

Thanks, Ntero. I realize that I need preprocessor directive statements like:

But what do I need to actually directed it to do in the case of Unity Webplayer? That is what I’m confused about.

Ah ok
You’d probably want to wrape #if !UNITY_WEBPLAYER, and don’t perform file i/o in the web player. As to alternatives you’ll likely need to track something either in memory or on a server.