Browse Source

加入ios方面的一些代码

hexiaojie 1 year ago
parent
commit
5678a3cca4

+ 243 - 0
GameClient/Assets/Game/HotUpdate/Platform/QDAppStoreManager.cs

@@ -0,0 +1,243 @@
+#if !UNITY_EDITOR && UNITY_IOS
+using UnityEngine.Purchasing;
+using UnityEngine;
+using ET;
+
+namespace GFGGame
+{
+    public class QDAppStoreManager
+    {
+        public static void Init()
+        {
+            IAPManager.Instance.InitializePurchasing();
+//#if !UNITY_EDITOR
+//            ReYunSDKManager.Instance.Init();
+//#endif
+        }
+
+        public static void Pay(int buyID, int count, string orderID, long Price)
+        {
+            IAPManager.Instance.Pay(IAPManager.Instance.GetIosProductId(buyID), count, orderID, Price);
+        }
+
+        public static void OnCreateRole()
+        {
+//#if !UNITY_EDITOR
+//            var accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
+//            ReYunSDKManager.Instance.Register(accountInfoComponent.Account);
+//#endif
+        }
+
+        public static void OnEnterGame()
+        {
+            IAPManager.Instance.CheckOrderToDo();
+
+//#if !UNITY_EDITOR
+//            var accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
+//            ReYunSDKManager.Instance.Login(accountInfoComponent.Account);
+//#endif
+        }
+    }
+
+    public class IAPManager : SingletonBase<IAPManager>, IStoreListener
+    {
+        IStoreController m_StoreController; // The Unity Purchasing system.
+        private IAppleExtensions m_AppleExtensions;
+        private bool canMakePayments;
+        private string OrderId;
+
+        private string OrderIdLocal
+        {
+            get
+            {
+                return PlayerPrefs.GetString(OrderIdLocalkey, null);
+            }
+            set
+            {
+                PlayerPrefs.SetString(OrderIdLocalkey, value);
+            }
+        }
+        private string TransactionId
+        {
+            get
+            {
+                return PlayerPrefs.GetString(TransactionIdLocalkey, null);
+            }
+            set
+            {
+                PlayerPrefs.SetString(TransactionIdLocalkey, value);
+            }
+        }
+
+        private string OrderIdLocalkey
+        {
+            get { return RoleDataManager.roleId + "OrderId"; }
+        }
+
+        private string TransactionIdLocalkey
+        {
+            get { return RoleDataManager.roleId + "transactionID"; }
+        }
+
+        public void CheckOrderToDo()
+        {
+            if (!string.IsNullOrEmpty(this.OrderIdLocal) && !string.IsNullOrEmpty(this.TransactionId))
+            {
+                IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, this.TransactionId).Coroutine();
+            }
+        }
+
+        public void Pay(string buyID, int count, string orderID, long Price)
+        {
+            Debug.Log($"Pay {buyID}");
+            if(!canMakePayments)
+            {
+                PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
+                return;
+            }
+
+            if(!string.IsNullOrEmpty(this.OrderIdLocal))
+            {
+                PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
+                CheckOrderToDo();
+                return;
+
+            }
+            if(!string.IsNullOrEmpty(this.OrderId))
+            {
+                PromptController.Instance.ShowFloatTextPrompt("有未完成的订单,请稍后再试!");
+                return;
+            }
+            ViewManager.Show<ModalStatusView>("");
+            this.OrderId = orderID;
+            m_StoreController.InitiatePurchase(buyID);
+        }
+
+        public void OnServerSuccess(string OrderId, string TransactionId)
+        {
+            this.OrderId = null;
+            this.OrderIdLocal = null;
+            this.TransactionId = null;
+            PlayerPrefs.DeleteKey(OrderIdLocalkey);
+            PlayerPrefs.DeleteKey(TransactionIdLocalkey);
+        }
+
+        public void InitializePurchasing()
+        {
+            Debug.Log("InitializePurchasing");
+
+            var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
+            canMakePayments = builder.Configure<IAppleConfiguration>().canMakePayments;
+            builder.Configure<IAppleConfiguration>().SetApplePromotionalPurchaseInterceptorCallback(OnPromotionalPurchase);
+            var dataArray = ShopCfgArray.Instance.dataArray;
+            foreach(var shopCfg in dataArray)
+            {
+                if(shopCfg.costType == CostType.RMB)
+                {
+                    builder.AddProduct(GetIosProductId(shopCfg.id), ProductType.Consumable);
+                }
+            }
+
+            UnityPurchasing.Initialize(this, builder);
+        }
+
+        public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
+        {
+            Debug.Log("In-App Purchasing successfully initialized");
+            m_StoreController = controller;
+            m_AppleExtensions = extensions.GetExtension<IAppleExtensions>();
+
+            // 在 Apple 平台上,我们需要处理由 Apple 的"购买前先询问"功能导致的延期购买。
+            //在非 Apple 平台上,这将不起作用;永远不会调用 OnDeferred。
+            m_AppleExtensions.RegisterPurchaseDeferredListener(OnDeferred);
+        }
+
+        public void OnInitializeFailed(InitializationFailureReason error)
+        {
+            Debug.Log($"In-App Purchasing initialize failed: {error}");
+        }
+
+        public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
+        {
+            Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
+
+            this.OrderId = null;
+            ViewManager.Hide<ModalStatusView>();
+            
+            switch (failureReason)
+            {
+                case PurchaseFailureReason.PurchasingUnavailable:
+                    PromptController.Instance.ShowFloatTextPrompt("应用内购 (IAP) 可能在设备设置中受到限制,请开启后再试。");
+                    break;
+                case PurchaseFailureReason.ExistingPurchasePending:
+                    PromptController.Instance.ShowFloatTextPrompt("现购待定,请稍后再试。");
+                    break;
+                case PurchaseFailureReason.ProductUnavailable:
+                    PromptController.Instance.ShowFloatTextPrompt("该商品不可购买,请稍后再试。");
+                    break;
+                case PurchaseFailureReason.SignatureInvalid:
+                    PromptController.Instance.ShowFloatTextPrompt("验证失败,请稍后再试。");
+                    break;
+                case PurchaseFailureReason.UserCancelled:
+                    
+                    break;
+                case PurchaseFailureReason.PaymentDeclined:
+                    PromptController.Instance.ShowFloatTextPrompt("此次购买被拒绝,请稍后再试。");
+                    break;
+                case PurchaseFailureReason.DuplicateTransaction:
+                    PromptController.Instance.ShowFloatTextPrompt("重复的交易,请稍后再试。");
+                    break;
+                default:
+                    PromptController.Instance.ShowFloatTextPrompt("购买失败,请稍后再试!");
+                    break;
+            }
+        }
+
+        public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
+        {
+            ViewManager.Hide<ModalStatusView>();
+            //Retrieve the purchased product
+            var product = args.purchasedProduct;
+            Debug.Log($"Purchase Complete - Product: {product.definition.id}");
+            this.OrderIdLocal = this.OrderId;
+            this.TransactionId = product.transactionID;
+            GameGlobal.maxShowOrderTime = 300000;
+            IOSRechargeSProxy.IosVerifyOrder(OrderIdLocal, product.transactionID).Coroutine();
+
+            //We return Complete, informing IAP that the processing on our side is done and the transaction can be closed.
+            return PurchaseProcessingResult.Complete;
+        }
+
+        /// &lt;summary>
+        /// 特定于 iOS。
+        /// 未成年人要求购买并提交给家长批准时,
+        /// 此函数将作为 Apple 的"购买前先询问"功能的一部分
+        /// 接受调用。
+        ///
+        /// 购买被批准或拒绝时,将触发正常的购买
+        /// 事件。
+        /// &lt;/summary>
+        /// &lt;param name="item">Item.&lt;/param>
+        private void OnDeferred(Product item)
+        {
+            Debug.Log("Purchase deferred: " + item.definition.id);
+            PromptController.Instance.ShowFloatTextPrompt("未成年人购买需要经过家长批准才能继续!");
+        }
+
+        private void OnPromotionalPurchase(Product item)
+        {
+            Debug.Log("Attempted promotional purchase: " + item.definition.id);
+            // 已检测到推荐性购买。
+            // 通过呈现家长控制门等方式处理此事件。
+            //此处,仅出于演示目的,我们将等待五秒钟
+            // 再继续购买。
+            //StartCoroutine(ContinuePromotionalPurchases());
+        }
+
+        public string GetIosProductId(int cfgId)
+        {
+            return "wsj" + cfgId;
+        }
+    }
+}
+#endif

+ 3 - 0
GameClient/Assets/Game/HotUpdate/Platform/QDAppStoreManager.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: f80db6686bd24e3d9d1dad9298372a45
+timeCreated: 1722951514

+ 26 - 0
GameClient/Assets/Game/HotUpdate/Platform/QDManager.cs

@@ -24,6 +24,11 @@ namespace GFGGame
                     QDDouYouManager.Instance.Init();
                     QDShareManager.Instance.Init();
                     break;
+                case (int)ChannelID.AppStore:
+#if !UNITY_EDITOR && UNITY_IOS
+                    QDAppStoreManager.Init();
+#endif
+                    break;
                 default:
                     break;
             }
@@ -49,6 +54,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.Login();
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接douyou ios的sdk登录
+                    break;
                 default:
                     break;
             }
@@ -85,6 +93,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.OnEnterGame();
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }
@@ -104,6 +115,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.OnQuitToLoginView();
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }
@@ -122,6 +136,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.Pay(buyID, count, orderID, price);
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }
@@ -148,6 +165,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.Logout();
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }
@@ -166,6 +186,9 @@ namespace GFGGame
                 case (int)ChannelID.DouYouDev:
                     QDDouYouManager.Instance.Exit();
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }
@@ -224,6 +247,9 @@ namespace GFGGame
                         roleInfoDev.Id.ToString(), lvlDev.ToString(), roleInfoDev.Name, roleInfoDev.ServerId.ToString(),
                         serverNameDev);
                     break;
+                case (int)ChannelID.AppStore:
+                    //TODO 接入douYou sdk 
+                    break;
                 default:
                     break;
             }

+ 48 - 0
GameClient/Assets/Game/Launcher/Platform/ATTAuth.cs

@@ -0,0 +1,48 @@
+using System;
+using UnityEngine;
+
+namespace GFGGame.Launcher
+{
+    public class ATTAuth : MonoBehaviour
+    {
+        [System.Runtime.InteropServices.DllImport("__Internal")]
+        private static extern void _RequestTrackingAuthorizationWithCompletionHandler();
+
+        [System.Runtime.InteropServices.DllImport("__Internal")]
+        private static extern int _GetAppTrackingAuthorizationStatus();
+
+        private static Action<int> getAuthorizationStatusAction;
+
+        /// <summary>
+        /// 请求ATT授权窗口
+        /// </summary>
+        /// <param name="getResult"></param>
+        public static void RequestTrackingAuthorizationWithCompletionHandler(Action<int> getResult)
+        {
+            //-1:"ios版本低于14"
+            //0: "ATT 授权状态待定";
+            //1: "ATT 授权状态受限";
+            //2: "ATT 已拒绝";
+            //3: "ATT 已授权";
+            Debug.Log("RequestTrackingAuthorizationWithCompletionHandler");
+            getAuthorizationStatusAction = getResult;
+            _RequestTrackingAuthorizationWithCompletionHandler();
+        }
+
+        /// <summary>
+        /// 获取当前ATT授权状态
+        /// </summary>
+        /// <returns></returns>
+        public static int GetAppTrackingAuthorizationStatus()
+        {
+            return _GetAppTrackingAuthorizationStatus();
+        }
+
+        public void GetAuthorizationStatus(string status)
+        {
+            getAuthorizationStatusAction?.Invoke(int.Parse(status));
+        }
+
+    }
+}
+

+ 3 - 0
GameClient/Assets/Game/Launcher/Platform/ATTAuth.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 27ce48eca6084671b666830d26548e8c
+timeCreated: 1722951344

+ 46 - 0
GameClient/Assets/Game/Launcher/Platform/QDAppStoreManagerInit.cs

@@ -0,0 +1,46 @@
+using UnityEngine;
+using UniFramework.Event;
+
+namespace GFGGame.Launcher
+{
+
+    public static class QDAppStoreManagerInit
+    {
+        public static int AppTrackingAuthorizationStatus;
+        private const string ATTStatusLocalKey = "ATTStatusLocalKey";
+
+        public static void InitPlatform()
+        {
+#if !UNITY_EDITOR && UNITY_IOS
+            AppTrackingAuthorizationStatus = ATTAuth.GetAppTrackingAuthorizationStatus();
+            if (AppTrackingAuthorizationStatus == 0)
+            {
+                bool requested = LocalCache.GetBool(ATTStatusLocalKey, false);
+                if(!requested)
+                {
+                    AddIOSMethod();
+                    ATTAuth.RequestTrackingAuthorizationWithCompletionHandler((status) =>
+                    {
+                        Debug.Log("ATT status :" + status);
+                        AppTrackingAuthorizationStatus = status;
+                    });
+                }
+            }
+#endif
+            UniEvent.SendMessage(new LauncherEvent.InitPlatformResult() { success = true});
+        }
+
+
+        private static void AddIOSMethod()
+        {
+            string objName = "IOSMethod";
+            var obj = GameObject.Find(objName);
+            if(obj == null)
+            {
+                obj = new GameObject(objName);
+                GameObject.DontDestroyOnLoad(obj);
+            }
+            obj.AddComponent<ATTAuth>();
+        }
+    }
+}

+ 3 - 0
GameClient/Assets/Game/Launcher/Platform/QDAppStoreManagerInit.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: b1262b0f94484df486b817b6c864e73f
+timeCreated: 1722951338

+ 3 - 0
GameClient/Assets/Game/Launcher/Platform/QDManagerInit.cs

@@ -17,6 +17,9 @@ namespace GFGGame.Launcher
                     QDDouYouManagerInit.Instance.InitSDK(LauncherConfig.douYouAdId);
                     QDShareManagerInit.Instance.InitSDK();
                     break;
+                case (int)ChannelID.AppStore:
+                    QDAppStoreManagerInit.InitPlatform();
+                    break;
                 default:
                     UniEvent.SendMessage(new LauncherEvent.InitPlatformResult() { success = true });
                     QDShareManagerInit.Instance.InitSDK();