Last active
April 15, 2018 15:46
-
-
Save yasuyuki-kamata/4e55c95143acbdc2c98701b6efb17d64 to your computer and use it in GitHub Desktop.
UNIBOOK9 ゲーム内で課金アイテムのサジェストをしてみる IAP側
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Purchasing; | |
[RequireComponent(typeof(AdsManager))] | |
public class UnityIAP : MonoBehaviour, IStoreListener | |
{ | |
private IStoreController _controller; | |
private AdsManager _ads; | |
private const string ProductCoins = "100.gold.coins"; | |
private const string ProductHat = "top_hat"; | |
private const string ProductElite = "elite_status"; | |
private const string ProductBundle = "gem_super_box"; | |
public int GemsCount { get; set; } | |
private void Awake() | |
{ | |
_ads = GetComponent<AdsManager>(); | |
} | |
private void Start() | |
{ | |
Debug.Log("UnityIAP.Init()"); | |
StandardPurchasingModule module | |
= StandardPurchasingModule.Instance(); | |
// ダミーのストアを利用する。 | |
// 実際にAppStoreなどを使う場合には次の行を削除する | |
module.useFakeStoreAlways = true; | |
// IAP Catalogからカタログデータをロードする | |
ProductCatalog catalog | |
= ProductCatalog.LoadDefaultCatalog(); | |
ConfigurationBuilder builder | |
= ConfigurationBuilder.Instance(module); | |
IAPConfigurationHelper | |
.PopulateConfigurationBuilder(ref builder, catalog); | |
UnityPurchasing.Initialize(this, builder); | |
} | |
public void OnInitialized ( | |
IStoreController controller, | |
IExtensionProvider extensions) | |
{ | |
Debug.Log("UnityIAP.OnInitialized(...)"); | |
_controller = controller; | |
// IAPの初期化が終わった後に、Adsを初期化する必要がある。 | |
_ads.Init (); | |
} | |
public void OnInitializeFailed (InitializationFailureReason error) | |
{ | |
Debug.Log("UnityIAP.OnInitializeFailed(" + error + ")"); | |
// IAPの初期化が失敗したとしても、Adsは使えるようにしておく | |
// (IAP PromoのPlacementは使えないけど) | |
_ads.Init (); | |
} | |
public void Buy(string productId) { | |
Debug.Log("UnityIAP.BuyClicked(" + productId + ")"); | |
_controller.InitiatePurchase(productId); | |
} | |
public void OnPurchaseFailed(Product item, PurchaseFailureReason r) | |
{ | |
Debug.Log("UnityIAP.OnPurchaseFailed(" + item + ", " + r + ")"); | |
} | |
public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) | |
{ | |
string purchasedItem = e.purchasedProduct.definition.id; | |
switch (purchasedItem) { | |
case ProductCoins: | |
GemsCount += 100; | |
Debug.Log ("IAPLog: Congratualtions you are richer!"); | |
break; | |
case ProductHat: | |
Debug.Log ("IAPLog: Hat owned"); | |
break; | |
case ProductElite: | |
Debug.Log ("IAPLog: Elite member"); | |
break; | |
case ProductBundle: | |
GemsCount += 5000; | |
break; | |
} | |
return PurchaseProcessingResult.Complete; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment