- Home /
Save load file from PSVita
Hi all, Im making a PSVita game (not PSM) and I want to be able to save data in a file to later recover it (like to let player make custom track or anything that requires a lot of data), I have tried streamingassets folder and persistent data path, none seem to work, well I think streaming assets is read only or results shows that, I use streamreader and streamwriter for file access, could anyone please tell me how can I achieve this? I believe the problem might be the folder rather than the functions used, in the docs I cant find a good way to do this nor is told anywhere as far as i can see but of course it has to be written somewhere. Thanks a lot beforehand for any clue
Answer by highpockets · Aug 20, 2020 at 06:38 PM
This is what I use, mind you I have not worked with PSVita, but I believe it should work.
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class StorageSystem
{
public static void SaveData<T>( T _data, string _fileName )
{
string _path = Application.persistentDataPath + _fileName;
BinaryFormatter _formatter = new BinaryFormatter();
FileStream _stream = new FileStream( _path, FileMode.Create );
_formatter.Serialize( _stream, _data );
_stream.Close();
}
public static TypeOfData LoadData()
{
string _path = Application.persistentDataPath + "/mydata.data";
if( File.Exists( _path ) )
{
FileStream _stream = new FileStream( _path, FileMode.Open );
BinaryFormatter _formatter = new BinaryFormatter();
TypeOfData _data = _formatter.Deserialize( _stream ) as TypeOfData;
_stream.Close();
return _data;
}
else
{
Debug.LogError("File not found in " + _path);
return null;
}
}
}
EDIT: I forgot to mention that the data types that you save must be serializable. [System.Serializable] public class MyClass{}
Thanks for sharing your snippet, I think its quite clever strategy, however its not working for me in the Vita, in PC it works perfect. I tried also replacing the persistent data path with the Strea$$anonymous$$g assets folder and still didnt work (in pc it works with both folders), Im open to other ideas or folders that we could try to store file in, as said I have the feeling the biggest issue is the folder is not the right one, I heard player prefs work for PS$$anonymous$$ but well I didnt even try that as I want to store files and in player prefs as far as I know one can only store individual values or arrays isolated. As said feel free to suggest other methods or folders to achieve this =)
I noticed some others having issues with this. And there doesn’t seem to be a clear answer as far as I know. The reason that a persistent data path is not working is either because Unity has the wrong path, or you don’t have write permissions at that path on the console. $$anonymous$$y guess would be that it has to do with not having write permissions. Unfortunately I’m unfamiliar with PS Vita, so sorry I can’t be of more help.
I personally wouldn’t store sensitive data in PlayerPrefs. For example, on the iPhone, player prefs are stored in the app’s plist file and anyone can open that file and add whatever values they want to it. But I don’t have any idea where PlayerPrefs are stored on the Vita, so if you find that PlayerPrefs are a potential solution for you. You could turn the objects that you want to store into JSON strings and store them as strings in PlayerPrefs.SetString(keyName, value). Here is a little example of how you would store and retrieve an object:
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int lives;
public string username;
public float health;
}
public class StorageSystem : $$anonymous$$onoBehaviour
{
public void SavePlayerData( PlayerData data )
{
if( data != null )
{
string json = JsonUtility.ToJson(data);
PlayerPrefs.SetString( “PLAYER_DATA”, json );
}
}
public PlayerData LoadPlayerData()
{
string json = PlayerPrefs.GetString( “PLAYER_DATA”, “No Player Data Found” );
return JsonUtility.FromJson<PlayerData>( json );
}
}
That should work, but again, I would personally dig into how to get the write permissions for the persistent data path and store the file as binary as opposed to PlayPrefs, but I thought I would just show another option.
Thanks a lot for sharing this =), I had no idea we could convert a class into a json string and use it like this. Playerprefs works in the Vita so... your solution is perfect! =). I have tried several things and there is only one thing that apparently doesnt work but Im sure is because Im doing something wrong or the json is not supposed to support this, I tried making in my class different types of data and among the many things I tried I wanted arrays, they worked great, and also multidimensional arrays and these dont work, of course I can find a work around storing my data in single dimension array and just knowing where each row ends could do the trick, but just I wonder if there is a direct cleaner way to store the multidimensional array into the json, as far as I can read in the docs about ToJson, arrays wont convert unless these are in a class although they dont mention multidimension arrays there
Answer by gregory34690 · Feb 21, 2021 at 12:19 AM
Hello! Did you find a solution? I'm not able to make a file in my psvita with unity too :( PlayerPrefs doesn't work too...
Hello, I think one can save some stuff using playerprefs but to create files is possible too, a friend found out the way in a PSVita development group we have at discord (if you want to join dont hesitate to ask). The method he found is something I havent tested myself but I can say if he says it works then it works, the way is to make any file you need in ux0:\data\ I cant remember entirely now if it was possible to make folder there too, but with that path you can make files so its a big step forward =).
Oh yes! It's works! Thanks, now i'm able to create files and folders in /ux0:/data/ You saved my life ahah
Great! =), Im glad I could help, so it worked even for new folders, its even better, I had that doubt, couldnt remember the exact way it worked, I hope you can make a great project and show it to the world, by the way if you have a youtube channel or place where you will showcase it Im eager to watch more about it =).
Answer by Riviera71 · Feb 24, 2021 at 12:57 PM
Hi all,
For PlayerPrefs users I use "PlayerPrefs.Save()" method during gameover.
That's all!
When the game restart the new values are loaded.
I do'nt know where the PlayerPrefs are saved but it works!
I will investigate where they are stored.
Enjoy!
Thanks for sharing this =), yes right, it seems this way it works in almost every platform from what i tested, and as you said I have no idea where these are stored but works and thats what matters =). by the way since you are working for the Vita I wonder if you have a youtube channel or some site where you showcase your works, Im eager to see them =)
Your answer
![](https://koobas.hobune.stream/wayback/20220613010130im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IOException: Sharing violation on path. Please Help! 2 Answers
Create image-file from resources image 1 Answer
Console.WriteLine Redirect to file 0 Answers
Save XML file on desktop unity 1 Answer