Unity接入苹果内购前,需要提前配置好产品的内购ID以及拥有苹果账号,关于这个提前准备各位需要自行百度解决(笔者是运维那边配置好了内购ID直接发ID给我使用即可)
大多数游戏在苹果商城下载的游戏,付费时不可避免的都要通过苹果的内购来进行付款(切支付的除外),虽然不可避免的都要接受苹果的抽成,但总得来说,还是相对方便的。那么以下是在Unity中加入内购的方法。
1、通过Window->General->Services,打开Unity服务面板。
2、选择In-App Purchasing。
3、如果没有工程ID需要创建,选择是否应用针对13岁以下的儿童。创建了可跳过这步。
4、将Off改成On,即内购开启完成。
图解如下:
使用内购的方法主要有两种,一种是通过Unity安装好的UnityIAP来创建内购按钮,并且给按钮配置好相应的内购ID,即可使用.第二种是自己写个内购ID管理,然后通过按钮调用点击方法来验证返回ID并给予奖励.(笔者使用的是第二种方法,第一种方法太过繁琐麻烦就没使用和研究了.)
直接创建IAPManager脚本,完整代码如下:
using UnityEngine;
using UnityEngine.Purchasing;
using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class Products //产品类
{
public string id; //产品内购ID
public int productType; //产品类型
}
public class IAPManager : MonoBehaviour, IStoreListener
{
public static IAPManager instance; //单例
public List<Products> products = new List<Products>();//所有产品列表,公有为了方便直接输入
public string publicKey;//游戏的公共密钥,在苹果ITC后台创建APP的时候有个
ConfigurationBuilder builder;
private IStoreController m_Controller;
private IAppleExtensions m_AppleExtensions;
private static bool isInited = false;
private bool isInitFailed = false;
public GameObject wait;//购买产品等待回应时显示的旋转动画,因为要跨场景所以使用的预制体
GameObject waitPanel;//实例化预制体后的对象
public void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(this);
if (!isInited)
InitPurchase();
}
//初始化内购
void InitPurchase()
{
Debug.Log("初始化");
var module = StandardPurchasingModule.Instance();
builder = ConfigurationBuilder.Instance(module);
for (int i = 0; i < products.Count; i++)
{
builder.AddProduct(products[i].id,
(ProductType)products[i].productType);
}
UnityPurchasing.Initialize(this, builder);
}
///
/// 初始化成功
///
/// Controller.
/// Extensions.
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Debug.Log("初始化成功");
m_Controller = controller;
m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
isInited = true;
}
///
/// iOS ⽹网络延迟错误
///
/// Item.
private void OnDeferred(Product item)
{
Debug.Log("⽹网络连接不不稳");
}
///
/// 初始化失败
///
/// Error.
public void OnInitializeFailed(InitializationFailureReason error)
{
isInitFailed = true;
Debug.Log("初始化失败");
Debug.Log("IAPInitializeFailed!!!" + "Reason:" + error);
}
///
/// 恢复购买
///
public void RestorePurchases()
{
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
if (!isInited)
{
//loading.SetActive(false);
InitPurchase();
}
StartCoroutine("InitAndRestore");
}
}
IEnumerator InitAndRestore()
{
if (isInitFailed || !isInited)
{
//初始化失败
StopCoroutine("InitAndRestore");
}
yield return new WaitUntil(() =>
{
return m_Controller != null && m_AppleExtensions != null;
});
m_AppleExtensions.RestoreTransactions((result) =>
{
// The first phase of restoration. If no more responses are received on ProcessPurchase then
// no purchases are available to be restored.
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
if (result)
{
//产品已经restore,不不过官⽅方的解释是恢复过程成功了了,并不不代表所购买的物品都恢复了了
}
else
{
// 恢复失败
}
StopCoroutine("InitAndRestore");
});
}
///
/// 按钮点击 也可以重写为传入产品ID 此处是序列号,按钮调用的时间为这个,参数为产品内购ID
///
/// Index.
public void OnPurchaseClicked(int index)
{
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer ||
Application.platform == RuntimePlatform.OSXEditor)
{
waitPanel = Instantiate(wait, transform);
waitPanel.transform.SetParent(GameObject.Find("Canvas").transform, false);
if (!isInited)
InitPurchase();
StartCoroutine("InitAndPurchase", index);
}
}
IEnumerator InitAndPurchase(int index)
{
if (isInitFailed || !isInited)
{
//初始化失败
StopCoroutine("InitAndPurchase");
}
yield return new WaitUntil(() =>
{
return m_Controller != null && m_AppleExtensions != null;
});
m_Controller.InitiatePurchase(products[index].id);
StopCoroutine("InitAndPurchase");
}
///
/// 购买成功回调
///
/// The purchase.
/// E.
public PurchaseProcessingResult
ProcessPurchase(PurchaseEventArgs e)
{
Debug.Log("购买成功回调");
if (e.purchasedProduct.definition.id == products[0].id) //产品ID验证要发放什么奖励
{
//此处可以对订单进行验证,因为我们项目吧验证放到服务器了,所以我们可以把购买成功后的凭证发给服务器去验证
//这个购买验证码特别特别长,验证分为沙盒验证和真实验证 就不赘述了
//处理
}
else if (e.purchasedProduct.definition.id == products[1].id)
{
}
else if (e.purchasedProduct.definition.id == products[2].id)
{
}
else if (e.purchasedProduct.definition.id == products[3].id)
{
}
else if (e.purchasedProduct.definition.id == products[4].id)
{
}
else if (e.purchasedProduct.definition.id == products[5].id)
{
}
else if (e.purchasedProduct.definition.id == products[6].id)
{
}
else if (e.purchasedProduct.definition.id == products[7].id)
{
}
else if (e.purchasedProduct.definition.id == products[8].id)
{
}
else if (e.purchasedProduct.definition.id == products[9].id)
{
}
else if (e.purchasedProduct.definition.id == products[10].id)
{
}
else if (e.purchasedProduct.definition.id == products[11].id)
{
}
else if (e.purchasedProduct.definition.id == products[12].id)
{
}
if (waitPanel)
Destroy(waitPanel);
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
{
//购买失败的逻辑
if (waitPanel)
Destroy(waitPanel);
//Debug.Log("购买失败的逻辑");
}
}
注释已经解释清楚,代码参考链接为:
Unity接入苹果内购(IAP)