Skip to content

Dimitris-Klis/Jimms-Prefs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jimm's Prefs

A custom playerprefs system with a couple extra savable variable types! Showcase

Documentation

Storing Values

Jimm's Prefs works exactly like UnityEngine.PlayerPrefs:

  JimmsPrefs.SetInt("RandomInterger", 1);
  JimmsPrefs.SetString("RandomString", "Hello World!");
  JimmsPrefs.SetFloat("RandomFloat", 1.5f);

However, this system has some extra features! Most importantly, you can now store more variable types directly!

No more boolean to int conversion nonsense!

  JimmsPrefs.SetBool("IsTrue", true);

You can also store Vector2 & Vector3 positions!

  JimmsPrefs.SetVector2("Position2D", new(0, 5));
  JimmsPrefs.SetVector3("Position3D", new(0, 5, 3));


All of this is done with just 2 scripts! Also, instead of storing the data in the windows registry, JimmsPrefs stores it in a .json file!

Here's the code that handles the saving:

public string SaveName = "Jimm's Prefs.json";
void SavePrefs()
{
    // Creating the data and converting it to json.
    JimmsPrefsData data = new JimmsPrefsData(IntPairs, FloatPairs, BoolPairs, StringPairs, Vector2Pairs, Vector3Pairs);
    string jsonData = JsonUtility.ToJson(data, true);

    // Storing the data at Application.persistentDatPAth
    StreamWriter writer = new(Application.persistentDataPath + Path.DirectorySeparatorChar + SaveName, false);
    writer.Write(jsonData);
    writer.Close();
}

Loading Values

Here are the parameters for the GetInt() function.

JimmsPrefs.GetInt(string key, int DefaultValue); // If nothing is found, the DefaultValue gets returned instead.

Here's an example of each value being loaded:

  JimmsPrefs.GetInt("RandomInterger", 1);
  JimmsPrefs.GetString("RandomString", "Hello World!");
  JimmsPrefs.GetFloat("RandomFloat", 1.5f);

  JimmsPrefs.GetBool("IsTrue", true);
  JimmsPrefs.GetVector2("Position2D", new(0, 5));
  JimmsPrefs.GetVector3("Position3D", new(0, 5, 3));

Finally, Default Values are mandatory.

    JimmsPrefs.GetInt("RandomInterger"); // -> This will throw an error!

About

A custom playerprefs system with a couple extra savable variables!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors