FC2ブログ
    01 «1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.» 03

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    デジゲー博Specialも行ったんだけど、Unity5.3から導入されてるIAPも試してみたんだけどどうだろうか? 

    はい、こんにちは坂内っす。

    1月30日に幕張メッセで行っていたニコニコ闘会議行ってきました!
    ニコニコみないので、本編はあんまり興味はなく、デジゲー博SPECIALが目当てです。
    [デジゲー博SPECIAL in 闘会議2016]
    http://digigame-expo.org/sp2016

    こちら、面白かったです。
    規模は去年にあったデジゲー博の半分くらい?もっと少なかったかな?くらいでした。
    Room6さんのブースでRoom6グッズ詰め合わせを買わせて頂きました!
    他にもラビアンさんのブース、ProtoAccelさんのブース、tomeappさんのブースなど、いくつかまわりましたよっと!

    はい、では話は変わって、UnityのIAP試しました!
    Unity5.3から導入された、アプリ内課金のシステムですね。
    今までappCさんのアイテムSTORE使ったり、AssetStoreでアプリ内課金のAssetを買っていた方には朗報ですよね!
    ただ…色々調べても英語のサイトばっかりで中々触る機会がありませんでした。

    ポケガ2も佳境に入ってきて、そろそろ課金周りをどうにかしないとなと、思っていたのでいじってみました。
    結果としては、AndroidでのConsumableのアプリ内課金は無事成功!(なのか?

    今回試したのはAndroidのConsumable(消費型)だけなので、リストア周りはコードに書きっぱなしですが、使っていません。あしからず。

    IOSでもこのままいくかもしれませんが、それは今週中に試したいと思います。

    IAPを使う上での手順を簡単に書くと以下の感じです。
    ①GooglePlayでアプリ内アイテムを作成し、有効化しておく。
    ②UnityにてIAPを有効にする。
    ③実際のコードを書く。
    ④一度GooglePlayの方にアルファかベータテストバージョンとしてあげる。

    では、①から行ってみましょ。

    【①GooglePlayでアプリ内アイテムを作成し、有効化しておく。】
    まぁ、これは皆さん分かっている通りGooglePlayにてアプリ内課金を作成します。


    と、ここで以前登録した時は、管理対象外のアイテム=Consumable(消費型)という項目があったと思ったんですが、気のせいかな・・

    と思ったら、去年(2015年)の1月に前のバージョンとなる「In-app Billing Version2」が終了していて、現在は「In-app Billing Version3」になっていました。
    Version3で消費型も非消費型も管理対象のアイテムになったようですね。
    こちらに書いていました。
    http://qiita.com/Night___/items/1ff72d0a926e0ebdf87c

    ってことで、まずは管理型のアイテムを作ります。


    【②UnityにてIAPを有効にする。】
    では次にUnity側でIAPを有効にします。

    有効にする方法は、次の通り。
    まずは、サービスウィンドウを開きます。


    ここに新しいボタンあるので、コレ押してね。
    Window⇒Serviceって押してもいいよ!

    そうするとこんな画面が出ます。


    ProjectIDを選択します。

    これで、「Create!!!」



    そしたらこんな画面になるので、In-App PurchasingのOFFを押しましょう!

    はい、こんな画面になります。


    おとなしく[Enable]を押します。



    この画面では、英語読んで下さい。
    上の選択肢が”13歳未満対象のゲームです”
    下の選択肢が”ちゃうわ!”
    らしいですよ。

    で、OK-!ってやると


    こんな画面になるので必要なデータをImportします。

    これで、②は終わり。

    【③実際のコードを書く】
    はい、ではコードかいてきまひょ。

    まずは、公式ページにあるコードをもらってきます。
    https://unity3d.com/jp/learn/tutorials/topics/analytics/integrating-unity-iap-your-game-beta
    ここのページの真ん中チョイ下あたりにあるコードですね。
    ガッツリ持ってきてください。
    もちろんクラス名は自分のクラス名に変えてね!

    基本はこれでおk!

    それでは、自分のアプリのアプリ内課金アイテムを指定してあげましょう。
    触ってみたのはこのあたりです。



    // General handle for the consumable product.
    private static string kProductIDConsumable = "consumable";
    // General handle for the non-consumable product.
    private static string kProductIDNonConsumable = "nonconsumable";
    // General handle for the subscription product.
    private static string kProductIDSubscription = "subscription";

    // Apple App Store identifier for the consumable product.
    private static string kProductNameAppleConsumable = "com.unity3d.test.services.purchasing.consumable";
    // Apple App Store identifier for the non-consumable product.
    private static string kProductNameAppleNonConsumable = "com.unity3d.test.services.purchasing.nonconsumable";
    // Apple App Store identifier for the subscription product.
    private static string kProductNameAppleSubscription = "com.unity3d.test.services.purchasing.subscription";

    // Google Play Store identifier for the consumable product.
    private static string kProductNameGooglePlayConsumable = "com.unity3d.test.services.purchasing.consumable";
    // Google Play Store identifier for the non-consumable product.
    private static string kProductNameGooglePlayNonConsumable = "com.unity3d.test.services.purchasing.nonconsumable";
    // Google Play Store identifier for the subscription product.
    private static string kProductNameGooglePlaySubscription = "com.unity3d.test.services.purchasing.subscription";



    kProductIDConsumable、kProductIDNonConsumable、kProductIDSubscription
    はアイテムのIDです。

    kProductNameAppleConsumable、kProductNameAppleNonConsumablekProductNameAppleSubscription、
    kProductNameGooglePlayConsumable、kProductNameGooglePlayNonConsumable、kProductNameGooglePlaySubscription
    は、GooglePlayやiTuneConnectで作成した、アプリ内課金アイテムのIDです。

    で、先ほど4つGooglePlayDeveloperConsoleでアイテムを作成しました。
    ●100円のアイテム、IDは「com.test.item01」
    ●500円のアイテム、IDは「com.test.item02」
    ●1000円のアイテム、IDは「com.test.item03」
    ●3000円のアイテム、IDは「com.test.item04」
    としましょう。

    なので、上記のコードの場所をこんな感じで変えてみました。



    // アイテム01のID.
    private static string consumable_item_01 = "item01";
    // アイテム02のID.
    private static string consumable_item_02 = "item02";
    // アイテム03のID.
    private static string consumable_item_03 = "item03";
    // Apple App Store アイテム01.
    private static string product_apple_consumable_item_01 = "apple.com.test.item01";
    // Apple App Store アイテム02.
    private static string product_apple_consumable_item_02 = "apple.com.test.item02";
    // Apple App Store アイテム03.
    private static string product_apple_consumable_item_03 = "apple.com.test.item03";

    // Google Play Store アイテム01.
    private static string product_google_consumable_item_01 = "com.test.item01";
    // Google Play Store アイテム02.
    private static string product_google_consumable_item_02 = "com.test.item02";
    // Google Play Store アイテム03.
    private static string product_google_consumable_item_03 = "com.test.item03";


    なんか分かりにくいけど、分かりやすい名前にしてあげてね!

    それに伴い、以下箇所も変更。
    ●InitializePurchasing()メソッドの中です。


    // Create a builder, first passing in a suite of Unity provided stores.
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

    // Add a product to sell / restore by way of its identifier, associating the general identifier with its store-specific identifiers.
    builder.AddProduct(kProductIDConsumable, ProductType.Consumable,
    new IDs(){{ kProductNameAppleConsumable,AppleAppStore.Name },
    { kProductNameGooglePlayConsumable, GooglePlay.Name },});
    builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable,
    new IDs(){{ kProductNameAppleNonConsumable,AppleAppStore.Name },
    { kProductNameGooglePlayNonConsumable, GooglePlay.Name },});
    builder.AddProduct(kProductIDSubscription, ProductType.Subscription,
    new IDs(){{ kProductNameAppleSubscription,AppleAppStore.Name },
    { kProductNameGooglePlaySubscription, GooglePlay.Name },});
    UnityPurchasing.Initialize(this, builder);


    ここを、以下にします。


    // Create a builder, first passing in a suite of Unity provided stores.
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

    builder.Configure().SetPublicKey(publicKey);

    // Add a product to sell / restore by way of its identifier, associating the general identifier with its store-specific identifiers.
    builder.AddProduct(consumable_item_01, ProductType.Consumable,
    new IDs() { { product_google_consumable_item_01,GooglePlay.Name }, });
    builder.AddProduct(consumable_item_02, ProductType.Consumable,
    new IDs() { { product_google_consumable_item_02,GooglePlay.Name }, });
    builder.AddProduct(consumable_item_03, ProductType.Consumable,
    new IDs() { { product_google_consumable_item_03,GooglePlay.Name }, });
    builder.AddProduct(consumable_item_04, ProductType.Consumable,
    new IDs() { { product_google_consumable_item_04,GooglePlay.Name }, });
    UnityPurchasing.Initialize(this, builder);


    ってやりました。
    公式のコードにはないんですが、1行追加の必要があります。
    builder.Configure().SetPublicKey(publicKey);
    パブリックキーはGooglePlayのDeveloperConsoleで探してください。

    あと、今回はまだAppleの方考慮していないんですが、Appleの方もbuilderに追加する場合は、


    builder.AddProduct(consumable_item_01, ProductType.Consumable,
    new IDs() {{ product_apple_consumable_item_01,AppleAppStore.Name },
    { product_google_consumable_item_01, GooglePlay.Name }, });


    とか、Apple側とGoogle側で同じアイテムIDの場合は


    builder.AddProduct(consumable_item_01, ProductType.Consumable,
    new IDs() {{ product_apple_consumable_item_01,AppleAppStore.Name,GooglePlay.Name }, });


    ってできるみたいですよ。

    更に、これに伴いProcessPurchaseメソッドの中も適当に変えてください。
    こんな感じですかね。


    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) {
    if(String.Equals(args.purchasedProduct.definition.id, consumable_item_01, StringComparison.Ordinal)) {
    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));//If the consumable item has been successfully purchased, add 100 coins to the player's in-game score.
    // ここでアイテム1をあげる処理
    } else if(String.Equals(args.purchasedProduct.definition.id, consumable_item_02, StringComparison.Ordinal)) {
    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    // ここでアイテム2をあげる処理
    } else if(String.Equals(args.purchasedProduct.definition.id, consumable_item_03, StringComparison.Ordinal)) {
    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    // ここでアイテム3をあげる処理
    } else if(String.Equals(args.purchasedProduct.definition.id, consumable_item_04, StringComparison.Ordinal)) {
    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    // ここでアイテム4をあげる処理
    } else {
    Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    }
    return PurchaseProcessingResult.Complete;
    }




    はい、大体コードはこんな感じ?

    あとは、自分の画面で100円購入ボタン押されたら、上記コードの100円のIDならば「consumable_item_01」をBuyProductIDメソッドの引数に渡してあげるって感じでできました。

    【④一度GooglePlayの方にアルファかベータテストバージョンとしてあげる】
    なんかそのままBuild&Runしたらエラー出てアイテムを取って来れなかったんで、一度Apkをアルファ版かベータ版でGooglePlayにあげ、テスト版としてGooglePlayから落としたら無事成功しました。
    なぜかそのあとは、直接Build&Runでもちゃんと課金ができたのですが・・・・
    一度テスト版としてやってみましょう!

    ということで、ちょっと長くなりましたが、こんな感じでUnity5.3 IAPが使えましたよー って話しでした。

    まだまだ触ったばかりなので、間違えているところとかあるかと思います。
    何か発見したら知らせて頂けると助かります!

    ということで、あでゅ~!!ノシ

    Category: 開発日記(Unity)

    tb 0 : cm 0