• Unity存档系统——Json格式的文件


    实例场景

     点击Save按钮后,查看保存的文件

    点击Load按钮后加载文档数据

    Json介绍https://www.json.org/json-zh.htmlUnity中自带的JsonUtility可以将可序列化对象与Json格式相互转换。

    将对象转为可序列化对象需要添加[SerializeField],且为public,然后才可以被转为Json格式。

    JsonUtility内部API

    JsonUtility.ToJson将object对象转为Json格式

    1. //两种重载
    2. public static string ToJson(object obj);
    3. public static string ToJson(object obj, bool prettyPrint);
    4. //obj为被转换为Json格式的对象
    5. //prettyPrint为是否将输出的Json文本转为适合阅读的格式,默认false,尽量不选true,对性能有影响
    6. //返回值为Json格式的string数据

    JsonUtility.FromJson将Json格式转为object格式

    1. public static T FromJson<T>(string json);
    2. //T为泛型,代各类数据
    3. //json为json格式的数据
    4. //返回值为某格式的对象

     JsonUtility.FromJsonOverwrite通过读取对象的 JSON 表示形式覆盖其数据

    1. public static void FromJsonOverwrite(string json, object objectToOverwrite);
    2. //json为对象的json格式
    3. //objectToOverwrite为被重写的对象

    这个方法与JsonUtility.FromJson不同在于:不产生新的对象加载Json格式,而是在已有的对象内加载Json格式,无需进行任何分配即可更新存储在类或对象中的值。

    输入输出流

    命名空间:using System.IO

    File.WriteAllText 写入文件

    1. public static void WriteAllText (string path, string ?contents);
    2. //path:文件路径
    3. //contents:文件内容

    path一般指定为:Application.persistentDataPath,避免平台不同发生错误

    该值是目录路径;此目录中可以存储每次运行要保留的数据。在 iOS 和 Android 上发布时,persistentDataPath 指向设备上的公共目录。应用程序更新不会擦除此位置中的文件。用户仍然可以直接擦除这些文件。 

    如果文件已经存在,则会将第二次输入的内容覆盖到原文件中,不会创建新文件。 

    File.ReadAllText 读取文件

    1. public static string ReadAllText (string path);
    2. //path:文件路径
    3. //返回值为json格式的字符串

    File.ReadAllText 读取文件

    public static void ReadAllText (string path);

    try—catch语句 

    源码

    PlayerSystem

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class PlayerSystem : MonoBehaviour
    6. {
    7. public List<Text> text = new List<Text>();
    8. [SerializeField] string playerName;
    9. [SerializeField] string playerMoney;
    10. [SerializeField] string playerLevel;
    11. [SerializeField] string playerScore;
    12. const string PLAYER_FILE_NAME = "playerFile";
    13. void Update() {
    14. text[0].text = playerName;
    15. text[1].text = playerMoney;
    16. text[2].text = playerLevel;
    17. text[3].text = playerScore;
    18. }
    19. public void Save(){
    20. var player = new Player();
    21. player.playerName = playerName;
    22. player.playerMoney = playerMoney;
    23. player.playerLevel = playerLevel;
    24. player.playerScore = playerScore;
    25. SaveFile.SaveByJson(PLAYER_FILE_NAME,player);
    26. }
    27. public void Load(){
    28. Player savePlayer = SaveFile.LoadFromJson<Player>(PLAYER_FILE_NAME);
    29. playerName = savePlayer.playerName;
    30. playerMoney = savePlayer.playerMoney;
    31. playerLevel = savePlayer.playerLevel;
    32. playerScore = savePlayer.playerScore;
    33. }
    34. [UnityEditor.MenuItem("Developer/Delete Player Prefabs")]
    35. public static void DeletePlayerSavaFiles(){
    36. SaveFile.DeleteSaveFile(PLAYER_FILE_NAME);
    37. }
    38. [SerializeField] class Player{
    39. public string playerName;
    40. public string playerMoney;
    41. public string playerLevel;
    42. public string playerScore;
    43. }
    44. }

    SaveSystem

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. public class SaveFile
    6. {
    7. //存储文件
    8. public static void SaveByJson(string fileName,object data){
    9. var json = JsonUtility.ToJson(data);
    10. var path = Path.Combine(Application.persistentDataPath,fileName);
    11. try{
    12. File.WriteAllText(path,json);
    13. Debug.Log("存储成功");
    14. }
    15. catch(System.Exception e){
    16. Debug.Log("存储失败");
    17. }
    18. }
    19. //加载文件
    20. public static T LoadFromJson<T>(string fileName){
    21. var path = Path.Combine(Application.persistentDataPath,fileName);
    22. try{
    23. var json = File.ReadAllText(path);
    24. var data = JsonUtility.FromJson<T>(json);
    25. Debug.Log("读取成功");
    26. return data;
    27. }
    28. catch(System.Exception e){
    29. Debug.Log("读取失败");
    30. return default;
    31. }
    32. }
    33. //删除文件
    34. public static void DeleteSaveFile(string fileName){
    35. var path = Path.Combine(Application.persistentDataPath,fileName);
    36. try{
    37. File.Delete(path);
    38. Debug.Log("删除成功");
    39. }
    40. catch(System.Exception e){
    41. Debug.Log("删除失败");
    42. }
    43. }
    44. }

  • 相关阅读:
    基于STM32设计的动态密码锁
    斗鱼主播查询易语言代码
    windows中MySQL主从配置【第一篇】
    kuberadm搭建k8s集群
    leetcode 547.省份数量 并查集
    深度强化学习(Deep Reinforcement Learning, DRL)阶段性学习汇总(二)
    python中“_“用法
    Graph WaveNet:用于时空图建模的图神经网络结构
    陈奂仁音乐狂欢节:怪异的多色吉他 NFT 系列来袭!
    hive之full outer join(全连接)使用方法
  • 原文地址:https://blog.csdn.net/weixin_51327051/article/details/125426262