赞
踩
目录
3.读取StreamingAssets文件夹下的json文件
- {
- "level": {
- "floorCount": 2,
- "maxNum": 2,
- "left": 1,
- "right": 1,
- "time":[
- {
- "floor1":0.2
- },
- {
- "floor2":0.5
- }
- ]
- }
- }
- [Serializable]
- public class LevelEntity
- {
- public int _floorCount;
- public int _maxNum;
- public int _left;
- public int _right;
- public List<float> _time = new List<float>();
- }
- public IEnumerator LoadLevel(string fileName)
- {
- string filePath = "";
- string result = "";
- #if UNITY_EDITOR
- filePath = Application.dataPath + "/StreamingAssets/" + fileName + ".txt";
- #elif UNITY_IPHONE
- filePath = Application.dataPath +"/Raw/"+fileName+".txt";
- #elif UNITY_ANDROID
- //filePath = “jar:file://” + Application.dataPath + “!/assets//"+fileName+".txt";
- filePath = Application.streamingAssetsPath+fileName+".txt";
- #endif
- Debug.LogError("FilePath:" + filePath);
- if (filePath.Contains("://"))
- {
- UnityWebRequest www = UnityWebRequest.Get(filePath);
- yield return www.SendWebRequest();
- if (www.isDone == false)
- {
- Debug.LogError("read json error:" + fileName);
- }
- result = www.downloadHandler.text;
- }
- else
- {
- result = System.IO.File.ReadAllText(filePath);
- }
- Debug.Log(result);
-
- JObject jsonData = JObject.Parse(result);
- JToken jToken = jsonData["Data"];
- le = new LevelEntity
- {
- _floorCount = int.Parse(jToken["floorCount"].ToString()),
- _maxNum = int.Parse(jToken["maxNum"].ToString()),
- _random = int.Parse(jToken["random"].ToString()),
- _left = int.Parse(jToken["left"].ToString()),
- _right = int.Parse(jToken["right"].ToString())
- };
- }
上面报错的话,读取方式二:
- public IEnumerator LoadConfig(string fileName)
- {
- string filePath = Path.Combine(Application.streamingAssetsPath,fileName);
- string url = "";
-
- if (Application.platform == RuntimePlatform.Android)
- {
- // Android平台的URL前缀
- url = "jar:file://" + filePath;
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- // iOS平台的URL前缀
- url = "file://" + filePath;
- }
- else
- {
- // 其他平台的URL前缀
- url = "file://" + filePath;
- }
-
- using (UnityWebRequest www = UnityWebRequest.Get(url))
- {
- yield return www.SendWebRequest();
-
- if (www.result == UnityWebRequest.Result.Success)
- {
- // 读取文件成功
- string fileContents = www.downloadHandler.text;
- Debug.Log("文件内容: " + fileContents);
- //ToolClass.DebugInfo("字符串内容是:" + www.downloadHandler.text);
- switch (fileName)
- {
- case EnumTableName.ZiMuZu:
- comJson_ZiMuZu = JsonUtility.FromJson<Json_ZiMuZu>(www.downloadHandler.text);
- cntLoadedConfig++;
- break;
- }
- }
- else
- {
- // 读取文件失败
- Debug.LogError("读取文件失败: " + www.error);
- }
- }
- }
- //读取Resources下的json文件
- public void ReadJson(string fileName)
- {
- //获得Json字符串
- string json = "";
- TextAsset text = Resources.Load<TextAsset>("levels/" + fileName);
- json = text.text;
- if (string.IsNullOrEmpty(json))
- {
- json = "";
- Debug.LogError(fileName + "...json is null or empty!");
- }
- Debug.Log(json);
-
- JObject jsonData = JObject.Parse(json);
- JToken jToken = jsonData["level"];
- JArray ja = JArray.Parse(jsonData["level"]["time"].ToString());
-
- le = new LevelEntity();
- le._floorCount = int.Parse(jToken["floorCount"].ToString());
- le._maxNum = int.Parse(jToken["maxNum"].ToString());
- le._left = int.Parse(jToken["left"].ToString());
- le._right = int.Parse(jToken["right"].ToString());
- for (int i = 0; i < ja.Count; i++)
- {
- string str1 = ja[i].ToString().Replace("{", "").Replace("}", "");
- string[] str2 = str1.Split(':');
- le._time.Add(float.Parse(str2[1]));
- }
- }
- //存本地--单个
- string str = JsonUtility.ToJson(entity, true);
- string filePath = Application.streamingAssetsPath + "/sbHaveList.json";
- using (StreamWriter sw = new StreamWriter(filePath))
- {
- sw.WriteLine(str);
- sw.Close();
- sw.Dispose();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。