I made a database in Unity with C#, and for some reason it is not working. I made a static class with all the variables static and made save, load, check, create and reset functions. I also made a script that sits on a ScriptHolder GameObject and all the Buttons and InputFields reference. Here is my code:
Database Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Database
{
public enum Roles{
Teacher,
Student
}
public static Dictionary<string, string> logins = new Dictionary<string, string>();
public static List<Roles> roles = new List<Roles>();
public static void AddUser(string username, string password, Roles role)
{
logins.Add(username, password);
roles.Add(role);
}
public static void SaveUsers()
{
LoadUsers();
PlayerPrefs.DeleteAll();
if(logins.Count != roles.Count)
{
throw new System.Exception("Roles count and login count do not match!");
}
int counter = 0;
foreach (KeyValuePair<string, string> login in logins)
{
PlayerPrefs.SetString(login.Key, login.Value);
PlayerPrefs.SetString("usernames", "");
PlayerPrefs.SetString("usernames", PlayerPrefs.GetString("usernames") + "/" + login.Key);
PlayerPrefs.SetInt(login.Key, (int)roles[counter]);
counter += 1;
Debug.Log(PlayerPrefs.GetString("usernames") + "/" + login.Key);
}
}
public static void LoadUsers()
{
logins = new Dictionary<string, string>();
roles = new List<Roles>();
foreach (string key in PlayerPrefs.GetString("usernames").Split('/'))
{
logins.Add(key, PlayerPrefs.GetString(key));
roles.Add((Roles)PlayerPrefs.GetInt(key));
}
}
public static void FactorySettings()
{
PlayerPrefs.DeleteAll();
LoadUsers();
logins = new Dictionary<string, string>();
}
public static bool CheckUser(string username, string password)
{
if (logins.ContainsKey(username))
{
if (password == logins[username])
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
ScriptHolder:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class LoginProcess : MonoBehaviour
{
public TextMeshProUGUI username;
public TextMeshProUGUI password;
public Slider loadingSlider;
public GameObject error;
public GameObject loading;
public float progress = 0f;
public Dictionary<string, string> show = Database.logins;
public void Login()
{
if(Database.CheckUser(username.text, password.text))
{
StartCoroutine(LoadAsync(1));
loading.SetActive(true);
}
else
{
error.SetActive(false);
}
}
public void Create()
{
Database.AddUser(username.text, password.text, Database.Roles.Teacher);
Database.SaveUsers();
Database.LoadUsers();
}
public void _Reset()
{
Database.FactorySettings();
}
public void Start()
{
Database.LoadUsers();
}
IEnumerator LoadAsync (int index)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(index);
while (!operation.isDone)
{
float _progress = Mathf.Clamp01(operation.progress / .9f);
progress = _progress;
yield return null;
}
}
public void Update()
{
if (progress != 0)
{
loadingSlider.value = progress;
}
if (Input.GetMouseButtonDown(0) && error.activeSelf)
{
error.SetActive(true);
}
}
}
My Error:
ArgumentException: An item with the same key has already been added. Key: System.Collections.Generic.Dictionary'2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Collections.Generic.Dictionary'2[TKey,TValue].Add (TKey key, TValue value) (at <599589bf4ce248909b8a14cbe4a2034e>:0)
