Create an Application Support Folder

Hey, I’m trying to create a directory where I can store player data, preferences, usernames, ect… I’ve created this script:

REMOVED SCRIPT - solved this problem

I then attached this script to an empty GO. All the debug messages execute, but when I try to navigate to this folder, it isn’t there. Is it creating it in another place I’m not aware of, or am I just doing this incorrectly?

Edit - I was able to create a folder in Library/Application Support, so I fixed that. But now, I’m able to serialize my Hashtable, but sometimes it deletes its self, and writes over it, and I KNOW it does when trying to right to it from either the editor or the standalone, but it does also do it others time for reasons I’m not sure of.

What I’m doing is I store a Hashtable of objects of type User (which is serializable and contains a member Score that us serializable as well). Then, on start of the application I create the directory and the file (I only do this the first time the game ever runs), then I deserialize the file containg the hastable of User objects like this:

	void Awake ()
	{
		// Keep this alove each scene
	    DontDestroyOnLoad (this);
		
		// Read in the PlayerPrefs data and load it into the HashTable
		Stream fs;
		if( !Application.isEditor) // The reason for this is that the editor and standalone have different encryptions for some odd reason
		{
			fs = File.OpenRead("/Library/Application Support/Asteroid Blaster/PlayerPrefs.abp");
		}
		else
		{
			fs = File.OpenRead("/Library/Application Support/Asteroid Blaster/PlayerPrefsEditor.abp");
		}
		try
		{
			//IFormatter formatter = new BinaryFormatter();
			BinaryFormatter formatter = new BinaryFormatter();
			m_UserList = formatter.Deserialize(fs) as Hashtable;
		}
		catch(SerializationException e)
		{
			System.Console.WriteLine("Failed to de-serialize. Reason: " + e.Message);
			throw;
		}
		finally
		{
			fs.Close();
		}
	}

This should reload hashtables with all the User back into the Hashtable object on startup everytime the game is ran.
Then in the menu I have a text field and button. The user enter ther username, and if the username doesn’t all ready exist, I stores it n hashtable:

void OnMouseDown()
	{
		// Get the text from the Inputfield	
		string UserNameString = GameObject.Find("MainMenuBackLayer/TextInput/InputField").guiText.text;
		
		// If it doesn't all ready contain the key (the key will be the name of the username)
		if( !GlobalData.Users.ContainsKey(UserNameString) )
		{
			System.Console.WriteLine( "Added a new user" );
			// Create a new User
			User newUser = new User( UserNameString );
			// Add the user to the hash
			GlobalData.Users.Add( UserNameString, newUser );
			
			// TODO - Implement active user
			//GlobalData.ActiveUser = UserNameString;
		}
		
		LoadMainGame();
	}

Finally, at the end of the game when it quits I serialize the hashtable and write to the file where I will eventually load it from again next time the game is ran.

// Ensure that the instance is destroyed when the game is stopped in the editor.
	void OnApplicationQuit()
	{
		// Save the user(s) data
		Stream myStream;
		if( !Application.isEditor)
			myStream = File.OpenWrite("/Library/Application Support/Asteroid Blaster/PlayerPrefs.abp");
		else
			myStream = File.OpenWrite("/Library/Application Support/Asteroid Blaster/PlayerPrefsEditor.abp");
			
		BinaryFormatter formatter = new BinaryFormatter();
		
		try
		{
			formatter.Serialize(myStream, GlobalData.Users);
		}
		catch(SerializationException e)
		{
			System.Console.WriteLine("Failed to serialize. Reason: " + e.Message);
			throw;
		}
		finally
		{
			myStream.Close();
			Debug.Log( "The newely serialized file is has closed");
		}
		
		s_Instance = null;
		//Debug.Log( "we quit" );
	}

I just don’t know why sometimes the it erases its self, and the two different games (the editor version compared to the standalone) do it differently. If it makes any different the Hashtable is stored in the Global Data class and the Awake and OnApplicationQuit is called from that class, and that class/GO exists through the whole program.

Thanks a bunch for any help!!
Jedd

I imagine it’s because you can’t use ~ outside of a shell. This isn’t possible, because it’s part of the shell’s functionality, rather than something you automatically get when you try to use a path.

I think you’re supposed to use System.Environment.GetFolderPath() from .NET to get a path to the folder you’re after. This page gives you a list of values you can use to tell it which folder you’re after. Click the link to Environment.GetFolderPath at the top of the page to see the rest of its documentation.

You’ll notice that the names you can choose are rather Windowsy, but I believe they map onto the closest OSX equivalents. You might have to experiment to find out which is which.

Thanks, I editted my post as you were posting, I think I fixed the first problem, I have a new one now… :frowning:

Your problem has become so large that I can’t see all of it at once, so I’ll have to pass on it for now. :wink:

However, I should mention that you can’t rely on being able to write to anything in /Library (the one in the root folder) unless you’re running as an administrator. You really should try to find the one in the user’s home folder (at least as a fallback in case accessing /Library fails).

Hehe, I figured most wouldn’t want to tackle this one :stuck_out_tongue:

I’ll try finding another folder then :slight_smile: (maybe that is the problem :slight_smile: )

well, the folder wasn’t the problem, but I did switch. I did find that when I ever I changed any code (even adding a space in the middle of nowhere) in the Global Data source file or the file that adds a user to the hashtable when the button is pressed, I get this error:

and I see that the large string of characters in the error message is often similar to the string in the actual file that stores the serialized hashtable - and it seems the forum won’t take any more characters after this >_>, so I can’t show you the actual file

Edit - I made a new revelation! Every compile (so, that includes editing scripts) is when the error/erase occurs. It’s expecting a different string of characters that I mentioned earlier in this post… So basically, nothing goes wrong if the serialized data is used from one version, which is not very convinient, and I don’t know how to fix it.

I found my problem! This… Version-tolerant binary serialization | Microsoft Learn :frowning:
Does Unity support Mono 2.0 (thus supporting this feature)?

Did anyone find a solution? I am stuck in the same problem. As far as I know Unity does not run on Mono 2.0 yet.

I first tried using the xml serialization, but it required all my fields to be public. Why, Microsoft, why?!

Also, AudioClip and other Unity classes are not serializable. Why is this?

… why?!

~ce