Skip to content

Instantly share code, notes, and snippets.

@ysalihtuncel
Created April 5, 2025 18:44
Show Gist options
  • Save ysalihtuncel/2feb02e0f45ebbf80a419c0ee3ba6474 to your computer and use it in GitHub Desktop.
Save ysalihtuncel/2feb02e0f45ebbf80a419c0ee3ba6474 to your computer and use it in GitHub Desktop.
PlayerPrefs Save&Load
using UnityEngine;
public static class SaveLoad
{
public static void SaveInt(string key, int value) => PlayerPrefs.SetInt(key, value);
public static void SaveFloat(string key, float value) => PlayerPrefs.SetFloat(key, value);
public static void SaveString(string key, string value) => PlayerPrefs.SetString(key, value);
public static int LoadInt(string key, int defaultValue = 0) => PlayerPrefs.GetInt(key, defaultValue);
public static float LoadFloat(string key, float defaultValue = 0f) => PlayerPrefs.GetFloat(key, defaultValue);
public static string LoadString(string key, string defaultValue = "") => PlayerPrefs.GetString(key, defaultValue);
public static bool HasKey(string key) => PlayerPrefs.HasKey(key); // Truee if the key exists
public static void DeleteKey(string key) => PlayerPrefs.DeleteKey(key); // Deletes the key
public static void DeleteAll() => PlayerPrefs.DeleteAll(); // Deletes all keys
public static void Save() => PlayerPrefs.Save(); // Saves all changes to PlayerPrefs
public static void SaveLocation(Transform t)
{
SaveFloat("PlayerPosX", t.position.x);
SaveFloat("PlayerPosY", t.position.y);
SaveFloat("PlayerPosZ", t.position.z);
SaveFloat("PlayerRotX", t.eulerAngles.x);
SaveFloat("PlayerRotY", t.eulerAngles.y);
SaveFloat("PlayerRotZ", t.eulerAngles.z);
Save();
}
public static void LoadLocation(out Vector3 pos, out Vector3 rot)
{
Vector3 _pos = new Vector3(LoadFloat("PlayerPosX"), LoadFloat("PlayerPosY"), LoadFloat("PlayerPosZ"));
Vector3 _rot = new Vector3(LoadFloat("PlayerRotX"), LoadFloat("PlayerRotY"), LoadFloat("PlayerRotZ"));
pos = _pos;
rot = _rot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment