Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.ChannelPurchase;
  6. using UnityEngine.Purchasing;
  7. using UnityEngine.Store;
  8. using UnityEngine.UI;
  9.  
  10. public class XLoginListener : ILoginListener
  11. {
  12.     public Action OnInitSuccess;
  13.     public Action<string> OnInitFailure;
  14.     public Action<UserInfo> OnLoginSuccess;
  15.     public Action<string> OnLoginFailure;
  16.  
  17.     public void OnLogin(UserInfo User)
  18.     {
  19.         Debug.LogErrorFormat("[Xiaomi] Login Succeed: userId {0}, userLoginToken {1}, channel {2}",
  20.             User.userId, User.userLoginToken, User.channel);
  21.         OnLoginSuccess.SafeInvoke(User);
  22.     }
  23.  
  24.     public void OnInitialized()
  25.     {
  26.         Debug.LogError("[Xiaomi] Initialization Succeed");
  27.         OnInitSuccess.SafeInvoke();
  28.     }
  29.  
  30.     public void OnInitializeFailed(string Message)
  31.     {
  32.         Debug.LogError("[Xiaomi] Initialization Failed: " + Message);
  33.         OnInitFailure.SafeInvoke(Message);
  34.     }
  35.  
  36.     public void OnLoginFailed(string Message)
  37.     {
  38.         Debug.LogError("[Xiaomi] Login Failed: " + Message);
  39.         OnLoginFailure.SafeInvoke(Message);
  40.     }
  41. }
  42.  
  43. public class XStoreListener : IStoreListener
  44. {
  45.     public Action OnPurchaseEnd;
  46.     public Action OnPurchaseBegin;
  47.     public Action<GameProduct> OnPurchaseSuccess;
  48.     public IStoreController StoreController;
  49.  
  50.     public bool IsInitialized
  51.     {
  52.         get { return (StoreController != null); }
  53.     }
  54.  
  55.     public void OnInitialized(IStoreController Controller, IExtensionProvider Extensions)
  56.     {
  57.         Debug.LogError("Billing initialized");
  58.         StoreController = Controller;
  59.     }
  60.  
  61.     public void OnInitializeFailed(InitializationFailureReason Error)
  62.     {
  63.         Debug.LogError("Billing failed to initialize!");
  64.         switch (Error)
  65.         {
  66.             case InitializationFailureReason.AppNotKnown:
  67.                 Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
  68.             break;
  69.  
  70.             case InitializationFailureReason.PurchasingUnavailable:
  71.                 // Ask the user if billing is disabled in device settings.
  72.                 Debug.LogError("Billing disabled!");
  73.             break;
  74.             case InitializationFailureReason.NoProductsAvailable:
  75.                 // Developer configuration error; check product metadata.
  76.                 Debug.LogError("No products available for purchase!");
  77.             break;
  78.         }
  79.     }
  80.  
  81.     public void OnPurchaseFailed(Product Item, PurchaseFailureReason Error)
  82.     {
  83.         OnPurchaseEnd.SafeInvoke();
  84.         Debug.Log("Purchase failed: " + Item.definition.id + ". Error: " + Error);
  85.     }
  86.    
  87.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs Args)
  88.     {
  89.         try
  90.         {
  91.             OnPurchaseEnd.SafeInvoke();
  92.             OnPurchaseSuccess.SafeInvoke(Game.ToGameProduct(Args.purchasedProduct));
  93.         }
  94.         catch (Exception e)
  95.         {
  96.             Debug.LogError("PurchaseProcessingResult Exception: " + e);
  97.         }
  98.  
  99.         return (PurchaseProcessingResult.Complete);
  100.     }
  101. }
  102.  
  103. public class XPlatformDef
  104. {
  105.     public XStoreListener StoreListener;
  106.     public XLoginListener LoginListener;
  107. }
  108.  
  109. public partial class Game : MonoBehaviour
  110. {
  111.     XPlatformDef Xiaomi;
  112.  
  113.     void Xiaomi_Init() // Gets called in the game's Awake on app startup
  114.     {
  115.         StandardPurchasingModule Module = StandardPurchasingModule.Instance();
  116.         ConfigurationBuilder Builder = ConfigurationBuilder.Instance(Module);
  117.  
  118.         ProductCatalog Catalog = ProductCatalog.LoadDefaultCatalog();
  119.         foreach (ProductCatalogItem Product in Catalog.allProducts)
  120.         {
  121.             if (Product.allStoreIDs.Count > 0)
  122.             {
  123.                 IDs IdList = new IDs();
  124.                 foreach (StoreID ID in Product.allStoreIDs)
  125.                 {
  126.                     IdList.Add(ID.id, ID.store);
  127.                 }
  128.                 Builder.AddProduct(Product.id, Product.type, IdList);
  129.             }
  130.             else
  131.             {
  132.                 Builder.AddProduct(Product.id, Product.type);
  133.             }
  134.         }
  135.  
  136.         Xiaomi = new XPlatformDef();
  137.         Xiaomi.LoginListener = new XLoginListener();
  138.         Xiaomi.StoreListener = new XStoreListener()
  139.         {
  140.             OnPurchaseSuccess = ...,
  141.             OnPurchaseEnd = ...,
  142.             OnPurchaseBegin = ...,
  143.         };
  144.        
  145.         AppInfo App = new AppInfo();
  146.         App.appId = "...";
  147.         App.appKey = "...";
  148.         App.clientId = "...";
  149.         App.clientKey = "...";
  150.         App.debug = true;
  151.  
  152.         #if UNITY_EDITOR
  153.         {
  154.             UnityPurchasing.Initialize(Xiaomi.StoreListener, Builder);
  155.         }
  156.         #else
  157.         {
  158.             Xiaomi.LoginListener.OnInitSuccess = ()=>
  159.             {
  160.                 UnityPurchasing.Initialize(Xiaomi.StoreListener, Builder);
  161.             };
  162.             StoreService.Initialize(App, Xiaomi.LoginListener);
  163.             StoreService.Login(Xiaomi.LoginListener);
  164.         }
  165.         #endif
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement