using System; using System.Collections; using System.Collections.Generic; using HC; using UnityEngine; #if USE_IAP using UnityEngine.Purchasing; #endif public partial class HCSDKManager : HCSingleton { // 内购回调 public Action OnPurchaseDone; // 恢复购买回调 public Action OnRestoreDone; // 游戏暂停/恢复声音回调 public Action SetGameFocusListener; // Adjust归因回调 public Action OnUserSourceListenerCallback; private bool isInit = false; /// /// 初始化SDK /// /// public void InitializeSdk(Action _initCallback = null) { if (isInit) return; HCAdsManager.Instance.InitializeSdk(); HCAnalyticsManager.Instance.InitializeSdk(); SensitiveManager.Instance.InitializeSdk(); isInit = true; _initCallback?.Invoke(); if (!HCTools.HasKey(HCTools.FIRST_TIME_OPEN_GAME)) { HCTools.SavePlayerPrefsInt(HCTools.FIRST_TIME_OPEN_GAME, (int)HCTimeTools.GetCurrentTimestamp()); } } #region Ads /// /// 播放激励视频广告 /// /// 激励视频广告点位 /// 激励视频关闭回调 /// 激励视频展示失败回调 public void ShowRewardedAd(HCRVPositionName _adPos, Action _rewardCallback = null, Action _showFailedCallback = null, bool _useSDKToast = true) { HCAdsManager.Instance.ShowRewardedAd(_adPos, _rewardCallback, _showFailedCallback, _useSDKToast); } /// /// 展示插屏广告 /// /// 插屏点位 /// 插屏关闭回调 public void ShowInterstitial(HCIVPositionName _adPos, HCIVADType _IvType = HCIVADType.IV1, Action _closeCallback = null) { HCAdsManager.Instance.ShowInterstitial(_adPos, _IvType, _closeCallback); } /// /// 插屏广告是否准备好 /// /// public bool IsInterstitialReady() { return HCAdsManager.Instance.IsInterstitialReady(); } /// /// 激励视频广告是否准备好 /// /// public bool IsRewardedAdReady() { return HCAdsManager.Instance.IsRewardedAdReady(); } /// /// 展示banner /// public void ShowBanner() { HCAdsManager.Instance.ShowBanner(); } /// /// 隐藏banner /// public void HideBanner() { HCAdsManager.Instance.HideBanner(); } public bool IsNativeAdReady(string adUnit = "") { return HCAdsManager.Instance.IsNativeReady(adUnit); } public void ShowNative(RectTransform pRect, Camera pCam = null, string pAdPos = "", string adUnit = "", Action _showFailedCallback = null, Action _closeCallback = null) { HCAdsManager.Instance.ShowNative(pRect, pCam, pAdPos, adUnit, _showFailedCallback, _closeCallback); } public void RemoveNative(string adUnit = "") { HCAdsManager.Instance.RemoveNative(adUnit); } #endregion #region Analytics /// /// Log Event /// /// /// 上报平台,all 包含firebase/数数 public void LogEvent(string _eventSort, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All) { HCAnalyticsManager.Instance.TrackEvent(_eventSort, _trackEventPlatform); } /// /// Log Event /// /// /// /// /// 上报平台,all 包含firebase/数数 public void LogEvent(string _eventSort, string _key, object _value, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All) { HCAnalyticsManager.Instance.TrackEvent(_eventSort,_key,_value, _trackEventPlatform); } /// /// Log Event /// /// /// /// /// /// /// 上报平台,all 包含firebase/数数 public void LogEvent(string _eventSort, string _key01, object _value01, string _key02, object _value02, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All) { HCAnalyticsManager.Instance.TrackEvent(_eventSort, _key01, _value01, _key02, _value02, _trackEventPlatform); } /// /// /// /// /// /// 上报平台,all 包含firebase/数数 public void LogEvent(string _eventSort, Dictionary _eventDic, TrackEventPlatform _trackEventPlatform = TrackEventPlatform.All) { HCAnalyticsManager.Instance.TrackEvent(_eventSort, _eventDic, _trackEventPlatform); } #endregion #region review /// /// 评论 /// public void Review() { HCNativeInterface.Instance.NativeReview(); } #endregion #region /// /// /// /// 在线参数key /// 在线参数默认值 /// public string GetRemoteConfigStr(string _key, string _defaultValue) { #if !UNITY_EDITOR return HCAnalyticsManager.Instance.GetRemoteConfigStr(_key,_defaultValue); #else return _defaultValue; #endif } /// /// /// /// 在线参数key /// 在线参数默认值 /// public int GetRemoteConfigInt(string _key, int _defaultValue) { #if !UNITY_EDITOR return HCAnalyticsManager.Instance.GetRemoteConfigInt(_key, _defaultValue); #else return _defaultValue; #endif } /// /// /// /// 在线参数key /// 在线参数默认值 /// public bool GetRemoteConfigBool(string _key, bool _defaultValue) { #if !UNITY_EDITOR return HCAnalyticsManager.Instance.GetRemoteConfigBool(_key, _defaultValue); #else return _defaultValue; #endif } #endregion #region other public void SetUserSourceListenerCallback(Action OnUserSourceListenerCallbackAction) { OnUserSourceListenerCallback = OnUserSourceListenerCallbackAction; } public void Shake(int _shakeType, float _intensity = 1) { #if !UNITY_EDITOR HCNativeInterface.Instance.Shake(_shakeType, _intensity); #endif } public void ShowToast(string _msg) { #if !UNITY_EDITOR HCNativeInterface.Instance.ShowToast(_msg); #endif } /// /// 设置公共事件属性 /// /// public void SetSuperProperties(Dictionary _mPoperties) { HCAnalyticsManager.Instance.SetSuperProperties(_mPoperties); } /// /// 设置用户属性 /// /// public void SetUserProperties(Dictionary _mPoperties) { HCAnalyticsManager.Instance.SetUserProperties(_mPoperties); } public void SetLogEnable(bool _enable) { #if !UNITY_EDITOR HCOtherConfigs.IsDebugLog = _enable; HCAdsManager.Instance.SetLogEnable(_enable); HCAnalyticsManager.Instance.SetLogEnable(_enable); #endif } public void LogRewardBtnShow(string _pos) { #if !UNITY_EDITOR HCAnalyticsManager.Instance.LogRewardBtnShow(_pos); #endif } #endregion #region IAP /// /// 购买商品回调 /// orderID, productName, productID, purchaseResult, gameExtra /// /// 返回 public void SetOnPurchaseDone(Action onPurchaseDoneAction) { OnPurchaseDone = onPurchaseDoneAction; } #if USE_IAP /// /// 获取 AppStore/Google Play 上所有配置的商品; /// /// 返回所有商品信息 /// ex: /// Product[0].metadata.localizedTitle /// Product[0].metadata.localizedPriceString /// Product[0].metadata.localizedDescription /// Product[0].metadata.isoCurrencyCode public void OnGetProductsInfo(Action onGetProductsInfo) { HCPurchaseManager.Instance.OnGetProductsInfo += onGetProductsInfo; } /// /// 根据商品ID获取商品信息 /// /// 商品ID /// 返回商品信息 public Product GetProductInfoByID(string productID) { return HCPurchaseManager.Instance.GetProductInfoByID(productID); } /// /// 获取带货币符号价格字符串 /// /// 商品ID /// 返回带货币符号字符串 public string GetPriceByID(string productID) { return HCPurchaseManager.Instance.GetPriceByID(productID); } /// /// 点击购买方法 /// /// 商品SKU /// 商品名称 /// 游戏拓展参数 public void BuyProductByID(string productId, string enProductName, string gameExtraParam = "") { HCPurchaseManager.Instance.BuyProductByID(productId, enProductName, gameExtraParam); } /// /// 动态添加商品ID /// /// Product IDs /// bool Is it added successfully string Additional Information public void AddProducts(Dictionary products) { HCPurchaseManager.Instance.AddProducts(products); } /// /// 恢复购买 /// public void RestorePurchases() { HCPurchaseManager.Instance.RestorePurchases(); } public void SetRestoreDone(Action onRestoreDoneAction) { OnRestoreDone = onRestoreDoneAction; } /// /// IAP 按钮展示 /// /// 商品名称 /// 商品ID public void LogIAPBtnShow(string productName, string productId) { var productInfo = GetProductInfoByID(productId); var currency = ""; var price = ""; if (productInfo != null) { currency = productInfo.metadata.isoCurrencyCode; price = productInfo.metadata.localizedPriceString; } #if !UNITY_EDITOR HCAnalyticsManager.Instance.IAPBtnShow(productName, productId, currency, price); #endif } #endif #endregion #region 用户登录 /// /// User Login /// /// 登录类型 /// 登录回调Action public void Login(HCLoginType _loginType, Action _loginResultCallback = null) { #if WEBGL_WX HCDebugger.LogWarning($"微信小游戏只支持微信登陆,默认会把所有传入过来的登陆类型修改为 HCLoginType.LOGIN_BY_WECHAT_MINI_GAME 类型,Editor下会使用游客登陆来模拟微信登陆。当前传入的类型[{_loginType}]"); _loginType = HCLoginType.LOGIN_BY_WECHAT_MINI_GAME; #endif #if WEBGL_BYTEDANCE HCDebugger.LogWarning($"抖音小游戏只支持抖音登陆,默认会把所有传入过来的登陆类型修改为 HCLoginType.LOGIN_BY_BYTEDANCE 类型,Editor下会使用游客登陆来模拟抖音登陆。当前传入的类型[{_loginType}]"); _loginType = HCLoginType.LOGIN_BY_BYTEDANCE; #endif #if UNITY_WEBGL && UNITY_EDITOR _loginType = HCLoginType.LOGIN_BY_GUESTER; #endif HCAccountManager.Instance.Login(_loginType, _loginResultCallback); } /// /// 自动根据上次登陆的类型进行登陆,登陆失败需要跳转到登陆界面。 /// /// public void AutoLogin(Action _loginResultCallback = null) { HCAccountManager.Instance.AutoLogin(_loginResultCallback); } /// /// 获取当前账号类型。 用于显示绑定界面。 /// /// public HCLoginType GetAccountType() { return HCAccountManager.Instance.GetAccountType(); } /// /// 是否可以自动登陆 /// /// public bool IsCanAutoLogin() { return HCAccountManager.Instance.IsCanAutoLogin(); } /// /// Logout /// /// public void Logout(Action _logoutCallback = null) { HCAccountManager.Instance.Logout(_logoutCallback); } /// /// Obtain a list of login channels, /// and the game displays the obtained login list for users to choose to log in. /// /// available login channels public List AvailableLoginChannelList() { return HCAccountManager.Instance.AvailableLoginChannelList(); } /// /// user actively deletes account. /// /// Return the deleted userId. If the game needs to record it, you can operate it in the action. /// /// code Delete userId public void DeleteAccount(Action deleteAccountCallback = null) { HCAccountManager.Instance.DeleteAccount(deleteAccountCallback); } /// /// Social account binding for Guester. /// 游客绑定社交账号 /// /// 游戏发起社交账号的绑定: /// 根据 Action中的BindAccountStatus判断 /// 如果 BindAccountStatus == BindAccountStatus.BIND_CODE_SUCC /// 需要判断userId和当前userId是否一致,如果不一致,需要切换账号,或者重启游戏。如果一致,无需操作。 /// 如果 BindAccountStatus == BindAccountStatus.BIND_CODE_SELECT /// 这个是因为第三方账号已经被绑定过了,string[] 中将会返回已经绑定的userId,和当前userId,游戏需要展示出来提供给玩家选择, /// 玩家选择完成后,需要将选中的userId通过调用ForceBindAccount接口,进行强制绑定。 /// /// 其情况,游戏做相应操作 /// /// /// /// /// 社交账号类型 /// 是否强制绑定 /// 绑定的userId /// 回调的信息 private void BindAccount(HCLoginType type, HCBindType isForce = HCBindType.NO_FORCE, string userId = "", Action bindAccountCallback = null) { HCAccountManager.Instance.BindAccount(type, isForce, userId, bindAccountCallback); } private HCLoginType bindLoginType = HCLoginType.LOGIN_BY_GUESTER; /// /// Social account binding for Guester. /// /// 情况一 : bindAccountStatusEnum == BindAccountStatus.BIND_CODE_SUCC 绑定成功 /// 绑定成功的时候,需要获取 userId和当前userId是否一致, 如果不一致,需要重新切换到新的userId账号上来。 (切换账号) /// 情况二 : BindAccountStatus == BindAccountStatus.BIND_CODE_SELECT 社交账号已经绑定了其他账号 (社交账号已经在其他设备上登陆) /// 需要获取到userIdList 里面的userId列表,获取到userId对应的角色/关卡信息,展示出来供玩家选择。 玩家选择完成之后调用 `ForceBindAccount` 接口将选中的userId传递进去,进行强制绑定 /// ⚠️ 如果选择的是当前账号强制绑定,那么社交账号原有的账号将会被删除 /// 如果选择的是社交账号关联的账号,那么当前账号将会被删除 /// /// /// /// 社交账号类型 /// bindAccountStatusEnum, loginTypeEnum, _msg, _userId, _token, _email,_displayName, _photoUrl, userIdList public void BindAccount(HCLoginType type, Action bindAccountCallback = null) { bindLoginType = type; BindAccount(type, isForce: HCBindType.NO_FORCE, userId: "", bindAccountCallback); } public void ForceBindAccount(string userId, Action bindAccountCallback = null) { BindAccount(bindLoginType, HCBindType.FORCE, userId, bindAccountCallback); } /// /// 获取当前用户信息 /// /// public HC.UserInfo GetCurrAccountInfo() { return HCAccountManager.Instance.GetCurrUserInfo(); } #endregion /// /// /// /// 文本内容 /// 类型1:昵称,2:对话 /// 是否可用,true:可用,false 不可用 public void CheckSensitiveWords(string text, SensitiveType type,Action action) { if (string.IsNullOrEmpty(text)) { action(new SensitiveCheckResult() { HasSensitive = false, Text = text }); return; } SensitiveManager.Instance.CheckSensitiveWords(text, type, action); } }