【问题标题】:Writing an XML file on an iOS device and have Unity read from it on the same device在 iOS 设备上编写 XML 文件并让 Unity 在同一设备上读取该文件
【发布时间】:2013-09-25 08:54:26
【问题描述】:

我想让一个应用程序(非 Unity 项目)在后台运行并将一些数据写入 XML 文件。这是我已经拥有的代码。但是,对于我的 Unity 应用程序,我需要知道文件的位置,以便我可以阅读它。

有谁知道 iOS 自动将它创建的 XML 文件保存在哪里?

【问题讨论】:

  • 使用简单的文件读取 Unity 函数无法做到这一点:stackoverflow.com/questions/9425706/…。这个答案stackoverflow.com/questions/12561471/… 很好地描述了如何在两个本机 iOS 应用程序之间共享数据,但我认为提议的解决方案都不适用于 Unity 应用程序。您可以考虑使用 iCloud、Parse、Amazon S3 或其他云存储。
  • 我知道 iOS 应用程序可以编写 XML 文件,我知道 Unity 可以读取 XML 文件。那么为什么我的想法行不通呢?除非后台应用程序在后台无法主动写入 XML?
  • 您没有阅读链接的答案吗? iOS 文件系统不允许访问其他应用程序。它被称为沙盒。应用程序只能在其沙箱内读取/写入文件,并且无法访问其他应用程序沙箱。基本上,文件系统沙箱是一个不受其他应用程序保护的目录。应用程序之间的数据共享仅允许通过系统机制,例如自定义 URL 方案、自定义文档类型、钥匙串组 - 不是通过文件系统机制。它与 XML 文件无关——它与每个文件有关。我说清楚了吗?

标签: ios xml unity3d


【解决方案1】:

这个问题有点老了,但我会在这里回答,以防有人需要指南。

现在在 iOS8.0+ 中使用新的App Group 功能很可能做到这一点。此功能允许 2 个或更多应用程序在一个共享目录中会面。这是设置它的非常简短的步骤:

  1. 进入您的 Apple Developer 代理帐户,在您管理应用 ID 和证书的同一页面中,创建新的应用组。假设 group.com.company.myapp
  2. 我们需要 2 个应用程序来共享数据,对吗?确保您准备好 2 个应用程序 ID,如果没有,请创建新的。在这里您可以启用 App Group 并创建 2 个应用 ID,例如 com.company.myapp.readercom.company.myapp.writer。 (可以是任何名称,您实际上不必像这样嵌套名称)您可以暂时取消选中应用组的应用权利,因为 XCode 将为我们完成所有配置。
  3. 在 XCode 上新建项目,每个项目一个,单视图应用程序是一个很好的项目模板尝试。
  4. 在项目设置/功能选项卡上打开“应用程序组”功能。不要忘记选择要使用的应用组。
  5. 您必须在此处编写一些 Objective-C 代码来获取该共享文件夹路径。 (请参阅NSFileManager containerURLForSecurityApplicationGroupIdentifier)UI 的 viewDidLoad 函数是尝试您的代码 sn-p 的好地方。
    • Writer 应用程序可能会将此路径与文件名连接并尝试将一些文件写入该目录,您可以使用任何具有此路径的文件 API。
    • 阅读器应用程序执行相同的操作,但要从中读取。
  6. 测试直到您可以让阅读器应用程序看到编写器应用程序的文件。 (我将把所有的工作留在这里作为练习,正如我所说的,这将是简短的)

特定于 OP 的场景:

  1. 好的,你想要 Unity3D 中的这个功能。看起来没有内置的统一 API 或任何资产商店,所以你必须编写自己的 iOS 原生插件。如果您不熟悉它,请阅读一些文档。统一端的 C# 存根可以是这样的:

    using UnityEngine;
    using System.Collections;
    using System.Runtime.InteropServices;
    
    public static class AppGroupPlugin
    {
    
        /* Interface to native implementation */
    
        #region Native Link
        #if UNITY_EDITOR
    
        private static string _GetSharedFolderPath( string groupIdentifier )
        {
            // Handle dummy folder in editor mode
            string path = System.IO.Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), groupIdentifier );
            if( !System.IO.Directory.Exists( path ) )
                System.IO.Directory.CreateDirectory( path );
    
            return path;
        }
    
        #elif UNITY_IOS
    
        [DllImport ("__Internal")]
        private static extern string _GetSharedFolderPath( string groupIdentifier );
    
        #endif
        #endregion
    
        public static string GetSharedFolderPath( string groupIdentifier )
        {
            return _GetSharedFolderPath( groupIdentifier );
        }
    }
    
  2. 只需让 Objective-C 向您返回共享路径即可。我测试过,获取到这个路径后,你可以在任何System.IO.File操作中使用这个路径来读写文件,就好像它们是普通文件夹一样。这可以只是位于 Plugins/iOS 文件夹中的一个 .m 文件。

    char* MakeNSStringCopy (NSString* ns)
    {
        if (ns == nil)
            return NULL;
    
        const char* string = [ns UTF8String];
    
        char* res = (char*)malloc(strlen(string) + 1);
        strcpy(res, string);
        return res;
    }
    
    NSString* _GetSharedFolderPathInternal( NSString* groupIdentifier )
    {
        NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupIdentifier];
        if( containerURL != nil )
        {
            //NSLog( @"Path to share content is %@", [containerURL path] );
        }
        else
        {
            NSLog( @"Fail to call NSFileManager sharing" );
        }
    
        return [containerURL path];
    }
    
    // Unity script extern function shall call this function, interop NSString back to C-string, 
    // which then scripting engine will convert it to C# string to your script side.
    const char* _GetSharedFolderPath( const char* groupIdentifier )
    {
        NSString* baseurl = _GetSharedFolderPathInternal( CreateNSString( groupIdentifier ) );
        return MakeNSStringCopy( baseurl );
    }
    
  3. 从 Unity 导出 XCode 项目后,不要忘记再次打开“App Group”功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多