Search Unity

[RELEASED] Free plugin for MBSCore : PlayerPrefs++

Discussion in 'Assets and Asset Store' started by MrDude, May 5, 2014.

  1. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    For those of you who already have the myBad Studios Core kit (available on the asset store), here is a nice little bonus for you..

    I call it PlayerPrefs++

    The first +:
    Now you can use playerprefs to store string, int, float, Rect, Vector3, Vector2, Quaternion, Color and bool types.

    The second +:
    You can optionally store your PlayerPrefs by category also. This means that should you need to delete a bunch of settings, you no longer have to either delete every single pref there is OR delete each pref you want one by one... For example, when having multiple save game slots in your game, simply store all settings for slot 1 under "Slot1" like so:
    Code (csharp):
    1.  
    2. float total_time_played = mbsPlayerPrefs.GetFloat("TTP") + Time.time;
    3. mbsPlayerPrefs.SetFloat("TTP", total_time_played);
    4.  
    5. mbsPlayerPrefs.SetBool("Complete", false, "Slot1")
    6. mbsPlayerPrefs.SetVector3("position", player.transform.position, "Slot1");
    7. mbsPlayerPrefs.SetQuaternion("rotation", player.transform.rotation, "Slot1");
    8.  
    ...and when you want to delete that save slot, just say:
    Code (csharp):
    1. mbsPlayerPrefs.DeleteAll("Slot1");
    Side notes:
    1. Technically it is possible to save any serializable object but that will require just a little bit of work on your end, though.
    2. By changing just two lines of code you can actually make the kit store the PlayerPrefs to file in case you ever feel like sharing it for whatever reason.
    3. Since the playerprefs are now presented in array form you could create a visual editor for them in absolutely no time... Simply write the arrays out and you are done... PlayerPrefs names are stored in Keys and the values in Values but Values is a property so I would recommend accessing it directly in the Dictionary like so (untested code):
    Code (csharp):
    1.  
    2. Rect window_area = new Rect(150,150,500,500);
    3. string[] categories;
    4. int tab_index = 0;
    5.  
    6. void Start()
    7. {
    8.     categories = new string[PlayerPref.Count];
    9.     for ( int i =0; i < categories.Length; i++)
    10.         categories[i] = playerprefs[i].data_type;
    11. }
    12.  
    13. void OnGUI()
    14. {
    15.     window_area = GUI.Window(0,window_area,window,"");
    16. }
    17.  
    18. void window(int id) {
    19.     GUILayout.BeginArea(new Rect(5f,40f,window_area.width-10f, window_area.height - 45f));
    20.     tab_index = GUILayout.ToolBar(tab_index, categories);
    21.     foreach(string key in playerprefs[tab_index].Keys)
    22.     {
    23.         GUILayout.BeginHorizontal();
    24.         GUILayout.Label(key); GUILayout.FlexibleSpace(20f);    
    25.         playerprefs[tab_index].defined[key] = GUILayout.TextField(playerprefs[tab_index].defined[key]);
    26.         GUILayout.EndHorizontal();
    27.     }
    28.     GUILayout.EndArea();
    29.     if (GUI.Button( new Rect(window_area.width - 65f, 5f, 60f, 30f), "Save") ) Save();
    30. }
    Well, there you have it... Provided as c# source code to download and enjoy :)

    http://www.mybadstudios.com/?page_id=1331