应用无法从Android读取xml文件,但可以从PC读取

时间:2019-09-23 13:00:15

标签: c# android xml unity3d

所以我有此代码,我的问题未存储在任何文件夹中。它就在资产中。问题是当我构建应用程序并从任何Android手机打开它时都不会显示问题。对不起,我的英语。

using System.IO;
using System.Xml.Serialization;
using UnityEngine;

public class GameUtility {

    public const float      ResolutionDelayTime     = 1;
    public const string     SavePrefKey             = "Game_Highscore_Value";

    public const string     FileName                = "Q";
//file name is upthere and the dir down

    public static string    FileDir                 
    {
        get
        {
            return Application.dataPath + "/";
        }
    }
}
//this gives me a new question

[System.Serializable()]
public class Data
{
    public Question[] Questions = new Question[0];

    public Data () { }

    public static void Write(Data data, string path)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Data));
        using (Stream stream = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(stream, data);
        }
    }
    public static Data Fetch(string filePath)
    {
        return Fetch(out bool result, filePath);
    }
    public static Data Fetch(out bool result, string filePath)
    {
        if (!File.Exists(filePath)) { result = false; return new Data(); }

        XmlSerializer deserializer = new XmlSerializer(typeof(Data));
        using (Stream stream = new FileStream(filePath, FileMode.Open))
        {
            var data = (Data)deserializer.Deserialize(stream);

            result = true;
            return data;
        }
    }
}

我只想知道为什么我的应用程序无法显示来自Android的问题?在PC上可以正常运行。

1 个答案:

答案 0 :(得分:0)

Android文件在APK中被压缩,因此您需要以其他方式打开它们。您可以尝试以下代码。

    string xml;
#if UNITY_EDITOR || !UNITY_ANDROID
    xml = File.ReadAllText(StreamingXmlPath);
#else
    // compressed in android (not readable with File).
    WWW reader = new WWW (StreamingXmlPath);
    while (!reader.isDone) {}
    xml = reader.text;
#endif