美文网首页
unity Json创建、读取、修改、

unity Json创建、读取、修改、

作者: WOTTOW | 来源:发表于2020-04-22 17:41 被阅读0次

    使用Newtonsoft.Json

    using Newtonsoft.Json;
    using System.Collections;
    using System.IO;
    using System.Text;
    using UnityEditor;
    using UnityEngine;
    using UnityEngine.Networking;
    using Newtonsoft.Json.Linq;
    
    public class SetJson : MonoBehaviour
    {
        private string path = Application.streamingAssetsPath + "/save.json";
        private string content;
        private int[] nums = { 20, 50, 40, 100 };
        private TestStruct[] testStructs;
    
        //----------
        public StudentData data;
    
        private void Start()
        {
            CreateFile();
        }
    
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                StartCoroutine(ReadJson());
            }
            if (Input.GetKeyDown(KeyCode.M))
            {
                StartCoroutine(JsonToObject());
            }
            if (Input.GetKeyDown(KeyCode.N))
            {
                ChangeJson();
            }
        }
    
        private void CreateFile()
        {
            Grade grade = new Grade();
            grade.math = 80;
            grade.physics = 80;
            grade.english = 80;
    
            testStructs = new TestStruct[10];
            for (int i = 0; i < 10; i++)
            {
                testStructs[i].name = "Test_" + i;
                testStructs[i].content = "content_" + i;
            }
    
            StudentData studentData = new StudentData();
            studentData.id = 1001;
            studentData.name = "Test";
            studentData.sexType = SexType.Male;
            studentData.age = 20;
            studentData.bCylet = false;
            studentData.grade = grade;
            studentData.nums = nums;
            studentData.testStructs = testStructs;
    
            content = JsonConvert.SerializeObject(studentData);
            Save(content);
            //   Debug.Log("path:"+path);
        }
    
        public void Save(string content)
        {
            FileInfo file = new FileInfo(path);//判断文件是否存在
            if (!file.Exists)   //此方法只能在Windows平台
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                byte[] bytes = new UTF8Encoding().GetBytes(content);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
                Debug.Log("Create OK");
                AssetDatabase.Refresh();
            }
            else
            {
                Debug.Log("已存在json");
            }
        }
    
        private IEnumerator ReadJson()
        {
            UnityWebRequest unityWebRequest = UnityWebRequest.Get(path);
            yield return unityWebRequest.SendWebRequest();
            if (unityWebRequest.isNetworkError)
            {
                Debug.LogError("出错");
            }
            string content = unityWebRequest.downloadHandler.text;
            data = JsonConvert.DeserializeObject<StudentData>(content);
            Debug.LogError(data.testStructs[1].name);
        }
    
        private void ChangeJson()
        {
            if (File.Exists(path))
                File.Delete(path);
            if (data == null)
            {
                StartCoroutine(ReadJson());
            }
            data.testStructs[1].name = "000000";
            content = JsonConvert.SerializeObject(data);
            Save(content);
            Debug.Log("Change OK");
        }
    
        private IEnumerator JsonToObject()
        {
            UnityWebRequest unityWebRequest = UnityWebRequest.Get(path);
            yield return unityWebRequest.SendWebRequest();
            if (unityWebRequest.isNetworkError)
            {
                Debug.LogError("出错");
            }
            string content = unityWebRequest.downloadHandler.text;
            JObject jobject = (JObject)JsonConvert.DeserializeObject(content);
            Debug.LogError(jobject["id"]);
        }
    }
    

    JSON结构

    image.png
    image.png

    相关文章

      网友评论

          本文标题:unity Json创建、读取、修改、

          本文链接:https://www.haomeiwen.com/subject/rkswihtx.html