gpt4 book ai didi

android - 如何使用unity3d在移动设备上正确加载XML文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:14 25 4
gpt4 key购买 nike

我在项目的资源文件夹中(没有任何子文件夹)有一些 xml 格式的级别定义(扩展名为 .txt)

为了提高可扩展性,我有一个纯文本文件命名所有这些关卡定义 XML

我用

读了那个文件
TextAsset WorldList = (TextAsset)Resources.Load("WorldList");

然后我加载所需的世界:

TextAsset TA = (TextAsset)Resources.Load(/*"LevelDefs/" +*/ Worlds[worldToload]);
xps.parseXml(TA.text);
loadLevel(levelToLoad);

(你看我已经把这些资源移出子文件夹以减少它们不加载的机会)worldToload这里是那个世界的索引号

程序在 Windows 上运行良好,但在我的 Android 测试设备上没有加载任何内容。我似乎在设备上调试时遇到问题,所以我只是猜测加载阶段出了点问题。

有什么建议吗?

最佳答案

来自 Unity 文档:

Most assets in Unity are combined into the project when it is built. However, it is sometimes useful to place files into the normal filesystem on the target machine to make them accessible via a pathname. An example of this is the deployment of a movie file on iOS devices; the original movie file must be available from a location in the filesystem to be played by the PlayMovie function.

Any files placed in a folder called StreamingAssets in a Unity project will be copied verbatim to a particular folder on the target machine. You can retrieve the folder using the Application.streamingAssetsPath property. It’s always best to use Application.streamingAssetsPath to get the location of the StreamingAssets folder, it will always point to the correct location on the platform where the application is running.

所以下面的代码 fragment 应该可以工作:

// Put your file to "YOUR_UNITY_PROJ/Assets/StreamingAssets"
// example: "YOUR_UNITY_PROJ/Assets/StreamingAssets/Your.xml"

if (Application.platform == RuntimePlatform.Android)
{
// Android
string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");

// Android only use WWW to read file
WWW reader = new WWW(oriPath);
while ( ! reader.isDone) {}

realPath = Application.persistentDataPath + "/Your"; // no extension ".xml"
var contents = System.IO.File.ReadAll(realPath);
}
else // e.g. iOS
{
dbPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Your.xml");
}

关于android - 如何使用unity3d在移动设备上正确加载XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com