【问题标题】:How to check the path on Android如何在Android上检查路径
【发布时间】:2015-03-07 13:25:25
【问题描述】:

我尝试访问由 unity 创建的 Android 应用程序上的一些 XML 文件。我将它们放在我的 StreamingAssets 文件夹中,并通过 Application.streamingAssetsPath 恢复它们。

这是我获取路径的代码:

 int index=0;
 index=m_RuleBaseFilePath.LastIndexOf("/");
 string path= m_RuleBaseFilePath.Substring(index,m_RuleBaseFilePath.Length-index);
 TextAsset textAsset = (TextAsset)Resources.Load(path, typeof(TextAsset));
 string realpath=Application.streamingAssetsPath+path;

当我尝试构建和运行时,此代码适用于编辑器和 Windows,但不适用于 Android。我问自己是否通过这种方法获得了良好的路径。 有什么调试方法可以检查吗?

感谢您的帮助。

【问题讨论】:

  • 你想访问什么?您是否将 XML 文件保存在 Layout 文件夹中?
  • 我尝试使用以下路径访问我的 XML 文件:string realpath=Application.streamingAssetsPath+path; Unity 将 Xml 文件保存在 assets 文件夹中:test.apk\assets
  • 检查这可能对你有帮助。 answers.unity3d.com/questions/516735/…

标签: c# android xml path unity3d


【解决方案1】:

为阅读创建一个Coroutine 并使用WWW。

void Start()
{
     WWW data = new WWW(Application.streamingAssetsPath + "/" + fileName);
     StartCoroutine(MyRead(data));
}

IEnumerator MyRead(WWW data) 
{
    yield return data;

    if(!string.IsNullOrEmpty(data.error))
       myData = data.text;
}

【讨论】:

  • 感谢您的回答,我已尝试使用统一编辑器。我试试这个: WWW data = new WWW("file:///"+Application.streamingAssetsPath +"/"+path);首先在编辑器中恢复文件,但我总是得到:您正在尝试从 www 流加载数据,下载时出现以下错误。无法打开文件 /C:/Users/sébastien/Documents/New Unity Project 1/Assets/StreamingAssets/ExampleRulesBase.xml
  • 你为什么放"file:///"+Application...+path);?这与我的建议不同。
  • 但是如果你没有那个,你会得到无法解析主机C:所以为了解决这个问题,我已经按照统一文档的指示:注意:''当使用文件协议时用于访问本地文件的 Windows 和 Windows 应用商店应用程序,您必须指定 file:///__(带有三个斜杠)。
  • 右窗口。在 Android 上尝试一下,看看会发生什么。我就是这样测试的。
  • 当我尝试为 android (file->build) 构建项目时,我有相同的消息,Application.streamingasssets 返回我猜的计算机的路径。
【解决方案2】:

我编辑帖子以提供有关我的问题的一些消息,我检查了文件是否在我的 apk 中。我的 Xml 文件暂时在资源和流资产中,所以我在我的 APK/assets 和 apk\assets\bin\Data 中找到它们。我改变了检索文件的方式(它在编辑器上工作,但我在 android 上得到空对象)

TextAsset read=(TextAsset)Resources.Load("ExampleRulesBase",typeof(TextAsset));
Debug.Log(read.text);
Stream s=GenerateStreamFromString (read.text);
XmlReader reader = XmlReader.Create(s); 

public Stream GenerateStreamFromString(string s)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }

我只需要一个 XMLreader 来继续,但我的日志返回空白。

【讨论】:

    【解决方案3】:

    机器人:

    Application.dataPath /data/app/xxx.xxx.xxx.apk

    Application.streamingAssetsPath jar:file:///data/app/xxx.xxx.xxx.apk/!/assets

    Application.persistentDataPath /data/data/xxx.xxx.xxx/files

    Application.temporaryCachePath /data/data/xxx.xxx.xxx/cache

    IOS:

    Application.dataPath Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data

    Application.streamingAssetsPath Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw

    Application.persistentDataPath Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents

    Application.temporaryCachePath Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

    来自platform.so的路径差异请使用Application.XXXX。

    对于您的情况,您可以从您将它们放入编辑器的路径中读取 xml。例如 /Users/chenjd/workspace/chenjd/kiwi/xxx.xml 但在 Android 上使用 Application.streamingAssetsPath。

    【讨论】:

      猜你喜欢
      • 2014-10-05
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2014-03-09
      • 2011-10-16
      • 2011-04-24
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多