Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.ChannelPurchase;
- using UnityEngine.Purchasing;
- using UnityEngine.Store;
- using UnityEngine.UI;
- public class XLoginListener : ILoginListener
- {
- public Action OnInitSuccess;
- public Action<string> OnInitFailure;
- public Action<UserInfo> OnLoginSuccess;
- public Action<string> OnLoginFailure;
- public void OnLogin(UserInfo User)
- {
- Debug.LogErrorFormat("[Xiaomi] Login Succeed: userId {0}, userLoginToken {1}, channel {2}",
- User.userId, User.userLoginToken, User.channel);
- OnLoginSuccess.SafeInvoke(User);
- }
- public void OnInitialized()
- {
- Debug.LogError("[Xiaomi] Initialization Succeed");
- OnInitSuccess.SafeInvoke();
- }
- public void OnInitializeFailed(string Message)
- {
- Debug.LogError("[Xiaomi] Initialization Failed: " + Message);
- OnInitFailure.SafeInvoke(Message);
- }
- public void OnLoginFailed(string Message)
- {
- Debug.LogError("[Xiaomi] Login Failed: " + Message);
- OnLoginFailure.SafeInvoke(Message);
- }
- }
- public class XStoreListener : IStoreListener
- {
- public Action OnPurchaseEnd;
- public Action OnPurchaseBegin;
- public Action<GameProduct> OnPurchaseSuccess;
- public IStoreController StoreController;
- public bool IsInitialized
- {
- get { return (StoreController != null); }
- }
- public void OnInitialized(IStoreController Controller, IExtensionProvider Extensions)
- {
- Debug.LogError("Billing initialized");
- StoreController = Controller;
- }
- public void OnInitializeFailed(InitializationFailureReason Error)
- {
- Debug.LogError("Billing failed to initialize!");
- switch (Error)
- {
- case InitializationFailureReason.AppNotKnown:
- Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
- break;
- case InitializationFailureReason.PurchasingUnavailable:
- // Ask the user if billing is disabled in device settings.
- Debug.LogError("Billing disabled!");
- break;
- case InitializationFailureReason.NoProductsAvailable:
- // Developer configuration error; check product metadata.
- Debug.LogError("No products available for purchase!");
- break;
- }
- }
- public void OnPurchaseFailed(Product Item, PurchaseFailureReason Error)
- {
- OnPurchaseEnd.SafeInvoke();
- Debug.Log("Purchase failed: " + Item.definition.id + ". Error: " + Error);
- }
- public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs Args)
- {
- try
- {
- OnPurchaseEnd.SafeInvoke();
- OnPurchaseSuccess.SafeInvoke(Game.ToGameProduct(Args.purchasedProduct));
- }
- catch (Exception e)
- {
- Debug.LogError("PurchaseProcessingResult Exception: " + e);
- }
- return (PurchaseProcessingResult.Complete);
- }
- }
- public class XPlatformDef
- {
- public XStoreListener StoreListener;
- public XLoginListener LoginListener;
- }
- public partial class Game : MonoBehaviour
- {
- XPlatformDef Xiaomi;
- void Xiaomi_Init() // Gets called in the game's Awake on app startup
- {
- StandardPurchasingModule Module = StandardPurchasingModule.Instance();
- ConfigurationBuilder Builder = ConfigurationBuilder.Instance(Module);
- ProductCatalog Catalog = ProductCatalog.LoadDefaultCatalog();
- foreach (ProductCatalogItem Product in Catalog.allProducts)
- {
- if (Product.allStoreIDs.Count > 0)
- {
- IDs IdList = new IDs();
- foreach (StoreID ID in Product.allStoreIDs)
- {
- IdList.Add(ID.id, ID.store);
- }
- Builder.AddProduct(Product.id, Product.type, IdList);
- }
- else
- {
- Builder.AddProduct(Product.id, Product.type);
- }
- }
- Xiaomi = new XPlatformDef();
- Xiaomi.LoginListener = new XLoginListener();
- Xiaomi.StoreListener = new XStoreListener()
- {
- OnPurchaseSuccess = ...,
- OnPurchaseEnd = ...,
- OnPurchaseBegin = ...,
- };
- AppInfo App = new AppInfo();
- App.appId = "...";
- App.appKey = "...";
- App.clientId = "...";
- App.clientKey = "...";
- App.debug = true;
- #if UNITY_EDITOR
- {
- UnityPurchasing.Initialize(Xiaomi.StoreListener, Builder);
- }
- #else
- {
- Xiaomi.LoginListener.OnInitSuccess = ()=>
- {
- UnityPurchasing.Initialize(Xiaomi.StoreListener, Builder);
- };
- StoreService.Initialize(App, Xiaomi.LoginListener);
- StoreService.Login(Xiaomi.LoginListener);
- }
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement