Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[WP-WS] Save file, HELP!!!

Discussion in 'Windows' started by Stefano75, Sep 18, 2015.

  1. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6
    Hi, I am trying to convert my work to Windows Phone and Windows Store, but I cannot write a routine to write and read a binary file for save game and save the status game. There is an documentations to do this? There are samples?

    Thanks!!
     
    Last edited: Sep 18, 2015
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,638
    What is WPS? How are you writing to binary file?
     
  3. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6
    sorry I have wrong word. WP and WS, Windows Phone and Windows Store!
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,638
    Still the question remains, how are you writing/reading.
     
  5. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6
    I have used FileStream but at runtime the application crashes...
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,638
    What is the callstack of the crash? Also show some code
     
  7. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
  8. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6
    Sorry for late:

    this is the code test to write a file... but when I try to read I have an error:

    #if NETFX_CORE

    public async void WriteBuffer() {
    var _Name = _fileName;
    var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var _Option = Windows.Storage.CreationCollisionOption.ReplaceExisting;
    var _File = await _Folder.CreateFileAsync(_Name, _Option);
    if (_File != null)
    {
    Debug.Log("*** Create file: " + _Name);​
    }
    else
    return;​

    byte[] buffer = new byte[_length];
    System.Buffer.BlockCopy(_buffer, 0, buffer, 0, _length);
    await Windows.Storage.FileIO.WriteBytesAsync(_File, buffer);​
    }

    #endif

    for read:

    #if NETFX_CORE

    public async void ReadBuffer() {

    var _Name = _fileName;
    var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var _File = await _Folder.GetFileAsync(_Name);

    if (_File != null)
    {
    Debug.Log("*** Open file: " + _Name);​
    }
    else {
    Debug.LogError("*** Error open file: " + _Name); <<<< this is the error message. So GetFileAsync fails.
    return;​
    }

    IBuffer buffer = await FileIO.ReadBufferAsync(_File);
    using (DataReader dataReader = DataReader.FromBuffer(buffer))
    {
    dataReader.ReadBytes(_buffer);
    if(_buffer == null) {
    Debug.LogError("*** Cannot open file for read: " + _Name);​
    }​
    }​
    }

    #endif

    Thanks for help!
     
  9. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,638
    How the error looks like and what line throws it, there are multiple places here which output an "error"
     
  10. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6
    inside ReadBuffer() fails _Folder.GetFileAsync(_Name);

    but, my questione is: is it a right way to save and write a file? Thanks!!!
     
  11. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,638
  12. Stefano75

    Stefano75

    Joined:
    Sep 18, 2015
    Posts:
    6