Author :JerryYang
Create by 2021.03.24
環(huán)境:
Unity:2020.3.0f1c1
一秽荞、后臺(tái)商品配置
- 進(jìn)入蘋果開發(fā)者后臺(tái)扬跋,填寫稅務(wù)信息钦听,銀行信息(不填會(huì)出現(xiàn)購(gòu)買失斊由稀)卒煞;
- 選擇你要添加內(nèi)購(gòu)的應(yīng)用畔裕,找到 In-App Purchases 選項(xiàng)扮饶,點(diǎn)擊 Manage;
- 點(diǎn)擊添加商品靠粪,然后完善商品信息;
注意:這里要填寫所以信息昔善,包括截圖畔乙,直到狀態(tài)為 Ready to Submit 為止牲距,不然也會(huì)出現(xiàn)購(gòu)買失敗
二牍鞠、unity API接入
-
點(diǎn)擊 manage service 打開services面板萤晴;
- 選擇一個(gè)組織店读,沒(méi)有的話創(chuàng)建一個(gè)屯断;
-
根據(jù)提示打開Purchasing殖演,導(dǎo)入包紧武,如果需要更新阻星,請(qǐng)點(diǎn)擊update妥箕;
-
購(gòu)買功能可以通過(guò)unity提供的組件實(shí)現(xiàn),也可以用代碼實(shí)現(xiàn)缆蝉,我們選擇用代碼實(shí)現(xiàn)。
三诸尽、代碼部分
using UnityEngine;
using UnityEngine.Purchasing;
using System;
public class PurchasingManager : IStoreListener
{
public static readonly PurchasingManager Instance = new PurchasingManager();
private IStoreController storeController;
private static IExtensionProvider extensionProvider;
private static IAppleExtensions appleExtension;
//private static IGooglePlayStoreExtensions googleExtension;
private Action<string> failedCallback;
private Action<Product> successedCallback;
PurchasingManager()
{
}
/// <summary>
/// 初始化商品
/// 建議在游戲初始化完成的時(shí)候就去初始化商品
/// </summary>
public void InitProduct()
{
if (IsInitialized()) return;
var module = StandardPurchasingModule.Instance();
ConfigurationBuilder builder = ConfigurationBuilder.Instance(module);
builder.AddProduct("goods_coins100", ProductType.Consumable);
builder.AddProduct("goods_vip7days", ProductType.NonConsumable);
UnityPurchasing.Initialize(this, builder);
}
/// <summary>
/// 發(fā)起內(nèi)購(gòu)
/// </summary>
/// <param name="_productId">要購(gòu)買的商品ID</param>
/// <param name="_successedCallback">購(gòu)買成功回調(diào)</param>
/// <param name="_failedCallback">購(gòu)買失敗回調(diào)</param>
public void Purchase(string _productId, Action<Product> _successedCallback, Action<string> _failedCallback)
{
failedCallback = _failedCallback;
successedCallback = _successedCallback;
if (!IsInitialized())
{
OnFailedCallback("Not initialized.");
return;
}
var product = storeController.products.WithID(_productId);
if (product == null || !product.availableToPurchase)
{
OnFailedCallback("Either is not found or is not available for purchase");
return;
}
Debug.Log($"Inicializando compra del producto {product.metadata.localizedTitle} en la tienda.");
storeController.InitiatePurchase(product);
}
/// <summary>
/// IOS恢復(fù)內(nèi)購(gòu)
/// Google會(huì)在刪除應(yīng)用后,第一次安裝是自動(dòng)恢復(fù)
/// </summary>
/// <param name="restoreCallback">恢復(fù)回調(diào)</param>
public void IosRestore(Action<bool> restoreCallback)
{
if (appleExtension != null)
{
appleExtension.RestoreTransactions(restoreCallback);
}
else
{
Debug.LogWarning("IAppleExtensions is null");
restoreCallback(false);
}
}
//======================================分割線=========================================
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
storeController = controller;
extensionProvider = extensions;
appleExtension = extensions.GetExtension<IAppleExtensions>();
//googleExtension = extensions.GetExtension<IGooglePlayStoreExtensions>();
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.LogWarning("OnInitializeFailed Reason:" + error);
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Debug.LogWarning("OnPurchaseFailedproduct:"+ product.transactionID + " failureReason:"+ failureReason);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
if (successedCallback != null)
{
successedCallback(purchaseEvent.purchasedProduct);
}
return PurchaseProcessingResult.Complete;
}
private bool IsInitialized()
{
return storeController != null && extensionProvider != null;
}
private void OnFailedCallback(string _reason)
{
if (failedCallback != null)
{
failedCallback(_reason);
}
}
}
四、打包測(cè)試
包名一定和你后臺(tái)的包名對(duì)應(yīng)仲闽;
商品ID和類型也要和后臺(tái)對(duì)應(yīng)蔼囊;
-
在蘋果開發(fā)者后臺(tái)的用戶和訪問(wèn)菜單中找到沙盒畏鼓,添加沙盒測(cè)試人員壶谒;
在App Store登陸該測(cè)試賬號(hào)让禀;
-
點(diǎn)擊要購(gòu)買的商品巡揍,輸入密碼腮敌,彈出以下彈窗的時(shí)候說(shuō)明購(gòu)買成功了
走到這里說(shuō)明你接成功了糜工,恭喜你捌木。
暫時(shí)寫這么多刨裆,有不懂的或者需要補(bǔ)充的請(qǐng)私信我。