查看PlayerPrefs存储内容,及编辑存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
#if UNITY_EDITOR_WIN using Microsoft.Win32; using System; using System.Collections.Generic; using System.Text; using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine; public class PlayerPrefsEditor : EditorWindow { private enum PrefType { Float = 0, Int, String, Bool }; [Serializable] private struct PrefPair { public string Key { get; set; } public object Value { get; set; } } private static readonly Encoding encoding = new UTF8Encoding(); private static readonly DateTime missingDateTime = new DateTime(1601, 1, 1); private bool showEditorPrefs; private SearchField searchField; private List<PrefPair> deserializedPlayerPrefs = new List<PrefPair>(); private readonly List<PrefPair> filteredPlayerPrefs = new List<PrefPair>(); private DateTime? lastDeserialization; private Vector2 scrollPosition; private Vector2 lastScrollPosition; private int inspectorUpdateFrame; private string searchFilter = string.Empty; private string keyQueuedForDeletion; private PrefType newEntryType = PrefType.String; private string newEntryKey = ""; private float newEntryValueFloat; private int newEntryValueInt; private bool newEntryValueBool; private string newEntryValueString = ""; private void OnEnable () { searchField = new SearchField(); } [MenuItem("Window/PlayerPrefs Editor")] private static void OpenWindow () { var editor = GetWindow<PlayerPrefsEditor>("Prefs Editor", true); editor.titleContent = new GUIContent("Prefs Editor", EditorGUIUtility.IconContent("Settings").image); editor.minSize = new Vector2(230, 400); } private void OnGUI () { EditorGUILayout.Space(); DrawTopBar(); if (!lastDeserialization.HasValue || DateTime.UtcNow - lastDeserialization.Value > TimeSpan.FromMilliseconds(500)) { deserializedPlayerPrefs = new List<PrefPair>(RetrieveSavedPrefs(PlayerSettings.companyName, PlayerSettings.productName)); lastDeserialization = DateTime.UtcNow; } DrawMainList(); DrawAddEntry(); DrawBottomMenu(); EditorGUILayout.Space(); // If the user has scrolled, deselect - this is because control IDs within carousel will change when scrolled // so we'd end up with the wrong box selected. if (scrollPosition != lastScrollPosition) GUI.FocusControl(""); } private void OnInspectorUpdate () { // If a PlayerPref has been specified for deletion. if (!string.IsNullOrEmpty(keyQueuedForDeletion)) { // If the user just deleted a PlayerPref, find the ID and defer it for deletion by OnInspectorUpdate(). if (deserializedPlayerPrefs != null) { var entryCount = deserializedPlayerPrefs.Count; for (int i = 0; i < entryCount; i++) { if (deserializedPlayerPrefs[i].Key == keyQueuedForDeletion) { deserializedPlayerPrefs.RemoveAt(i); break; } } } // Remove the queued key since we've just deleted it. keyQueuedForDeletion = null; // Update the search results and repaint the window. UpdateSearch(); Repaint(); } else if (inspectorUpdateFrame % 10 == 0) // Once a second (every 10th frame) { // Force the window to repaint. Repaint(); } // Track what frame we're on, so we can call code less often. inspectorUpdateFrame++; } private void DeleteAll () { if (showEditorPrefs) EditorPrefs.DeleteAll(); else PlayerPrefs.DeleteAll(); } private void DeleteKey (string key) { if (showEditorPrefs) EditorPrefs.DeleteKey(key); else PlayerPrefs.DeleteKey(key); } private int GetInt (string key, int defaultValue = 0) { if (showEditorPrefs) return EditorPrefs.GetInt(key, defaultValue); return PlayerPrefs.GetInt(key, defaultValue); } private float GetFloat (string key, float defaultValue = 0.0f) { if (showEditorPrefs) return EditorPrefs.GetFloat(key, defaultValue); return PlayerPrefs.GetFloat(key, defaultValue); } private string GetString (string key, string defaultValue = "") { if (showEditorPrefs) return EditorPrefs.GetString(key, defaultValue); return PlayerPrefs.GetString(key, defaultValue); } private bool GetBool (string key, bool defaultValue = false) { if (showEditorPrefs) return EditorPrefs.GetBool(key, defaultValue); throw new NotSupportedException("PlayerPrefs interface does not natively support booleans."); } private void SetInt (string key, int value) { if (showEditorPrefs) EditorPrefs.SetInt(key, value); else PlayerPrefs.SetInt(key, value); } private void SetFloat (string key, float value) { if (showEditorPrefs) EditorPrefs.SetFloat(key, value); else PlayerPrefs.SetFloat(key, value); } private void SetString (string key, string value) { if (showEditorPrefs) EditorPrefs.SetString(key, value); else PlayerPrefs.SetString(key, value); } private void SetBool (string key, bool value) { if (showEditorPrefs) EditorPrefs.SetBool(key, value); else throw new NotSupportedException("PlayerPrefs interface does not natively support booleans."); } private void Save () { if (showEditorPrefs) { } else PlayerPrefs.Save(); } /// <summary> /// This returns an array of the stored PlayerPrefs from the Windows registry, to allow /// us to to look up what's actually in the PlayerPrefs. This is used as a kind of lookup table. /// </summary> private PrefPair[] RetrieveSavedPrefs (string companyName, string productName) { RegistryKey registryKey; if (showEditorPrefs) { var majorVersion = Application.unityVersion.Split('.')[0]; registryKey = Registry.CurrentUser.OpenSubKey("Software\\Unity Technologies\\Unity Editor " + majorVersion + ".x"); } // On Windows, PlayerPrefs are stored in the registry under HKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings. else registryKey = Registry.CurrentUser.OpenSubKey("Software\\Unity\\UnityEditor\\" + companyName + "\\" + productName); // No prefs saved for the project. if (registryKey is null) return Array.Empty<PrefPair>(); var valueNames = registryKey.GetValueNames(); var tempPlayerPrefs = new PrefPair[valueNames.Length]; for (int i = 0; i < valueNames.Length; i++) { var valueName = valueNames[i]; var key = valueNames[i]; // Remove the _h193410979 style suffix used on PlayerPref keys in Windows registry. var index = key.LastIndexOf("_", StringComparison.Ordinal); key = key.Remove(index, key.Length - index); var ambiguousValue = registryKey.GetValue(valueName); // Unfortunately floats will come back as an int (at least on 64 bit) because the float is stored as // 64 bit but marked as 32 bit - which confuses the GetValue() method greatly! if (ambiguousValue is int) { // If the PlayerPref is not actually an int then it must be a float, this will evaluate to true // (impossible for it to be 0 and -1 at the same time). if (GetInt(key, -1) == -1 && GetInt(key) == 0) ambiguousValue = GetFloat(key); // If it reports a non default value as a bool, it's a bool not a string. else if (showEditorPrefs && (GetBool(key, true) != true || GetBool(key))) ambiguousValue = GetBool(key); } else if (ambiguousValue.GetType() == typeof(byte[])) // On Unity 5 a string may be stored as binary, so convert it back to a string. ambiguousValue = encoding.GetString((byte[])ambiguousValue).TrimEnd('\0'); tempPlayerPrefs[i] = new PrefPair() { Key = key, Value = ambiguousValue }; } return tempPlayerPrefs; } private void UpdateSearch () { filteredPlayerPrefs.Clear(); if (string.IsNullOrEmpty(searchFilter)) return; var entryCount = deserializedPlayerPrefs.Count; for (int i = 0; i < entryCount; i++) { var fullKey = deserializedPlayerPrefs[i].Key; var displayKey = fullKey; if (displayKey.ToLower().Contains(searchFilter.ToLower())) filteredPlayerPrefs.Add(deserializedPlayerPrefs[i]); } } private void DrawTopBar () { var newSearchFilter = searchField.OnGUI(searchFilter); GUILayout.Space(4); // If the requested search filter has changed. if (newSearchFilter != searchFilter) { searchFilter = newSearchFilter; // Trigger UpdateSearch to calculate new search results. UpdateSearch(); } // Allow the user to toggle between editor and PlayerPrefs. var oldIndex = showEditorPrefs ? 1 : 0; var newIndex = GUILayout.Toolbar(oldIndex, new[] { "PlayerPrefs", "EditorPrefs" }); // Has the toggle changed? if (newIndex != oldIndex) { // Reset. lastDeserialization = null; showEditorPrefs = newIndex == 1; } } private void DrawMainList () { // The bold table headings. EditorGUILayout.BeginHorizontal(); GUILayout.Label("Key", EditorStyles.boldLabel); GUILayout.Label("Value", EditorStyles.boldLabel); GUILayout.Label("Type", EditorStyles.boldLabel, GUILayout.Width(37)); GUILayout.Label("Del", EditorStyles.boldLabel, GUILayout.Width(25)); EditorGUILayout.EndHorizontal(); var textFieldStyle = new GUIStyle(GUI.skin.textField); var activePlayerPrefs = deserializedPlayerPrefs; if (!string.IsNullOrEmpty(searchFilter)) activePlayerPrefs = filteredPlayerPrefs; int entryCount = activePlayerPrefs.Count; lastScrollPosition = scrollPosition; scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); if (scrollPosition.y < 0) scrollPosition.y = 0; // The following code has been optimised so that rather than attempting to draw UI for every single PlayerPref // it instead only draws the UI for those currently visible in the scroll view and pads above and below those // results to maintain the right size using GUILayout.Space(). This enables us to work with thousands of // PlayerPrefs without slowing the interface to a halt. var rowHeight = 18; var visibleCount = Mathf.CeilToInt((float)Screen.height / rowHeight); var firstShownIndex = Mathf.FloorToInt(scrollPosition.y / rowHeight); var shownIndexLimit = firstShownIndex + visibleCount; if (entryCount < shownIndexLimit) shownIndexLimit = entryCount; // If the number of displayed PlayerPrefs is smaller than the number we can display (like we're at the end // of the list) then move the starting index back to adjust. if (shownIndexLimit - firstShownIndex < visibleCount) firstShownIndex -= visibleCount - (shownIndexLimit - firstShownIndex); // Can't have a negative index of a first shown PlayerPref, so clamp to 0 if (firstShownIndex < 0) firstShownIndex = 0; // Pad above the on screen results so that we're not wasting draw calls on invisible UI and the drawn player // prefs end up in the same place in the list. GUILayout.Space(firstShownIndex * rowHeight); for (int i = firstShownIndex; i < shownIndexLimit; i++) { textFieldStyle.normal.textColor = GUI.skin.textField.normal.textColor; textFieldStyle.focused.textColor = GUI.skin.textField.focused.textColor; var fullKey = activePlayerPrefs[i].Key; var displayKey = fullKey; var deserializedValue = activePlayerPrefs[i].Value; EditorGUILayout.BeginHorizontal(); var valueType = deserializedValue.GetType(); EditorGUILayout.TextField(displayKey, textFieldStyle); if (valueType == typeof(float)) { var initialValue = GetFloat(fullKey); var newValue = EditorGUILayout.FloatField(initialValue, textFieldStyle); if (!Mathf.Approximately(newValue, initialValue)) { SetFloat(fullKey, newValue); Save(); } GUILayout.Label("float", GUILayout.Width(37)); } else if (valueType == typeof(int)) { var initialValue = GetInt(fullKey); int newValue = EditorGUILayout.IntField(initialValue, textFieldStyle); if (newValue != initialValue) { SetInt(fullKey, newValue); Save(); } GUILayout.Label("int", GUILayout.Width(37)); } else if (valueType == typeof(bool)) { var initialValue = GetBool(fullKey); var newValue = EditorGUILayout.Toggle(initialValue); if (newValue != initialValue) { SetBool(fullKey, newValue); Save(); } GUILayout.Label("bool", GUILayout.Width(37)); } else if (valueType == typeof(string)) { var initialValue = GetString(fullKey); var newValue = EditorGUILayout.TextField(initialValue, textFieldStyle); if (newValue != initialValue) { SetString(fullKey, newValue); Save(); } GUILayout.Label("string", GUILayout.Width(37)); } if (GUILayout.Button("X", GUILayout.Width(25))) { DeleteKey(fullKey); Save(); DeleteCachedRecord(fullKey); } EditorGUILayout.EndHorizontal(); } var bottomPadding = (entryCount - shownIndexLimit) * rowHeight; if (bottomPadding > 0) GUILayout.Space(bottomPadding); EditorGUILayout.EndScrollView(); GUILayout.Label("Entry Count: " + entryCount); var rect = GUILayoutUtility.GetLastRect(); rect.height = 1; rect.y -= 4; EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 0.5f)); } private void DrawAddEntry () { var textFieldStyle = new GUIStyle(GUI.skin.textField); EditorGUILayout.Space(); GUILayout.Label(showEditorPrefs ? "Add EditorPref" : "Add PlayerPref", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); if (showEditorPrefs) newEntryType = (PrefType)GUILayout.Toolbar((int)newEntryType, new[] { "float", "int", "string", "bool" }); else { if (newEntryType == PrefType.Bool) newEntryType = PrefType.String; newEntryType = (PrefType)GUILayout.Toolbar((int)newEntryType, new[] { "float", "int", "string" }); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); GUILayout.Label("Key", EditorStyles.boldLabel); GUILayout.Label("Value", EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); GUI.SetNextControlName("newEntryKey"); newEntryKey = EditorGUILayout.TextField(newEntryKey, textFieldStyle); GUI.SetNextControlName("newEntryValue"); switch (newEntryType) { case PrefType.Float: newEntryValueFloat = EditorGUILayout.FloatField(newEntryValueFloat, textFieldStyle); break; case PrefType.Int: newEntryValueInt = EditorGUILayout.IntField(newEntryValueInt, textFieldStyle); break; case PrefType.String: newEntryValueString = EditorGUILayout.TextField(newEntryValueString, textFieldStyle); break; case PrefType.Bool: newEntryValueBool = EditorGUILayout.Toggle(newEntryValueBool); break; } // If the user hit enter while either the key or value fields were being edited. var keyboardAddPressed = Event.current.isKey && Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyUp && (GUI.GetNameOfFocusedControl() == "newEntryKey" || GUI.GetNameOfFocusedControl() == "newEntryValue"); // If the user clicks the Add button or hits return (and there is a non-empty key), create the PlayerPref. if ((GUILayout.Button("Add", GUILayout.Width(40)) || keyboardAddPressed) && !string.IsNullOrEmpty(newEntryKey)) { if (newEntryType == PrefType.Float) { SetFloat(newEntryKey, newEntryValueFloat); CacheRecord(newEntryKey, newEntryValueFloat); } else if (newEntryType == PrefType.Int) { SetInt(newEntryKey, newEntryValueInt); CacheRecord(newEntryKey, newEntryValueInt); } else if (newEntryType == PrefType.Bool) { SetBool(newEntryKey, newEntryValueBool); CacheRecord(newEntryKey, newEntryValueBool); } else { SetString(newEntryKey, newEntryValueString); CacheRecord(newEntryKey, newEntryValueString); } Save(); Repaint(); newEntryKey = ""; newEntryValueFloat = 0; newEntryValueInt = 0; newEntryValueString = ""; // Deselect. GUI.FocusControl(""); } EditorGUILayout.EndHorizontal(); } private void DrawBottomMenu () { EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); float buttonWidth = (EditorGUIUtility.currentViewWidth - 10) / 2f; if (GUILayout.Button("Delete All Preferences", GUILayout.Width(buttonWidth))) { if (EditorUtility.DisplayDialog("Delete All?", "Are you sure you want to delete all preferences?", "Delete All", "Cancel")) { DeleteAll(); Save(); deserializedPlayerPrefs.Clear(); } } GUILayout.FlexibleSpace(); if (GUILayout.Button("Force Save", GUILayout.Width(buttonWidth))) Save(); EditorGUILayout.EndHorizontal(); } private void CacheRecord (string key, object value) { // First of all check if this key already exists, if so replace it's value with the new value/ var replaced = false; var entryCount = deserializedPlayerPrefs.Count; for (int i = 0; i < entryCount; i++) { if (deserializedPlayerPrefs[i].Key == key) { deserializedPlayerPrefs[i] = new PrefPair { Key = key, Value = value }; replaced = true; break; } } // PlayerPref doesn't already exist (and wasn't replaced) so add it as new. if (!replaced) { // Cache a PlayerPref the user just created so it can be instantly display (mainly for OSX) deserializedPlayerPrefs.Add(new PrefPair { Key = key, Value = value }); } // Update the search if it's active UpdateSearch(); } private void DeleteCachedRecord (string fullKey) { keyQueuedForDeletion = fullKey; } } #endif |
- 本文固定链接: http://www.u3d8.com/?p=2799
- 转载请注明: 网虫虫 在 u3d8.com 发表过