【问题标题】:Unity assets path Android. [duplicate]Unity 资产路径 Android. [复制]
【发布时间】:2017-08-07 21:14:12
【问题描述】:

我有一个保存最高分的文件。在 Unity 编辑器中的路径是:path = Application.dataPath + "/Score.json"; 而且我不知道如何在 Android 中获取此文件的路径。 错误已被删除

【问题讨论】:

  • 您保存以使用Application.persistentDataPath 而不是Application.dataPath。在尝试保存之前,您还必须检查文件或目录是否存在。无论如何,您将游戏数据保存为 json,我认为这是重复的。

标签: c# unity3d


【解决方案1】:

据我所知,Application.dataPath 指向 Android 上的 .apk 文件。我建议您在每个平台上使用不同的路径。这是我为了保存一些游戏内容而更喜欢保存的东西。

 public static string Path
        {
            get
            {
                switch (Application.platform)
                {
                    case RuntimePlatform.IPhonePlayer:
                        return Path.Combine(Application.persistentDataPath, "MyStuff");

                    case RuntimePlatform.Android:
                        return Path.Combine(Application.temporaryCachePath, "MyStuff");

                    default:
                        return Path.Combine(Directory.GetParent(Application.dataPath).FullName, "MyStuff");
                }
            }
        }

【讨论】:

    【解决方案2】:

    在 Android 上,Application.dataPath 将指向 APK,因此您应该避免使用它来保存数据,因为您将无法写入它。相反,您想使用Application.persistentDataPath。正如链接文档所述:

    在 iOS 和 Android 上发布时,persistentDataPath 将指向一个 设备上的公共目录。此位置的文件不会被删除 随着应用程序的每次更新。

    【讨论】:

    • #if UNITY_ANDROID path = Application.persistentDataPath + "/Score.json"; Debug.Log("Android device"); #endif 对吗?
    • @Karol 我觉得不错。
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2023-03-31
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多