您的位置:首页 > 健康 > 美食 > 互联网电商公司排名_北京有几家宽带网络公司_巨量算数数据分析入口_企业建设网站公司

互联网电商公司排名_北京有几家宽带网络公司_巨量算数数据分析入口_企业建设网站公司

2025/3/17 20:57:40 来源:https://blog.csdn.net/tealcwu/article/details/142862895  浏览:    关键词:互联网电商公司排名_北京有几家宽带网络公司_巨量算数数据分析入口_企业建设网站公司
互联网电商公司排名_北京有几家宽带网络公司_巨量算数数据分析入口_企业建设网站公司

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

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

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方法更新显示玩家当前的金币数量,确保界面与数据保持同步。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com