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