• Unity PlayerPrefs相关应用


    PlayerPrefs是Unity游戏引擎中的一个类,用于在游戏中存储和访问玩家的偏好设置和数据。它可以用来保存玩家的游戏进度、设置选项、最高分数等信息。PlayerPrefs将数据存储在本地文件中,因此可以在游戏重新启动时保持数据的持久性。

    1. //PlayerPrefs的数据存储 类似于键值对存储 一个键对应一个值
    2. //提供了存储3种数据的方法 int float string
    3. //键: string类型
    4. //值:int float string 对应3种API
    5. PlayerPrefs.SetInt("myAge", 18);
    6. PlayerPrefs.SetFloat("myHeight", 177.5f);
    7. PlayerPrefs.SetString("myName", "TonyChang");
    8. //直接调用Set相关方法 只会把数据存到内存里
    9. //当游戏结束时 Unity会自动把数据存到硬盘中
    10. //如果游戏不是正常结束的 而是崩溃 数据是不会存到硬盘中的
    11. //只要调用该方法 就会马上存储到硬盘中
    12. PlayerPrefs.Save();
    13. //PlayerPrefs是有局限性的 它只能存3种类型的数据
    14. //如果你想要存储别的类型的数据 只能降低精度 或者上升精度来进行存储
    15. bool sex = true;
    16. PlayerPrefs.SetInt("sex", sex ? 1 : 0);
    17. //如果不同类型用同一键名进行存储 会进行覆盖
    18. PlayerPrefs.SetFloat("myAge", 20.2f);
    19. //注意 运行时 只要你Set了对应键值对
    20. //即使你没有马上存储Save在本地
    21. //也能够读取出信息
    22. //int
    23. int age = PlayerPrefs.GetInt("myAge");
    24. print(age);
    25. //前提是 如果找不到myAge对应的值 就会返回函数的第二个参数 默认值
    26. age = PlayerPrefs.GetInt("myAge", 100);
    27. print(age);
    28. //float
    29. float height = PlayerPrefs.GetFloat("myHeight", 1000f);
    30. print(height);
    31. //string
    32. string name = PlayerPrefs.GetString("myName");
    33. print(name);
    34. //第二个参数 默认值 对于我们的作用
    35. //就是 在得到没有的数据的时候 就可以用它来进行基础数据的初始化
    36. //判断数据是否存在
    37. if( PlayerPrefs.HasKey("myName") )
    38. {
    39. print("存在myName对应的键值对数据");
    40. }
    41. //删除指定键值对
    42. PlayerPrefs.DeleteKey("myAge");
    43. //删除所有存储的信息
    44. PlayerPrefs.DeleteAll();
    PlayerPrefs存储工具类:

    为了方便进行数据的存储,使用PlayerPrefs中进行存储方法的设置的存取!

    主要实现功能是数据的读和数据的取~ 通过反射进行数据类型的获取,利用PlayerPrefs进行数据存储。

    1. using System;
    2. using System.Collections;
    3. using System.Reflection;
    4. using UnityEngine;
    5. namespace Framwork
    6. {
    7. ///
    8. /// Playerprefs 存储类
    9. ///
    10. public class PlayerPrefsManager
    11. {
    12. private static PlayerPrefsManager instance=new PlayerPrefsManager();
    13. public static PlayerPrefsManager Instance => instance;
    14. private PlayerPrefsManager()
    15. {
    16. }
    17. ///
    18. /// 存取数据的方法
    19. ///
    20. /// 数据实体
    21. /// 数据名称
    22. public void SaveData(object data, string keyName)
    23. {
    24. Type type = data.GetType();
    25. FieldInfo[] infos = type.GetFields();
    26. string tempKey="null";
    27. FieldInfo tempInfo = null;
    28. for (int i = 0; i < infos.Length; i++)
    29. {
    30. //获取数据数据类型
    31. tempInfo = infos[i];
    32. Debug.Log("Types==="+tempInfo);
    33. //类的名字+类的类型 + 数据内容名字+数据类型
    34. //作为存储的keyName键
    35. tempKey = keyName + "_" + type.Name + "_" + tempInfo.Name
    36. + "_" + tempInfo.FieldType.Name;
    37. SaveValue(tempInfo.GetValue(data),tempKey);
    38. }
    39. //进行值的获取
    40. //tempInfo.GetValue(data);
    41. PlayerPrefs.Save();
    42. }
    43. ///
    44. /// 读取数据的类型
    45. ///
    46. /// 要读取的数据类型
    47. /// 要读取的数据名称
    48. /// 返回数据实体
    49. public object LoadData(Type type, string name)
    50. {
    51. //获取数据中的类型
    52. FieldInfo[] infos = type.GetFields();
    53. //创建存储数据信息的实体
    54. object data = Activator.CreateInstance(type);
    55. string tempName = null;
    56. FieldInfo tempInfo = null;
    57. for (int i = 0; i < infos.Length; i++)
    58. {
    59. tempInfo = infos[i];//数据结构中的数据名称
    60. tempName = name + "_" + type.Name + "_" +tempInfo.Name+"_"
    61. +tempInfo.FieldType.Name;//数据结构中的数据名称类型
    62. //装载的容器 容器中的数据
    63. //进行数据装载
    64. tempInfo.SetValue(data,LoadValue(tempInfo.FieldType,tempName));
    65. }
    66. return data;
    67. }
    68. ///
    69. /// 进行具体的类型数据的存储
    70. ///
    71. ///
    72. ///
    73. private void SaveValue(object value, string keyName)
    74. {
    75. Type fieldType = value.GetType();
    76. if (fieldType == typeof(int))
    77. {
    78. Debug.Log("存储int"+value);
    79. PlayerPrefs.SetInt(keyName,(int)value);
    80. }else if (fieldType == typeof(float))
    81. {
    82. Debug.Log("存储float"+value);
    83. PlayerPrefs.SetFloat(keyName,(float)value);
    84. }else if (fieldType == typeof(string))
    85. {
    86. Debug.Log("存储string"+value);
    87. PlayerPrefs.SetString(keyName,value.ToString());
    88. }
    89. //对于List存储的设置
    90. //根据存储的字段类型和IList是否是父子关系
    91. else if(typeof(IList).IsAssignableFrom(fieldType))
    92. {
    93. //父类装子类
    94. IList list=value as IList;
    95. //存储元素数量
    96. PlayerPrefs.SetInt(keyName,list.Count);
    97. Debug.Log("存储List长度为"+list.Count);
    98. int index = 0;
    99. foreach (var obj in list)
    100. {
    101. //存储list列表中元素内容
    102. //命名形式是 list名字+索引编号
    103. //递归调用存储
    104. SaveValue(obj,keyName+index);
    105. index++;
    106. }
    107. }else if (typeof(IDictionary).IsAssignableFrom(fieldType))
    108. {
    109. IDictionary dictionary = value as IDictionary;
    110. //存储数据个数
    111. PlayerPrefs.SetInt(keyName,dictionary.Count);
    112. Debug.Log("存储Dic长度为"+dictionary.Count);
    113. int index = 0;
    114. foreach (var key in dictionary.Keys)
    115. {
    116. //存储键
    117. SaveValue(key,keyName+"_key_"+index);
    118. //存储值
    119. SaveValue(dictionary[key],keyName+"_value_"+index);
    120. index++;
    121. }
    122. }//自定义数据类型的存储 进行解析
    123. else
    124. {
    125. SaveData(value,keyName);
    126. }
    127. }
    128. private object LoadValue(Type type, string name)
    129. {
    130. if (type == typeof(int))
    131. {
    132. return PlayerPrefs.GetInt(name,0);
    133. }else if (type == typeof(float))
    134. {
    135. return PlayerPrefs.GetFloat(name,0.0f);
    136. }else if (type == typeof(string))
    137. {
    138. return PlayerPrefs.GetString(name,"");
    139. }else if (typeof(IList).IsAssignableFrom(type))
    140. {
    141. //读取列表
    142. int count = PlayerPrefs.GetInt(name);
    143. IList tempList=Activator.CreateInstance(type) as IList;
    144. for (int i = 0; i < count; i++)
    145. {
    146. //获取List中存储元素的类型 type.GetGenericArguments()[0]
    147. tempList.Add(LoadValue(type.GetGenericArguments()[0],name+i));
    148. }
    149. return tempList;
    150. }else if (typeof(IDictionary).IsAssignableFrom(type))
    151. {
    152. //进行对字典的读取
    153. int count = PlayerPrefs.GetInt(name);
    154. IDictionary tempDictionary=Activator.CreateInstance(type) as IDictionary;
    155. for (int i = 0; i < count; i++)
    156. {
    157. tempDictionary.Add(LoadValue(type.GetGenericArguments()[0], name + "_key_" + i),
    158. LoadValue(type.GetGenericArguments()[1], name + "_value_" + i));
    159. }
    160. return tempDictionary;
    161. }
    162. else
    163. {
    164. //读取自定义类成员的设置
    165. return LoadData(type, name);
    166. }
    167. }
    168. }
    169. }

  • 相关阅读:
    《JavaSE-第十七章》之LinkedList
    postgreSQL15发布,难掩激动快速尝鲜
    web前端期末大作业:基于HTML+CSS+JavaScript制作我的音乐网站(带设计报告)
    MyBatis操作数据库的方式(api+注解)
    算法2:链表的逆转
    MySQL的Undo Log、Redo Log和Binlog
    gitlab 通过变量连接自建K8S
    JNI的简单使用(Eclipse)
    Android设计模式--原型模式
    Linux ——repo下载与使用方法详解
  • 原文地址:https://blog.csdn.net/xdpcxq/article/details/134526342