Getting Started with iOS Development Part9:Preparing your application for "In App Purchases"

Preparing your application for "In App Purchases"

This chapter does not aim to cover how to integrate your game with Apple's "StoreKit" API. It is assumed that you already have integration with "StoreKit" via anative code plugin.

Apple's "StoreKit" documentation defines four kinds of Products that could be sold via the "In App Purchase" process:

  • Content
  • Functionality
  • Services
  • Subscriptions

This chapter covers the first case only and focuses mainly on the downloadable content concept. AssetBundles are ideal candidates for use as downloadable content, and two scenarios will be covered:

  • How to export asset bundles for use on iOS
  • How download and cache them on iOS
 

Exporting your assets for use on iOS

Having separate projects for downloadable content can be a good idea, allowing better separation between content that comes with your main application and content that is downloaded later.

Please note: Any game scripts included in downloadable content must also be present in the main executable.

 
  1. Create an Editor folder inside the Project View.
  2. Create an ExportBundle.js script there and place the following code inside:
    @MenuItem ("Assets/Build AssetBundle From Selection - Track dependencies")
    static function ExportBundle(){
    
            var str : String = 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);
            }
    }
    
  3. Design your objects that need to be downloadable as prefabs
  4. Select a prefab that needs to be exported and mouse right click
    Getting <wbr>Started <wbr>with <wbr>iOS <wbr>Development <wbr>Part9:Preparing <wbr>your <wbr>application <wbr>for <wbr>"In <wbr>App <wbr>Purchases" 
    If the first two steps were done properly, then the Build AssetBundle From Selection - Track dependencies context menu item should be visible.
  5. Select it if you want to include everything that this asset uses.
  6. A save dialog will be shown, enter the desired asset bundle file name. An .assetbundle extension will be added automatically. The Unity iOS runtime accepts only asset bundles built with the same version of the Unity editor as the final application. Read BuildPipeline.BuildAssetBundle for details.

Downloading your assets on iOS

  1. Asset bundles can be downloaded and loaded by using the WWW class and instantiating a main asset. Code sample:
         var download : WWW;
    
            var url = "http://somehost/somepath/someassetbundle.assetbundle";
    
            download = new WWW (url);
    
            yield download;
    
            assetBundle = download.assetBundle;
    
            if (assetBundle != null) {
                    // Alternatively you can also load an asset by name (assetBundle.Load("my asset name"))
                    var go : Object = assetBundle.mainAsset;
    
                    if (go != null)
                            instanced = Instantiate(go);
                    else
                            Debug.Log("Couldnt load resource");     
            } else {
                    Debug.Log("Couldnt load resource");     
            }
    
  2. You can save required files to a Documents folder next to your game's Data folder.
            public static string GetiPhoneDocumentsPath () { 
                    // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents 
                    // Application.dataPath returns              
                    // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data 
                    // Strip "/Data" from path 
                    string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5); 
                    // Strip application name 
                    path = path.Substring(0, path.LastIndexOf('/'));  
                    return path + "/Documents"; 
            }
    
     
  3. Cache a downloaded asset bundle using the .NET file API and for reuse it in the future by loading it via WWW class andfile:///pathtoyourapplication/Documents/savedassetbundle.assetbundle. Sample code for caching:
     // Code designed for caching on iPhone, cachedAssetBundle path must be different when running in Editor
            // See code snippet above for getting the path to your Documents folder
            private var cachedAssetBundle : "path to your Documents folder" + "/savedassetbundle.assetbundle"; 
            var cache = new System.IO.FileStream(cachedAssetBundle, System.IO.FileMode.Create);
            cache.Write(download.bytes, 0, download.bytes.Length);
            cache.Close();
            Debug.Log("Cache saved: " + cachedAssetBundle);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值