Search Unity

XML and Unity 3

Discussion in 'Scripting' started by Voideo, Sep 28, 2010.

  1. Voideo

    Voideo

    Joined:
    Sep 28, 2010
    Posts:
    6
    Hi,

    I use a XML file to store the player's data this way:
    Code (csharp):
    1.  
    2. void SavePlayerData()
    3.    {
    4.         StreamWriter writer;
    5.         FileInfo info = new FileInfo(fileName);
    6.         if(!info.Exists)
    7.         {
    8.             writer = info.CreateText();
    9.         }
    10.         else
    11.         {
    12.             info.Delete();
    13.             writer = info.CreateText();
    14.         }
    15.         writer.Write(dataBuffer);
    16.         writer.Close();
    17.    }
    18.  
    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
     
  2. Voideo

    Voideo

    Joined:
    Sep 28, 2010
    Posts:
    6
    Nevermind, moved on to PlayerPrefs
     
  3. rkite

    rkite

    Joined:
    Feb 22, 2010
    Posts:
    34
    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?
     
  4. Chris Sinclair

    Chris Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    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.c...ependent+compilation&cd=1&hl=en&ct=clnk&gl=ca

    And it copy-pasted here:
     
    Last edited: Oct 4, 2010
  5. rkite

    rkite

    Joined:
    Feb 22, 2010
    Posts:
    34
    Thanks FizixMan, that will take care of my issue.
     
  6. deddytati

    deddytati

    Joined:
    Jul 22, 2011
    Posts:
    16
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    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
     
  8. joshimoo

    joshimoo

    Joined:
    Jun 23, 2011
    Posts:
    266
    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.
     
  9. deddytati

    deddytati

    Joined:
    Jul 22, 2011
    Posts:
    16
    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?
     
    Last edited: Aug 10, 2011
  10. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    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!
     
  11. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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).
     
  12. amherst

    amherst

    Joined:
    Dec 9, 2008
    Posts:
    30
    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.
     
  13. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.