【Unity】安卓读取StreamingAssets路径下json文件

Application.streamingAssetsPath + “/file.json”
“jar:file://” + Application.dataPath + “!/assets/” + “file.json”
上面路径同样指向StreamingAsset文件夹

StreamingAssets文件夹是可读的,但是不可写入

Loading:加载静态类
IEnumerator LoadJson(string path, LoadJsonFinish finish):这种写法使用泛型加委托回调

public delegate void LoadJsonFinish(object data);

public static class Loading
{
	public static IEnumerator LoadJson<T>(string path, LoadJsonFinish finish)
	{
		WWW www = new WWW(path);
		yield return www;
		while (www.isDone == false)
		{
			yield return new WaitForEndOfFrame();
		}
		string skipBom = Encoding.UTF8.GetString(www.bytes, 3, www.bytes.Length - 3);
		finish(JsonMapper.ToObject<T>(skipBom));
	}
}
private void Awake()
{

#if UNITY_EDITOR
	string filepath = Application.streamingAssetsPath + "/MusicLibrary_WAV.json";
#elif UNITY_IPHONE
	string filepath = Application.dataPath + "/Raw";
#elif UNITY_ANDROID
	string filepath = "jar:file://" + Application.dataPath + "!/assets/" + "MusicLibrary_WAV.json";
#endif
	//new Task(Loading.LoadJson>(filepath, LoadJsonFinish));
	StartCoroutine(Loading.LoadJson<List<MusicDataItem>>(filepath, LoadJsonFinish));
}
	private void LoadJsonFinish(object obj)
	{
		musicData.MusicDataItems = obj as List<MusicDataItem>;
	}

你可能感兴趣的:(Unity)