加载在Unity中创建的文件下载的iOS应用

镀银

我正在尝试加载已下载并保存在iOS设备的persistentDataPath中的json文件。下载和保存效果很好。但是我认为加载文件有很大的问题。实际上,当我尝试在Xcode中编译项目时,会出现一些错误消息。

首先,这里是我的C#代码:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using UnityEngine.UI;

public class ReadJson : MonoBehaviour
{
    public Text City;
    public Text Temperature;
    public Image Weather;
    public Text WeatherUrl;

    [System.Serializable]
    public class CityInfo 
    {
        public string name;
    }

    [System.Serializable]
    public class CurrentCondition
    {
        public string date; 
        public string hour; 
        public int tmp; 
        public int wnd_spd; 
        public int wnd_gust; 
        public string wnd_dir; 
        public double pressure; 
        public int humidity; 
        public string condition; 
        public string icon; 
        public string icon_big;
    }

    [System.Serializable]
    public class RootObject
    {
        public CityInfo city_info;
        public CurrentCondition current_condition;
    }

    void Start () {

        WebClient client = new WebClient();
        File.Delete(Path.Combine (Application.persistentDataPath, "myjson.json"));
        client.DownloadFile ("http://www.myjsonurl", Path.Combine (Application.persistentDataPath, "myjson.json"));

        TextAsset asset = Resources.Load (Path.Combine (Application.dataPath + "/Documents", "myjson")) as TextAsset;

        RootObject m = JsonUtility.FromJson<RootObject> (asset.text);

        City.text = m.city_info.name;
        Temperature.text = (m.current_condition.tmp).ToString();

    }
}

现在是xcode控制台:

2016-10-21 17:01:20.766001 json[1404:516674] [DYMTLInitPlatform] platform initialization successful

2016-10-21 17:01:20.929950 json[1404:516508] -> registered mono modules 0xb95f70
2016-10-21 17:01:21.356590 json[1404:516508] You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.
-> applicationDidFinishLaunching()
2016-10-21 17:01:21.452967 json[1404:516508] Metal GPU Frame Capture Enabled
2016-10-21 17:01:21.453369 json[1404:516508] Metal API Validation Enabled
-> applicationDidBecomeActive()
Init: screen size 750x1334
Initializing Metal device caps
Initialize engine version: 5.3.4f1 (fdbb5133b820)
UnloadTime: 1.705875 ms
Salut/var/mobile/Containers/Data/Application/51A2490E-94EB-4904-9F2E-112AD5632A98/Documents

(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

NullReferenceException: A null value was found where an object instance was required.


(Filename: currently not available on il2cpp Line: -1)

Setting up 1 worker threads for Enlighten.
  Thread -> id: 19f17000 -> priority: 1 

如果有人有主意。在此先多谢。

程序员

您的代码存在以下问题:

TextAsset asset = Resources.Load(Path.Combine(Application.dataPath + "/Documents", "myjson")) as TextAsset;

我认为您不了解如何使用和何时使用Resources.Load如果要使用该Resources.Load功能,则必须从编辑器中创建一个特殊文件夹。此文件夹应位于<ProjectDirectory>/Assets/Resources只能从编辑器将内容放入/写入该文件夹。在iOS上构建后,您将无法执行此操作。只有在构建后才变为只读然后可以使用该Resources.Load功能读取此特殊文件夹

更换:

TextAsset asset = Resources.Load(Path.Combine(Application.dataPath + "/Documents", "myjson")) as TextAsset;

string text = File.ReadAllText(Path.Combine(Application.persistentDataPath, "myjson.json"));
RootObject m = JsonUtility.FromJson<RootObject>(text);
City.text = m.city_info.name;
Temperature.text = (m.current_condition.tmp).ToString();

应该解决你的问题。

另一个不应该使用的API是WebClient除非您有充分的理由使用它,否则不应该这样做。您应该使用WWWUnityWebRequest(较新的和推荐的)API。

这是没有WebClient以下代码的代码的外观

void Start()
{
    StartCoroutine(DownloadAndloadJson());
}

IEnumerator DownloadAndloadJson()
{
    string url = "http://www.myjsonurl";

    UnityWebRequest www = UnityWebRequest.Get(url);
    yield return www.Send();

    if (www.isError)
    {
        Debug.Log(www.error);
    }
    else
    {
        //Sucessfully downloaded the Json File
        Debug.Log("Json: " + www.downloadHandler.text);

        string jsonFile = www.downloadHandler.text;

        //Delete old json file. Don't know why but this was in your original code
        File.Delete(Path.Combine(Application.persistentDataPath, "myjson.json"));

        //Save the downloaded data as File
        File.WriteAllText(Path.Combine(Application.persistentDataPath, "myjson.json"), jsonFile);

        //Load the downloaded data 
        string text = File.ReadAllText(Path.Combine(Application.persistentDataPath, "myjson.json"));

        Debug.Log(text);
        RootObject m = JsonUtility.FromJson<RootObject>(text);
        City.text = m.city_info.name;
        Temperature.text = (m.current_condition.tmp).ToString();
    }
}

如果您有问题,请更新到Unity 5.4。UnityWebRequestAPI应该用于此,它是在5.3推出,但错误是固定在5.4。您需要using UnityEngine.Networking;使用UnityWebRequest

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在iOS应用中打开下载的文件

如何从 Unity Firebase SDK 中的 Json 文件加载应用配置

如何让我的应用程序创建/下载的文件显示在“文件”应用程序中?

在文件系统中创建一个随机文件夹并保存下载的文件iOS

应用下载的纹理并通过EncodeToPNG转换回PNG后,PNG文件在Unity中增长

播放iOS中从应用程序文档文件夹下载的Widevine Classic文件?

将文件加载到使用Webplayer嵌入的unity3d应用程序中

在Java Servlet中创建并下载CSV文件

在Django中创建下载链接文件

在应用程序中创建CSV或TXT文件并将其保存到“下载”文件夹-Android

在iOS中批量下载多个文件

在iOS中后台下载大量文件

在iOS中自动下载文件

在iOS应用中的documents文件夹内创建一个文件夹

如何读取iOS WidgetKit应用创建的文件?

如何使用iOS中的React Native保存下载的文件并在File应用程序中查看?

iOS应用中的WKWebView加载时间很长

在iOS应用中从磁盘加载大图像

下载文件时在Chrome中检测iframe的加载

从Laravel存储下载而不将整个文件加载到内存中

下载文件,不加载到浏览器中

从Android应用中多次下载的.txt文件中读取数据

保存到“下载”文件夹中的文件未显示在“下载”应用程序中

“在iOS上解压缩许多文件的最佳方法”或“我还可以如何将许多文件快速下载到我的iOS应用中”

没有为 Xamarin 中的 iOS 示例应用程序创建 assemblyinfo.cs 文件

首次启动时通过iOS应用中的转储文件创建数据库

iOS:这是在应用程序支持中创建目录以存储我的SQL文件的正确方法吗?

无法加载资产(创建的应用程序文件)

下载后加载资源(iOS)