├── .github └── FUNDING.yml ├── Assets ├── PlayerPrefsExtra.meta ├── PlayerPrefsExtra │ ├── PlayerPrefsExtra.cs │ └── PlayerPrefsExtra.cs.meta ├── Scripts.meta ├── Scripts │ ├── Demo1.cs │ ├── Demo1.cs.meta │ ├── Demo2.cs │ ├── Demo2.cs.meta │ ├── Demo3.cs │ └── Demo3.cs.meta ├── scene.unity └── scene.unity.meta ├── LICENCE ├── PlayerPrefsExtra_v1.unitypackage └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ["https://paypal.me/hamzaherbou"] 2 | -------------------------------------------------------------------------------- /Assets/PlayerPrefsExtra.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f7d4bb3d7075f1a47847efd6066e6b3a 3 | folderAsset: yes 4 | timeCreated: 1593267586 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/PlayerPrefsExtra/PlayerPrefsExtra.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | /// 5 | /// PlayerPrefsExtra gives you more possibilities like saving complex types : 6 | /// Vectors, Colors, Quaternions, Lists, and Your Pre defined types (Object) [classes or structs]. 7 | /// 8 | /// Developed by Hamza Herbou 9 | /// -------------------------------------------------------------------------------------------------------- 10 | /// Email : hamza95herbou@gmail.com 11 | /// GITHUB : https://github.com/herbou/ 12 | /// 13 | /// 14 | 15 | public static class PlayerPrefsExtra 16 | { 17 | #region Bool ----------------------------------------------------------------------------------------- 18 | 19 | public static bool GetBool (string key) 20 | { 21 | return (PlayerPrefs.GetInt (key, 0) == 1); 22 | } 23 | 24 | public static bool GetBool (string key, bool defaultValue) 25 | { 26 | return (PlayerPrefs.GetInt (key, (defaultValue ? 1 : 0)) == 1); 27 | } 28 | 29 | public static void SetBool (string key, bool value) 30 | { 31 | PlayerPrefs.SetInt (key, (value ? 1 : 0)); 32 | } 33 | 34 | #endregion 35 | 36 | #region Vector 2 ----------------------------------------------------------------------------------------- 37 | 38 | public static Vector2 GetVector2 (string key) 39 | { 40 | return Get (key, Vector2.zero); 41 | } 42 | 43 | public static Vector2 GetVector2 (string key, Vector2 defaultValue) 44 | { 45 | return Get (key, defaultValue); 46 | } 47 | 48 | public static void SetVector2 (string key, Vector2 value) 49 | { 50 | Set (key, value); 51 | } 52 | 53 | #endregion 54 | 55 | #region Vector 3 ----------------------------------------------------------------------------------------- 56 | 57 | public static Vector3 GetVector3 (string key) 58 | { 59 | return Get (key, Vector3.zero); 60 | } 61 | 62 | public static Vector3 GetVector3 (string key, Vector3 defaultValue) 63 | { 64 | return Get (key, defaultValue); 65 | } 66 | 67 | public static void SetVector3 (string key, Vector3 value) 68 | { 69 | Set (key, value); 70 | } 71 | 72 | #endregion 73 | 74 | #region Vector 4 ----------------------------------------------------------------------------------------- 75 | 76 | public static Vector4 GetVector4 (string key) 77 | { 78 | return Get (key, Vector4.zero); 79 | } 80 | 81 | public static Vector4 GetVector4 (string key, Vector4 defaultValue) 82 | { 83 | return Get (key, defaultValue); 84 | } 85 | 86 | public static void SetVector4 (string key, Vector4 value) 87 | { 88 | Set (key, value); 89 | } 90 | 91 | #endregion 92 | 93 | #region Color ----------------------------------------------------------------------------------------- 94 | 95 | public static Color GetColor (string key) 96 | { 97 | return Get (key, new Color (0f, 0f, 0f, 0f)); 98 | } 99 | 100 | public static Color GetColor (string key, Color defaultValue) 101 | { 102 | return Get (key, defaultValue); 103 | } 104 | 105 | public static void SetColor (string key, Color value) 106 | { 107 | Set (key, value); 108 | } 109 | 110 | #endregion 111 | 112 | #region Quaternion ----------------------------------------------------------------------------------------- 113 | 114 | public static Quaternion GetQuaternion (string key) 115 | { 116 | return Get (key, Quaternion.identity); 117 | } 118 | 119 | public static Quaternion GetQuaternion (string key, Quaternion defaultValue) 120 | { 121 | return Get (key, defaultValue); 122 | } 123 | 124 | public static void SetQuaternion (string key, Quaternion value) 125 | { 126 | Set (key, value); 127 | } 128 | 129 | #endregion 130 | 131 | #region List ----------------------------------------------------------------------------------------- 132 | 133 | public class ListWrapper 134 | { 135 | public List list = new List (); 136 | } 137 | 138 | public static List GetList (string key) 139 | { 140 | return Get> (key, new ListWrapper ()).list; 141 | } 142 | 143 | public static List GetList (string key, List defaultValue) 144 | { 145 | return Get> (key, new ListWrapper { list = defaultValue }).list; 146 | } 147 | 148 | public static void SetList (string key, List value) 149 | { 150 | Set (key, new ListWrapper { list = value }); 151 | } 152 | 153 | #endregion 154 | 155 | 156 | #region Object ----------------------------------------------------------------------------------------- 157 | 158 | public static T GetObject (string key) 159 | { 160 | return Get (key, default(T)); 161 | } 162 | 163 | public static T GetObject (string key, T defaultValue) 164 | { 165 | return Get (key, defaultValue); 166 | } 167 | 168 | public static void SetObject (string key, T value) 169 | { 170 | Set (key, value); 171 | } 172 | 173 | #endregion 174 | 175 | //Generic template --------------------------------------------------------------------------------------- 176 | 177 | static T Get (string key, T defaultValue) 178 | { 179 | return JsonUtility.FromJson (PlayerPrefs.GetString (key, JsonUtility.ToJson (defaultValue))); 180 | } 181 | 182 | static void Set (string key, T value) 183 | { 184 | PlayerPrefs.SetString (key, JsonUtility.ToJson (value)); 185 | } 186 | 187 | } 188 | -------------------------------------------------------------------------------- /Assets/PlayerPrefsExtra/PlayerPrefsExtra.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a202e5c97cdd34742931ef81a630c4dd 3 | timeCreated: 1593266403 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3c2ee21c70f29ca45824f14d6efddf41 3 | folderAsset: yes 4 | timeCreated: 1600786977 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo1.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class Demo1 : MonoBehaviour { 4 | //Data 5 | public Vector2 pos; 6 | public Color col; 7 | public Quaternion rot; 8 | 9 | void Start ( ) { 10 | //Load data 11 | pos = PlayerPrefsExtra.GetVector2 ( "Pos", Vector2.zero ); 12 | col = PlayerPrefsExtra.GetColor ( "Col", Color.white ); 13 | rot = PlayerPrefsExtra.GetQuaternion ( "Rot", Quaternion.identity ); 14 | } 15 | 16 | void Update ( ) { 17 | //Update data only on mouse click 18 | if ( Input.GetMouseButtonUp ( 0 ) ) { 19 | //Update pos & save data: 20 | pos = new Vector2 ( pos.x + 1f, pos.y + 5f ); 21 | PlayerPrefsExtra.SetVector2 ( "Pos", pos ); 22 | 23 | //Update col & save 24 | col = new Color ( 25 | Random.Range ( 0f, 1f ), 26 | Random.Range ( 0f, 1f ), 27 | Random.Range ( 0f, 1f ), 28 | 1f 29 | ); 30 | PlayerPrefsExtra.SetColor ( "Col", col ); 31 | 32 | //Update & save rot 33 | rot = new Quaternion ( 34 | rot.x + 1f, 35 | rot.y + 5f, 36 | rot.z + 10f, 37 | rot.w + 15f 38 | ); 39 | PlayerPrefsExtra.SetQuaternion ( "Rot", rot ); 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo1.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: acbd0ab430d896d4887244a2ec453d5d 3 | timeCreated: 1600786986 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo2.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Demo2 : MonoBehaviour { 5 | //Data 6 | public List listInt; 7 | 8 | void Start ( ) { 9 | //Load data 10 | listInt = PlayerPrefsExtra.GetList ( "listInt", new List ( ) ); 11 | } 12 | 13 | void Update ( ) { 14 | //Update & save data 15 | if ( Input.GetMouseButtonUp ( 0 ) ) { 16 | listInt.Add ( Random.Range ( 0, 100 ) ); 17 | PlayerPrefsExtra.SetList ( "listInt", listInt ); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo2.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 06cb0b6364003e0498efa5bef1da22a1 3 | timeCreated: 1600787589 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo3.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | [System.Serializable] public class Shape { 4 | public int points = 0; 5 | public Vector2 pos = Vector2.zero; 6 | } 7 | 8 | public class Demo3 : MonoBehaviour { 9 | //Data 10 | public Shape myShape; 11 | 12 | void Start ( ) { 13 | //Load data 14 | myShape = PlayerPrefsExtra.GetObject ( "MyShape", new Shape ( ) ); 15 | } 16 | 17 | void Update ( ) { 18 | if ( Input.GetMouseButtonUp ( 0 ) ) { 19 | //Update & save data 20 | myShape.points += 3; 21 | myShape.pos = myShape.pos + Vector2.one; 22 | 23 | PlayerPrefsExtra.SetObject ( "MyShape", myShape ); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Assets/Scripts/Demo3.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e23c93927315d6747a39f7a61a1cf9a6 3 | timeCreated: 1600787947 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/scene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/UnityPlayerPrefsExtra/d1684fbd6a9002daea054a86e78190c3ae1dfad6/Assets/scene.unity -------------------------------------------------------------------------------- /Assets/scene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c37109e4b7a2a814c828e3bd84ab117c 3 | timeCreated: 1600785138 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Hamza Herbou 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /PlayerPrefsExtra_v1.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/UnityPlayerPrefsExtra/d1684fbd6a9002daea054a86e78190c3ae1dfad6/PlayerPrefsExtra_v1.unitypackage -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity PlayerPrefsExtra 2 | ### Video tutorial : [https://youtu.be/wqRYdFcfiVw](https://youtu.be/wqRYdFcfiVw) 3 | ![Video thumbnail](https://img.youtube.com/vi/wqRYdFcfiVw/0.jpg) 4 | 5 | 6 | Unity PlayerPrefsExtra gives you the ability to save more complexe data types such as : 7 | Vectors, Bool, Colors, Lists, ... 8 | and it uses the Unity's PlayerPrefs under the hood. 9 | 10 | # ⁉ How to use : 11 | Use the same syntaxe as PlayerPrefs. 12 | 13 | 14 | ## ■ Booleans : 15 | ```c# 16 | //Load 17 | bool b = PlayerPrefsExtra.GetBool("mybool", false); 18 | 19 | //Update (flip value) 20 | b = !b; 21 | 22 | //Save 23 | PlayerPrefsExtra.SetBool("mybool", b); 24 | ``` 25 | 26 |
27 | 28 | ## ■ Vectors ( Vector2, Vector3, and Vector4 ): 29 | ```c# 30 | //Load 31 | Vector2 v = PlayerPrefsExtra.GetVector2("myV2", Vector2.zero); 32 | 33 | //Update 34 | v+=Vector2.one; 35 | 36 | //Save 37 | PlayerPrefsExtra.SetVector2("myV2", v); 38 | ``` 39 | 40 |
41 | 42 | ## ■ Colors : 43 | ```c# 44 | // Get color 45 | Color c = PlayerPrefsExtra.GetColor("Col"); 46 | 47 | // Set color 48 | PlayerPrefsExtra.SetColor("Col", Color.red); 49 | ``` 50 | 51 |
52 | 53 | ## ■ Quaternions : 54 | ```c# 55 | // Get Quaternion 56 | Quaternion qua = PlayerPrefsExtra.GetQuaternion("q"); 57 | 58 | //Set Quaternion 59 | PlayerPrefsExtra.SetQuaternion("q", qua); 60 | ``` 61 | 62 |
63 | 64 | ## ■ Lists : 65 | ```c# 66 | // Get List 67 | List list = PlayerPrefsExtra.GetList("myList", new List()); 68 | 69 | // Add data to List 70 | list.Add(Random.Range(100,900); 71 | 72 | // Save List 73 | PlayerPrefsExtra.SetList("myList", list); 74 | ``` 75 | 76 |
77 | 78 | ## ■ Objects : 79 | ```c# 80 | //Class 81 | [System.Serializable] 82 | public class Shape{ 83 | public int totalPoints = 3; 84 | public float strokeWidth = 0f; 85 | public List points = new List(); 86 | } 87 | ``` 88 | ```c# 89 | // Get object 90 | Shape s = PlayerPrefsExtra.GetObject("myShape", new Shape()); 91 | 92 | // Update object data 93 | s.strokeWidth++; 94 | s.points.Add(Vector3.one*Random.Range(0f,3f)); 95 | 96 | // Save object 97 | PlayerPrefsExtra.SetObject("myShape", s); 98 | ``` 99 | 100 |
101 | 102 | ## ■ Delete All Keys (both PlayerPrefs & PlayerPrefsExtra) : 103 | use PlayerPrefs instead of PlayerPrefsExtra 104 | ```c# 105 | PlayerPrefs.DeleteAll(); 106 | ``` 107 | 108 |
109 | 110 | ## ■ Delete one key : 111 | use PlayerPrefs instead of PlayerPrefsExtra 112 | ```c# 113 | PlayerPrefs.DeleteKey("Key"); 114 | ``` 115 | 116 |
117 | 118 | ## ■ Check key existance : 119 | use PlayerPrefs instead of PlayerPrefsExtra 120 | ```c# 121 | PlayerPrefs.HasKey("Key"); 122 | ``` 123 | 124 | 125 |

126 |
127 | ## ❤️ Donate 128 | Paypal 129 | --------------------------------------------------------------------------------