Unity关于读取本地文本数据文件

背景

开发过程中难免会遇到读取StreamingAssets文件下的文本数据经过踩坑发现两种方式,其中一种方式移动端是读不到数据的,查了下资料发现,移动端需要使用WWW加载到内存中,移动端才能获取到。
核心代码:

using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Test19 : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //PC和移动端都可读到数据
        if (Input.GetKeyDown(KeyCode.A))
        {
            string json = FileRead();
            var  data = JsonMapper.ToObject<JsonInfo>(json);
            Debug.LogError(data);
        }
        //PC可以读到数据,移动端收不到数据
        if (Input.GetKeyDown(KeyCode.B))
        {
            var jsonPath = Application.streamingAssetsPath + "/jsonTest.txt";
            var txt = File.ReadAllText(jsonPath);
            var data = JsonMapper.ToObject<JsonInfo>(txt);
            Debug.LogError(data);
        }
    }
    private string FileRead()
    {
        var jsonPath = Application.streamingAssetsPath + "/jsonTest.txt";
        WWW t_WWW = new WWW(jsonPath);     //格式必须是"ANSI",不能是"UTF-8"
        if (t_WWW.error != null)
        {
            Debug.LogError("error : " + jsonPath);
            return "";          //读取文件出错
        }
        Debug.Log("t_WWW.text=  " + t_WWW.text);
        return t_WWW.text;
    }
}
public class JsonInfo
{
    public List<Childs> parents;
}
public class Childs
{
    public string lastname;
    public List<Child> firstname;
}
public class Child
{
    public string name;
    public string code;
}

文本文件:

{
  "parents": [
    {
      "lastname": "北京",
      "firstname": [
        {
          "name": "北京",
          "code": "101010100"
        },
        {
          "name": "海淀",
          "code": "101010200"
        }
      ]
    },
    {
      "lastname": "吉林",
      "firstname": [
        {
          "name": "长春",
          "code": "101060101"
        },
        {
          "name": "延吉",
          "code": "101060301"
        }
      ]
    }
  ]
}

Mark下,避免入坑,另外注意下:文本数据以字典规则存储时,key,必须为字符串。。。。
核心代码

using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Test19 : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //PC和移动端都可读到数据
        if (Input.GetKeyDown(KeyCode.A))
        {
            string json = FileRead();
            var  data = JsonMapper.ToObject<Dictionary<string, List<Childs>>>(json);
            Debug.LogError(data);
        }
        //PC可以读到数据,移动端收不到数据
        if (Input.GetKeyDown(KeyCode.B))
        {
            var jsonPath = Application.streamingAssetsPath + "/jsonTest.txt";
            var txt = File.ReadAllText(jsonPath);
            var data = JsonMapper.ToObject<Dictionary<string, List<Childs>>>(txt);
            Debug.LogError(data);
        }
    }
    private string FileRead()
    {
        var jsonPath = Application.streamingAssetsPath + "/jsonTest.txt";
        WWW t_WWW = new WWW(jsonPath);     //格式必须是"ANSI",不能是"UTF-8"
        if (t_WWW.error != null)
        {
            Debug.LogError("error : " + jsonPath);
            return "";          //读取文件出错
        }
        Debug.Log("t_WWW.text=  " + t_WWW.text);
        return t_WWW.text;
    }
}
public class Childs
{
    public string lastname;
    public List<Child> firstname;
}
public class Child
{
    public string name;
    public string code;
}

文本:

{
  "parents1": [
    {
      "lastname": "北京",
      "firstname": [
        {
          "name": "北京",
          "code": "101010100"
        },
        {
          "name": "海淀",
          "code": "101010200"
        }
      ]
    },
    {
      "lastname": "吉林",
      "firstname": [
        {
          "name": "长春",
          "code": "101060101"
        },
        {
          "name": "延吉",
          "code": "101060301"
        }
      ]
    }
  ],
  "parents2": [
    {
      "lastname": "北京",
      "firstname": [
        {
          "name": "北京",
          "code": "101010100"
        },
        {
          "name": "海淀",
          "code": "101010200"
        }
      ]
    },
    {
      "lastname": "吉林",
      "firstname": [
        {
          "name": "长春",
          "code": "101060101"
        },
        {
          "name": "延吉",
          "code": "101060301"
        }
      ]
    }
  ]
}
posted @ 2020-12-11 16:58  低小调  阅读(1027)  评论(0编辑  收藏  举报