• 【Unity数据交互】JsonUtility的“爱恨情仇“


    在这里插入图片描述


    👨‍💻个人主页@元宇宙-秩沅

    👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

    👨‍💻 本文由 秩沅 原创

    👨‍💻 收录于专栏Unity基础实战

    🅰️




    前言


    🎶(1持久数据文件夹


    在这里插入图片描述

    • 先获取文件夹的位置

    print(Application.persistentDataPath);

    • 1.存储字符串到指定路径文件中

    File.WriteAllText(Application.persistentDataPath + “/text.json”,“这是我创建的新json脚本” );

    • 2.在指定路径文件中读取字符串

    string str = File.ReadAllText(Application.persistentDataPath + “/Test.json”);


    🎶(2JsonUtlity进行序列化


    JsonUtility是Unity引擎中的一个工具类,用于将JSON字符串转换为Unity对象或将Unity对象转换为JSON字符串。它可以方便地在Unity游戏中进行JSON数据的序列化和反序列化,使开发者可以更轻松地处理JSON数据。JsonUtility是一个非常强大和实用的工具,可以被广泛应用于Unity游戏中的数据管理、网络通信、游戏存档等方面。

    • 现实作用:
      1.将对象序列化成Json格式
      2.将Json格式反序列化为对象

    在这里插入图片描述

    • 引用 using System.IO

    IO通常是指Input/Output(输入/输出)的缩写。在计算机中,IO是指数据在计算机内部和外部设备之间的传输和交换。例如,键盘和鼠标输入数据到计算机中,打印机、屏幕和音箱从计算机中输出数据。这种数据的输入和输出过程就是IO。


    🪶0. jsonUtilty的注意点(缺点)

    😶‍🌫️注意:

        1.float序列化时看起来会有一些误差
        2.被包裹的自定义类需要加上序列化特性[System.Serializable]
        3.想要序列化私有变量 需要加上特性[SerializeField]
        4.JsonUtility不支持字典
        5.JsonUtlity存储null对象不会是null 而是默认值的数据,
        比如空int 会变成0
        6.JsonUtility无法直接反序列化读取数据集合"[]",需要用一个对象包裹它
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

       7.文本编码格式需要时UTF-8 不然无法加载
    
    • 1

    在这里插入图片描述


    🪶1.将对象序列化成字符串(Json格式)

    • string jsonStr = JsonUtility.ToJson( dataGame );

             // DataGame 是类对象
      
      • 1
    • File.WriteAllText(Application.persistentDataPath + “/text1.json”, jsonStr);

           //将序列化后的字符串数据存在文件夹中
      
      • 1

    🪶2.将Json反序列化成对象

    • jsonStr = File.ReadAllText(Application.persistentDataPath + “/text1.json”);

         //读取文件中的字符串 
      
      • 1
    • DataGame dataGame = JsonUtility.FromJson(jsonStr);

        //使用Json字符串内容 转换成类对象
      
      • 1
    • 代码

     jsonStr  = File.ReadAllText(Application.persistentDataPath + "/text1.json");
     
      DataGame dataGame = JsonUtility.FromJson<DataGame >(jsonStr);
     
    
    • 1
    • 2
    • 3
    • 4
    🪶3.练习(将序列化和反序列化变成方法调用)

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    [System.Serializable]
    public class Item
    {
        public int id;
        public int num;
    
        public Item( int ID,int NUM)
        {
            id = ID;
            num = NUM;
        }
    }
    
    public class BossInfo
    {
        public string name;
        public int attack;
        public int defence;
        public float moveSpeed;
        public double roundSpeed;
        public Item weapon;
        public List<int> listInt;
        public List<Item> itemList;
        public Dictionary<int, Item> itemDic1;
        public Dictionary<string, Item> itemDic2;
        [SerializeField ]
        private int self = 2 ;
        [SerializeField]
        protected int pro = 3;   
    
        //序列化
        public void serilized(BossInfo obj)
        {
            if(obj!=null )
            {
                string onjInfo = JsonUtility.ToJson(obj);
                File.WriteAllText(Application.persistentDataPath + ("/BossInfo.json"), onjInfo);
            }
           
        }
    
        //反序列化
        public BossInfo  RevSerilized(string path)
        {
            if (path != null)
            {
                string objInfo = File.ReadAllText(Application.persistentDataPath +"/"+ path);
                return JsonUtility.FromJson<BossInfo>(objInfo);
            }
            else return null;
        } 
    }
    
    public class text : MonoBehaviour
    {
        private void Start()
        {
            BossInfo boss = new BossInfo();
            boss.name = "鸭嘴兽";
            boss.attack = 100;
            boss.defence = 20;
            boss.moveSpeed = 50;
            boss.roundSpeed = 30;
            boss.weapon = new Item(001, 10);
            boss.listInt = new List<int>() { 1, 2, 3, 4 };
            boss.itemList = new List<Item>() { new Item(002, 10), new Item(003, 10) };
            boss.itemDic1 = new Dictionary<int, Item>() { { 1, new Item(002, 10) }, { 2, new Item(003, 10) } };
            boss.itemDic2 = new Dictionary<string, Item>() { { "鸭子1", new Item(003, 10) }, { "鸭子2", new Item(004, 10) } };
            boss.serilized(boss); //JsonUyility 不支持字典
            print(Application.persistentDataPath);
             boss.RevSerilized("BossInfo.json");
        }
    }
    
    
    • 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

    🅰️


    【Unityc#专题篇】之c#进阶篇】

    【Unityc#专题篇】之c#核心篇】

    【Unityc#专题篇】之c#基础篇】

    【Unity-c#专题篇】之c#入门篇】

    【Unityc#专题篇】—进阶章题单实践练习

    【Unityc#专题篇】—基础章题单实践练习

    【Unityc#专题篇】—核心章题单实践练习


    你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


    在这里插入图片描述


  • 相关阅读:
    系统架构设计师-数据库系统(3)
    scanf跳过第一个输入读入第二个输入,咋做?
    每日一题 2511. 最多可以摧毁的敌人城堡数目
    两个有序表的合并(三种方法)
    【LeetCode-中等题】106. 从中序与后序遍历序列构造二叉树
    芯片数字后端设计入门书单推荐(可下载)
    如何管理销售团队?
    通过Python pypdf库轻松拆分大型PDF文件
    【Java面试】请描述一下Redis中AOF 重写的过程
    【libhv】udp客户端服务器简单例子
  • 原文地址:https://blog.csdn.net/m0_64128218/article/details/134074077