How to store game accounts locally?

I’m trying to implement the possibility of having a game account in my game. For now my idea is to store it locally.

Right now I only check your username (does it exist?) With PlayerPrefs. However that’s not enough. I want to implement a password as well. And that you can choose the colour of the spherelike player. I don’t think this is doable with PlayerPrefs, or is it?

My idea would be to create a class account with as attributes the name, the password and the colour. But how am I supposed to store the instances of that class, and how should I be able to retrieve the accounts out of the storage?

I sincerely hope someone can help me:)

You have to look into (de)serialization, it’s actually pretty easy. In essence, you open a file (or create it if it doesn’t already exist), write to it any data you want, and then close it again.

Here is a simple example.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class WriteToFile : MonoBehaviour
{
	private Color varToGet;
	private Color varToGet2;
	private TextAsset textFileVar;
	private string textFileToString;





	void Awake() {
		textFileVar = Resources.Load("Scripts/blubb") as TextAsset;


	}

	void Start() {
		Main ();
		textFileToString = textFileVar.text;
		var stringToDic = ParseColorTable(textFileToString);
		varToGet = stringToDic["skinColor"];
		varToGet2 = stringToDic["hairColor"];
	}

	public static void Main() {
		// Create an instance of StreamWriter to write text to a file.
		// The using statement also closes the StreamWriter.
		using (StreamWriter sw = new StreamWriter("Assets/Resources/Testfiles/TestFile.txt")) {
			Debug.Log ("writing to file");
			// Add some text to the file.
			sw.Write("This is the ");
			sw.WriteLine("header for the file.");
			sw.WriteLine("-------------------");
			// Arbitrary objects can also be written to the file.




		}
		System.IO.File.AppendAllText("Assets/Resources/Testfiles/TestFile.txt", System.String.Format("{0} {1} {2} {3} {4}", "string1", "string2", "float1", "float2", "float2"));
	}

	Dictionary<string, Color> ParseColorTable(string aText)	{
		
		Dictionary<string, Color> result = new Dictionary<string,Color>();
		
		string[] lines = aText.Split('

‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');

				// Remove all spaces and split the 4 color values
				string[] values = S[0].Replace(" ","").Split(',');
				
				// Parse the 4 strings into floats and create the color value
				Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
				
				// Read the colorname and remove leading or trailing spaces
				string colorName = S[1].Trim();
				
				result.Add(colorName,col);
			}
		}
		return result;
	}


	Dictionary<string, Color> ParseFillTable(string aText)	{

		Dictionary<string, Color> result = new Dictionary<string,Color>();
		
		string[] lines = aText.Split('

‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');

				// Remove all spaces and split the 4 color values
				string[] values = S[0].Replace(" ","").Split(',');
				
				// Parse the 4 strings into floats and create the color value
				Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
				
				// Read the colorname and remove leading or trailing spaces
				string colorName = S[1].Trim();
				
				result.Add(colorName,col);
			}
		}
		return result;
	}
}

One way to deal with passwords is to Hash the user input string, and store that hash. Then whenever the user inputs that string again, Hash it and check versus the stored Hash. This site has a bunch more detailed information on passwords ( Secure Salted Password Hashing - How to do it Properly )

Color might be a bit trivial an example, but you could use Color32 and cast it to an int ( Unity - Scripting API: Color32 )

Overall though you might want to look into Serializing various properties of your class and use a StreamWriter
(C# Write and Read Binary Data File - Visual C# Kicks)