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"
        }
      ]
    }
  ]
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM