在 iOS 设备上写入 XML 文件,并在同一设备上读取 Unity 文件



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

有谁知道iOS自动保存它创建的XML文件的位置?

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

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

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

具体到OP的场景:

  1. 好的,您希望在 Unity3D 中使用此功能。看起来没有内置的 Unity 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 操作上使用此路径来读取写文件,就好像它们是普通文件夹一样。这可能只是一个位于插件/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 项目后,不要忘记再次打开"应用程序组"功能。

最新更新