Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. //MIT License
  2.  
  3. //Copyright (c) 2019 William Herrera
  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.
  22.  
  23. using System.Collections;
  24. using System.Collections.Generic;
  25. using System.Text;
  26. using UnityEngine;
  27.  
  28. namespace OpenScaffolding
  29. {
  30. public static class PlayerPrefsEx
  31. {
  32. public static void AddInt(string key, int val, int defaultValue = 0)
  33. {
  34. int v = PlayerPrefs.GetInt(key, defaultValue);
  35.  
  36. PlayerPrefs.SetInt(key, v + val);
  37. }
  38.  
  39. public static void AddFloat(string key, float val, float defaultValue = 0f)
  40. {
  41. float v = PlayerPrefs.GetFloat(key, defaultValue);
  42.  
  43. PlayerPrefs.SetFloat(key, v + val);
  44. }
  45.  
  46. public static void Clear(string key)
  47. {
  48. PlayerPrefs.SetString(key, string.Empty);
  49. }
  50.  
  51. public static bool[] GetBools(string key)
  52. {
  53. string values = PlayerPrefs.GetString(key, string.Empty);
  54.  
  55. if (values.Length == 0)
  56. return new bool[] { };
  57.  
  58. bool[] result = new bool[values.Length];
  59.  
  60. for (int i = 0; i < values.Length; i++)
  61. {
  62. result[i] = values[i] == '1' ? true : false;
  63. }
  64.  
  65. return result;
  66. }
  67.  
  68. public static void SetBools(string key, bool[] values)
  69. {
  70. StringBuilder stringBuilder = new StringBuilder(values.Length);
  71.  
  72. foreach (bool value in values)
  73. stringBuilder.Append(value ? "1" : "0");
  74.  
  75. string v = stringBuilder.ToString();
  76.  
  77. PlayerPrefs.SetString(key, v);
  78. }
  79.  
  80. public static void SetBools(string key, int count, bool value)
  81. {
  82. StringBuilder stringBuilder = new StringBuilder(count);
  83.  
  84. for (int i = 0; i < count; i++)
  85. stringBuilder.Append(value ? "1" : "0");
  86.  
  87. string v = stringBuilder.ToString();
  88.  
  89. PlayerPrefs.SetString(key, v);
  90. }
  91. }
  92.  
  93. public class Manager<T> : MonoBehaviour where T : MonoBehaviour
  94. {
  95. static T _instance;
  96.  
  97. public static T Instance
  98. {
  99. get
  100. {
  101. if (_instance == null)
  102. {
  103. _instance = FindObjectOfType<T>();
  104. }
  105. return _instance;
  106. }
  107. set
  108. {
  109. _instance = value;
  110. }
  111. }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement