Thursday, October 31, 2013

UNITY3D PlayerPrefs viewer window

/*
    Copyright 2013, Aleksander Hristov, boxcollider@gmail.com .

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using UnityEngine;
using System;
using Microsoft.Win32;
using UnityEditor;

//Class responsible for viewing PlayerPrefs in Editor Window .
//Place this script in folder named "Editor".
//Click on menu bar on the top PlayerPrefs->Viewer

public class PlayerPrefsViewer : EditorWindow { 
 
 string[] appKeyNames; 
 string[] appValueData;
 int prefsElementNumber=0;
 Vector2 scrollInitialPosition =Vector2.zero;
 
 [MenuItem ("PlayerPrefs/Viewer")]
 static void Starti () { 
  
    EditorWindow.GetWindow<PlayerPrefsViewer>("PlayerPrefs");
 }
 
 void OnEnable(){
   getPlayerPrefs();
 }
 
 void getPlayerPrefs(){  
  
  RegistryKey appProductName = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey(PlayerSettings.companyName).OpenSubKey(PlayerSettings.productName,true);
  if(appProductName==null){
   return;
  }  
  
  prefsElementNumber=appProductName.ValueCount;  
  
  if(appProductName.ValueCount<=0){
   return;
  }
  
  appKeyNames=new string[ prefsElementNumber];  
  appValueData=new string[ prefsElementNumber];  
    
  appKeyNames=appProductName.GetValueNames();
  
  for(int i=0;i<prefsElementNumber;i++){  
    
     appKeyNames[i] = fromRegistryKeyToUnityKey(appKeyNames[i]);
     appValueData[i]= fromRegistryValueToUnityValue(appKeyNames[i]);
   
  }  
 } 
 
 void OnGUI () {   
  
       if(prefsElementNumber<=0){
   EditorGUILayout.BeginVertical();
   showRefreshButton();
   EditorGUILayout.LabelField("PLAYERPREFS EMPTY");   
   EditorGUILayout.EndVertical();
   return;
  }
  
  EditorGUILayout.BeginVertical(GUILayout.ExpandHeight(true));
  
   scrollInitialPosition=EditorGUILayout.BeginScrollView(scrollInitialPosition,false,true);
      showRefreshButton();
  
   EditorGUILayout.Space();
  
    for(int i=0;i<prefsElementNumber;i++){     
    
     EditorGUILayout.BeginHorizontal();
            
      EditorGUILayout.SelectableLabel(appKeyNames[i]);
             
      EditorGUILayout.SelectableLabel(appValueData[i]);
   
      GUI.backgroundColor=Color.red;
   
      if(GUILayout.Button("delete preference")){       
       PlayerPrefs.DeleteKey(appKeyNames[i]);
        getPlayerPrefs();    
      }
   
     EditorGUILayout.EndHorizontal(); 
    }  
   
   EditorGUILayout.Space();
  
   GUI.backgroundColor=Color.blue;
  
   if(GUILayout.Button("Delete all preferences",GUILayout.ExpandWidth(true))){
      PlayerPrefs.DeleteAll();
       prefsElementNumber=0;
    getPlayerPrefs();
   }
 
   EditorGUILayout.EndScrollView();
  
  EditorGUILayout.EndVertical();  
 }

    void showRefreshButton(){
   GUI.backgroundColor=Color.green;
   if(GUILayout.Button("Refresh player preferences",GUILayout.ExpandWidth(true))){
    getPlayerPrefs();
   }  
 } 
 
 string fromRegistryValueToUnityValue(string prefKey){
  
  if(PlayerPrefs.GetInt(prefKey,int.MinValue)!=int.MinValue){   
   return PlayerPrefs.GetInt(prefKey).ToString();
  }
  if(PlayerPrefs.GetFloat(prefKey,float.MinValue)!=float.MinValue){
   return PlayerPrefs.GetFloat(prefKey).ToString();   
  }  
  return PlayerPrefs.GetString(prefKey);
 }
 
 string fromRegistryKeyToUnityKey(string key){
  return key.Remove(key.LastIndexOf("_"));
 }

}

No comments:

Post a Comment