Version: 2022.2
Deep linking on iOS
Social API

准备应用程序进行应用内购 (IAP)

本章介绍如何将游戏与 Apple 的 StoreKit API 进行集成。此处假设已经通过原生代码插件与 StoreKit 进行集成。

Apple StoreKit 文档定义了可通过_应用内购 (In-App Purchase)_ 流程销售的四种商品:

  • 内容 (Content)
  • 功能 (Functionality)
  • 服务 (Services)
  • 订阅 (Subscriptions)

本章仅介绍第一种情况,主要侧重于可下载内容概念。建议使用 AssetBundles 在 Unity 中实现可下载内容,这里将介绍 AssetBundles 的创建和运行时使用。

导出资源以便在 iOS 上使用

为主应用程序和将使用的可下载 AssetBundles 维护单独的项目有时很有用。但是,应该注意,AssetBundle 中的对象引用的所有_脚本_必须存在于主游戏可执行文件中。用于创建 AssetBundle 的项目必须将 iOS 作为选定的构建目标,因为 AssetBundle 文件的内容在 iOS 和其他平台之间不兼容。

AssetBundles 是使用 Editor 脚本创建的 - 下面给出了一个简单的示例:

using UnityEngine;
using UnityEditor;

public class ExportBundle : MonoBehaviour {
    [MenuItem ("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void DoExport() {
        string str = EditorUtility.SaveFilePanel("Save Bundle...", Application.dataPath, Selection.activeObject.name, "assetbundle");
        if (str.Length != 0) {
            BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, str, BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
        }
    }
}

应将此代码保存在名为 ExportBundle 的文件中,并将文件放在名为 Editor 的文件夹中(如果项目中不存在该文件夹,则可以创建该文件夹)。此脚本将在 Editor 的 Assets 菜单上添加一个名为 Build AssetBundle From Selection - Track dependencies 的菜单项。

希望包含在捆绑包中的内容应以预制件的形式准备。在 Project 视图中选择一个预制件,然后选择 _Assets > Build AssetBundle From Selection - Track dependencies_(即 ExportBundle 脚本添加的菜单项)。此命令将打开一个保存对话框,可在其中选择 AssetBundle 文件的名称和位置。

在 iOS 上下载资源

注意:Apple 可能会更改允许写入数据的文件夹位置。请务必查看最新的 Apple 文档,以确保您的应用程序符合要求。以下建议截至 2018 年初有效。

可以使用 WWW 类下载 AssetBundles,一旦传输完成,就可以访问其中的资源。推荐的 AssetBundle 下载方法是使用 LoadFromCacheOrDownload,如以下示例所示:

IEnumerator GetAssetBundle() {
    WWW download;
    string url = "https://somehost/somepath/someassetbundle.assetbundle";

    while (!Caching.ready)
        yield return null;

    download = WWW.LoadFromCacheOrDownload(url, 0);

    yield return download;

    AssetBundle assetBundle = download.assetBundle;
    if (assetBundle != null) {
        // 或者也可以按名称加载资源 (assetBundle.Load("my asset name"))
        Object go = assetBundle.mainAsset;
            
        if (go != null)
            Instantiate(go);
        else
            Debug.Log("Couldn't load resource");    
    } else {
        Debug.Log("Couldn't load resource");    
    }
}

下载的 Asset Bundle 文件存储在 iOS 应用程序沙盒的 Library 文件夹中,并在其上设置了 No Backup 标志。这意味着操作系统不会意外删除这些文件,因此这些文件不会备份到 iCloud。

如果需要确切选择 AssetBundle 文件的存储位置,可以使用标准的 WWW 下载(即仅使用构造函数而不是 LoadFromCacheOrDownload),然后使用 .NET 文件 API 将下载的数据保存到磁盘上。可将所需文件保存到 Application.temporaryCachePath 文件夹(存储在 Library/Caches 中,此位置由操作系统定期“清除”)或 Application.persistentDataPath 文件夹(存储在 Documents 中,操作系统不会清除此位置)。应使用 iOS.Device.SetNoBackupFlag 在这些文件上设置 No Backup 标志以防止它们备份到 iCloud。

注意:如果未设置 No Backup 标志,则在将应用程序提交到 App Store 时可能会遭到拒绝。

可以通过使用 file:///pathtoyourapplication/Library/savedassetbundle.assetbundle 形式的 URL 创建 WWW 对象来访问已保存的文件:

string cachedAssetBundle = Application.temporaryCachePath + "/savedassetbundle.assetbundle"; 
System.IO.FileStream cache = new System.IO.FileStream(cachedAssetBundle, System.IO.FileMode.Create);
cache.Write(download.bytes, 0, download.bytes.Length);
cache.Close();

iOS.Device.SetNoBackupFlag(cachedAssetBundle);

Debug.Log("Cache saved: " + cachedAssetBundle);

注意:如果启用_文件夹共享_,可以测试对 Documents 文件夹中存储的文件的读取(在 Info.plist 中将 UIFileSharingEnabled 设置为 true 允许从 iTunes 访问 _Documents_文件夹)。请注意,Documents 文件夹的内容会缓存到 iCloud,因此不应使用此位置将 AssetBundles 存储到要发布的最终版本中。有关更多详细信息,请参阅 Apple iOS 文档中的文件系统基础知识 (File System Basics)


  • 2018–06–14 页面已修订
Deep linking on iOS
Social API