In-App Purchasing是Unity提供的跨平台IAP服務
本篇將介紹如何使用In-App Purchasing設定到Google Play Developer Console的商品
本篇教學軟體環境如下:
OS: Windows 10
Unity Version: 2020.1.6f
In-App Purchasing Version: 2.1.1
開啟Unity的IAP服務
- 選擇 Window->General->Services
- 選擇要用的Organization, 然後就可以點選Create project ID
P.S.這裡開始需要登入你的Unity ID以及有可對外連線的網路環境
這邊是問你的app是不是專門提供給13歲以下兒童, 給兒童的內容需注意色腥羶的控制, 細節可以點選下方的"Learn more about COPPA compliance", 請斟酌自己的案子情況選擇
- 一切順利的話, 就會出現這個"Project was linked successfully"頁面
這邊也會提醒在使用 In-App Purchasing的同時也會啟用Unity Analytics.
導入/設定IAP Plugin
- 啟動Unity IAP服務後, 點選Prooject Setting->Services-> In-App Purchasing Settings裡的OFF, 他會幫你自動導入
或者你也可以自己到Pakcage Manager安裝
- 接著會出現Unity IAP Installer, 按"Next>>"然後"Import"
*如果沒有出現的話, 記得把Build Settings的Platform轉到Android
成功的話會出現"Thanks for installing Unity IAP!"字樣
點選下方的"1. Go to Google Play Developer Console"會幫你開啟網頁
- 登入Google Play Developer Console後, 點選要設定的運用程式, 如果沒有就創建一個(這邊就不贅敘如何於Google Play Developer Console建立運用程式, 此並非本篇重點)
- 到 營利->營利設定 複製Public Key
P.S.這邊使用新版Google Play Developer Console介面來講解, 因為舊版介面將於2020/11/2終止服務
- 接著我們要設定Public Key給專案, 回到剛剛的Options下方欄位輸入Public Key, 然後按Update, 成功的話就會出現"Greate! You will now able to track verified transactions taking place on your app,"
或者你也可以到Unity Services的後台設定, 點選Dashboard會幫你開啟頁面
然後到Settings->Analytics Settings->Google License Key輸入Public Key, 然後按Save
建立商品ID
IAP的商品有分消耗型(Consumable)、非消耗型(NonConsumable)、訂閱型(Subscription)
本篇教學以消耗型商品示範之
- 在Google Play Developer Console的應用程式頁面->商品->應用程式內商品, 點選"建立商品"
- 接著設定商品ID等資料然後按儲存, 本篇教學設定為:
- 商品ID: test
- 名稱: 測試商品
- 說明: 這是測試商品
- 預售價格: TWD 30.00
實作購買程式碼
- 在實作前, 我們先在場景加上按鈕與文字元件, 在按下按鈕的時候購買, 然後把結果顯示在文字上
- 建立C#程式檔"TestIAP.cs", 程式碼內容如下:
using UnityEngine; using UnityEngine.Purchasing; using UnityEngine.UI; public class TestIAP : MonoBehaviour, IStoreListener { [SerializeField] private Text resultText = null; private IStoreController m_StoreController; private IExtensionProvider m_StoreExtensionProvider; private static string productID = "test"; void Start() { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); //加入要在遊戲內用的商品ID與類型, 本例商品類型為消耗型(Consumable) builder.AddProduct(productID, ProductType.Consumable); //初始化UnityPurchasing UnityPurchasing.Initialize(this, builder); } //確認是否UnityPurchasing是否已初始化 private bool IsInitialized() { return m_StoreController != null && m_StoreExtensionProvider != null; } //IStoreListener.IsInitialized的實作 //初始化成功時會自動call此函式 public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { resultText.text = "初始化成功"; //將傳來的IStoreController跟IExtensionProvider記起來 m_StoreController = controller; m_StoreExtensionProvider = extensions; } //IStoreListener.OnInitializeFailed的實作 //初始化失敗時會自動call此函式 public void OnInitializeFailed(InitializationFailureReason error) { resultText.text = $"初始化失敗, 原因:{error}"; } //IStoreListener.ProcessPurchase的實作 //購買成功時會自動call此函式 public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { resultText.text = $"購買成功, Product: {args.purchasedProduct.definition.id}"; return PurchaseProcessingResult.Complete; } //IStoreListener.OnPurchaseFailed的實作 //購買失敗時會自動call此函式 public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) { resultText.text = $"購買失敗, Product: '{product.definition.storeSpecificId}', 原因: {failureReason}"; } //按下按鈕時觸發此函式 public void OnBuyClick() { if (!IsInitialized()) { resultText.text = $"初始化沒完成喔!"; return; } Product product = m_StoreController.products.WithID(productID); if (product != null && product.availableToPurchase) { resultText.text = $"開始購買: {product.definition.id}"; m_StoreController.InitiatePurchase(product); } else { resultText.text = $"購買失敗 {product.definition.storeSpecificId}可能是無效的"; } } }
- 在把TestIAP.cs掛在按鈕上, 並把文字元件拉進"Result Text"欄位
- 在按鈕的On Click()加上TestIAP.OnBuyClick, 這樣在按下去時就會觸發函式了
- 接著執行Play, 應該就能看到"初始化成功, 按下按鈕應該就會出現"購買成功, Product: test"
也可以Build APK放到手機上試試看喔!
------
以上, 如果對你有幫助的話歡迎給我一些斗內支持, 你可以:
- 購買斗內咕咕App(就是用這篇文章教的東西做的喔!): https://play.google.com/store/apps/details?id=com.Goo.DonateToGoo
- Web donate: https://core.newebpay.com/EPG/GooDonateBox/7HM3Jf
作者已經移除這則留言。
回覆刪除