【Unity基础】Unity用脚本实现内购(IAP)

ops/2024/10/17 9:15:25/

本文介绍了如何使用脚本实现内购功能。

先看下脚本,代码中根据执行过程添加了序号。

using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.UI;namespace Samples.Purchasing.Core.BuyingConsumables
{public class BuyingConsumables : MonoBehaviour, IStoreListener{IStoreController m_StoreController; // The Unity Purchasing system.//Your products IDs. They should match the ids of your products in your store.public string coins100ProductId = "com.xxx.unitygame.coins100";public string coins500ProductId = "com.xxx.unitygame.coins500";public Text CoinsCountText;int m_GoldCount;// 1. 开始入口void Start(){Debug.Log("1. Start begin...");InitializePurchasing();UpdateUI();Debug.Log("1. Start end...");}// 2. 初始化内购void InitializePurchasing(){Debug.Log("2. InitializePurchasing begin...");// 创建实例var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());// 添加内购商品//Add products that will be purchasable and indicate its type.builder.AddProduct(coins100ProductId, ProductType.Consumable);builder.AddProduct(coins500ProductId, ProductType.Consumable);// 初始化UnityPurchasing.Initialize(this, builder);Debug.Log("2. InitializePurchasing end...");}// 4. Button事件处理public void BuyGold(int count){Debug.Log($"4. Player buy gold:{count}");Debug.Log("4. BuyGold begin...");if (count == 100){m_StoreController.InitiatePurchase(coins100ProductId);}else if(count == 500){m_StoreController.InitiatePurchase(coins500ProductId);}Debug.Log("4. BuyGold end...");}// 3. 初始化完成public void OnInitialized(IStoreController controller, IExtensionProvider extensions){Debug.Log("3. OnInitialized begin...");Debug.Log("3. In-App Purchasing successfully initialized");m_StoreController = controller;Debug.Log("3. OnInitialized end...");}public void OnInitializeFailed(InitializationFailureReason error){Debug.Log($"3. In-App Purchasing initialize failed: {error}");}// 5. 购买处理public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args){Debug.Log("5. ProcessPurchase begin...");//Retrieve the purchased productvar product = args.purchasedProduct;//Add the purchased product to the players inventoryif (product.definition.id == coins100ProductId){AddGold(100);}else if (product.definition.id == coins500ProductId){AddGold(500);}Debug.Log($"5. Purchase Complete - Product: {product.definition.id}");Debug.Log("5. ProcessPurchase end...");//We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.return PurchaseProcessingResult.Complete;}public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason){Debug.Log($"5. Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");}// 6. 完成购买事件void AddGold(int count){Debug.Log("6. AddGold begin...");m_GoldCount +=count;UpdateUI();Debug.Log("6. AddGold end...");}// 7. 更新UIvoid UpdateUI(){Debug.Log("7. UpdateUI begin...");CoinsCountText.text = $"Your Coins: {m_GoldCount}";Debug.Log("7. UpdateUI end...");}}
}

根据代码分析,可以看出在Unity中实现内购的过程分为几个关键步骤:

  1. 初始化内购:在Start方法中调用InitializePurchasing,创建一个ConfigurationBuilder实例并添加可购买的产品(如金币)。通过UnityPurchasing.Initialize初始化内购系统。

  2. 处理购买请求:当玩家点击购买按钮时,调用BuyGold方法。根据购买的金币数量,调用m_StoreController.InitiatePurchase发起购买请求。

  3. 购买成功处理:实现OnInitializedProcessPurchase方法。在OnInitialized中确认内购系统已成功初始化。在ProcessPurchase中根据购买的产品ID更新玩家的金币数量。

  4. 错误处理:实现OnInitializeFailedOnPurchaseFailed方法,以便在初始化或购买失败时进行日志记录和处理。

  5. 更新UI:通过UpdateUI方法更新显示玩家当前的金币数量,确保界面与数据保持同步。


http://www.ppmy.cn/ops/124292.html

相关文章

【Android】限制TextView大小并允许滑动

关于TextView大小限制 TextView本身支持大小限制,但只支持固定值 这里改用屏幕比例来判断,按照屏幕剩余空间的一定比例来现在TextView最大尺寸 TextView滑动 当TextView空间不足时,需要通过滑动来查看剩余文本 TextView默认是禁用滑动特…

QTday4

数据库头文件 #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include<QSqlDatabase> //数据库管理类 #include<QSqlQuery> //数据库查询类 #include<QSqlRecord> //记录类QT_BEGIN_NA…

使用aloam跑hesai Pandar-XT32激光雷达数据

参考自利用aloam跑数据集_aloam数据集-CSDN博客 第一步&#xff1a;查看bag的信息 输入rosbag info来查看bag包的信息&#xff1a; joeyjoey-Legion-Y7000P-IRX9:~$ rosbag info /home/joey/Downloads/data2022/indoor/LiDAR_IMU.bag path: /home/joey/Downloads/da…

【aws】从s3里拉取驱动 需要后台创建凭证

简答&#xff1a;建一个有s3readonlyaccess的role&#xff0c;绑定给e2就好了 详细步骤&#xff1a; 1.在控制台搜IAM----左侧导航栏点role/角色----右上角创建角色 2.使用案例里选EC2 3.搜s3readonlyaccess这个策略----创建角色 4.选中指定实例&#xff0c;设置&#xff0c;绑…

项目——超级马里奥——Day(2)

争取今天晚上能搞一半啊&#xff0c;啊啊啊啊&#xff0c;感觉事多的忙不过来 设计思路&#xff1a; 1&#xff09;创建并完成常量类 ------->一张图片的情况 先完成对图片的封装------>把图片加载一遍 &#xff08;老实说&#xff0c;我也不太知道为什么&#xff0…

【使用Java循环输出菱形,空心金字塔】

使用Java循环输出图形的探索之旅 在这篇博客中&#xff0c;我们将探讨如何使用Java中的循环结构来输出各种几何图形&#xff0c;特别是金字塔和菱形。通过这一过程&#xff0c;不仅能够加深对循环的理解&#xff0c;还能提升编程能力。 1. 打印矩形 首先&#xff0c;我们从最…

UE5.3.2查看引擎真正版本

编译好的插件给别人用&#xff0c;发现引擎不一致&#xff0c;而且双方都是5.3.2版本引擎 打开Help->About Unreal Editor可以看到引擎版本 或者直接查看引擎版本文件&#xff1a;XXXXX\Engine\Build\Build.version 里面能看到对应的分支名字

2024Java最新面试题总结(针对于一些小厂、中厂)

这是根据个人面试经历总结出来的一些经验希望可以帮助到有需要的人。 面试的时候&#xff0c;会先让你进行自我介绍&#xff0c;这个大家准备一两分钟的面试稿就可以。然后就是正式面试&#xff0c;面试官一般是两个人以上&#xff0c;开始&#xff0c;面试官会先提问一些基本…