【发布时间】:2017-04-17 21:26:04
【问题描述】:
我有一个下载 json 并尝试在设备上保存副本的功能。它在 android 和编辑器上运行良好。不能把我的手指放在它上面。 Ipad 正在运行 IOS 10,如果添加任何内容,统一设置为 9 以上。
下面是统一代码加上xcode上的错误。
单发展
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Collections.Generic;
using LitJson;
public class loadJSONHome : MonoBehaviour {
public string url;
public string url2;
private string jsonString;
public static loadJSONHome instance;
public GameObject preloader;
void Awake(){
if(instance==null){
instance = this;
}
}
void Start (){
WWW www2 = new WWW(url);
StartCoroutine(WaitForRequest(www2));
// preloader.SetActive(true);
}
IEnumerator WaitForRequest(WWW www2){
yield return www2;
// check for errors
if (www2.error == null)
{
//SAVE JSON FROM ONLINE LOCALLY
jsonString = www2.text;
writeStringToFile(jsonString, "FoodnDrink_Cats.json");
writeListings();
} else {
Debug.Log("WWW2 Error: "+ www2.error);
}
}
void writeListings(){
WWW www3 = new WWW(url2);
StartCoroutine(WaitForRequest2(www3));
}
IEnumerator WaitForRequest2(WWW www3){
yield return www3;
// check for errors
if (www3.error == null)
{
//SAVE JSON FROM ONLINE LOCALLY
jsonString = www3.text;
writeStringToFile(jsonString, "listings_FoodnDrink.json");
} else {
Debug.Log("WWW3 Error: "+ www3.error);
}
//preloader.SetActive(false);
}
public void writeStringToFile( string str, string filename ){
#if !WEB_BUILD
string path = pathForDocumentsFile( filename );
FileStream file = new FileStream (path, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter( file );
sw.WriteLine( str );
sw.Close();
file.Close();
#endif
}
public string readStringFromFile( string filename){ //, int lineIndex )
#if !WEB_BUILD
string path = pathForDocumentsFile( filename );
if (File.Exists(path))
{
FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader( file );
string str = null;
str = sr.ReadToEnd ();
sr.Close();
file.Close();
return str;
}else{
return null;
}
#else
return null;
#endif
}
public string pathForDocumentsFile( string filename ){
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
path = path.Substring( 0, path.LastIndexOf( '/' ) );
return Path.Combine( Path.Combine( path, "Documents" ), filename );
}else if(Application.platform == RuntimePlatform.Android){
string path = Application.persistentDataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}else {
string path = Application.dataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}
}
}
XCODE
未启用位置服务更新。在查询最后一个位置之前检查 LocationService.status。
IsolatedStorageException:找不到路径“/var/containers/Bundle/Application/BC5FA0A8-9767-4689-8707-EBB7FA1886F5/Documents/FoodnDrink_Cats.json”的一部分。 在 System.IO.FileStream..ctor(System.String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、布尔匿名、FileOptions 选项)[0x00000] in :0 在 System.IO.FileStream..ctor(System.String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、布尔 isAsync、布尔匿名)[0x00000] in :0 在 System.IO.FileStream..ctor(System.String 路径,FileMode 模式,FileAccess 访问)[0x00000] in :0 在 loadJSONHome.writeStringToFile (System.String str, System.String 文件名) [0x00000] in :0 在 loadJSONHome+c__Iterator5.MoveNext () [0x00000] in :0
(文件名:目前在 il2cpp 行中不可用:-1)
帮助表示赞赏。
【问题讨论】:
-
我相信您的代码中有多个问题。定位服务园请按:stackoverflow.com/a/24063578/1035008
-
感谢您的帮助。如果问题调试代码只是我需要清理的几个未使用的变量,则很多。问题是您提供帮助的定位服务,主要问题是隔离的存储线。如果您可以通过查找路径功能帮助解决该问题,我们将不胜感激。
-
它没有用 :( 我将尝试更新 unity 版本和 xcode 以查看结果如何
标签: c# android ios xcode unity3d