using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Touka; using WeChatWASM; public class TKGSDKManager : TKGSingleton { public bool IsNoAllAD { get => mIsNoAllAD; set { mIsNoAllAD = value; if (mIsNoAllAD) { PurchasedRemoveAds(); HideBanner(); } } } private bool mIsNoAllAD = false; public bool IsRemoveAds { get => mIsRemoveAds; set { mIsRemoveAds = value; if (mIsRemoveAds) { PurchasedRemoveAds(); HideBanner(); } } } private bool mIsRemoveAds = false; public bool IsIAPEnabled { get { AppChannel tChannel = GetChannel(); return (tChannel == AppChannel.AppStore_GB || tChannel == AppChannel.GooglePlay); } } private int mLoginCount { get => PlayerPrefs.GetInt(nameof(mLoginCount), 0); set => PlayerPrefs.SetInt(nameof(mLoginCount), value); } private int mPassCount { get => PlayerPrefs.GetInt(nameof(mPassCount), 0); set => PlayerPrefs.SetInt(nameof(mPassCount), value); } private bool mHasPoppedReview { get => PlayerPrefs.GetInt(nameof(mHasPoppedReview), 0) == 1; set => PlayerPrefs.SetInt(nameof(mHasPoppedReview), value ? 1 : 0); } private bool isInit = false; private ITKGSDK m_sdkInterface; protected override void OnInstanceCreate() { base.OnInstanceCreate(); m_sdkInterface = new TKGSDKNative(); } /// /// init sdk /// /// public void InitSDK(Action _initCallback = null) { if (isInit) return; m_sdkInterface.InitSDK(_initCallback); isInit = true; mLoginCount++; if (IsIAPEnabled) { #if USE_IAP IAPTool.Instance.Initialize(); #endif } } /// /// Set game focus /// /// public void SetGameFocusListener(Action _gameFocusAction) { m_sdkInterface.SetGameFocusListener(_gameFocusAction); } // reward click callback public void SetRewardClickListener(Action _clickCallback = null) { m_sdkInterface.SetRewardClickListener(_clickCallback); } /// /// Get channel /// /// public AppChannel GetChannel() { return m_sdkInterface.GetChannel(); } /// /// Set log enable /// public void SetLogEnable(bool _enable) { StaticOtherConfig.IsDebugLog = _enable; m_sdkInterface.SetLogEnable(_enable); } #region Ads // 去广告接口调用 public void PurchasedRemoveAds() { m_sdkInterface.PurchasedRemoveAds(); } /// /// Show Interstitial Ad /// /// Name of interstitial ad placement. /// Callback of interstitial ad close and show interstitial failed /// IVADType for distinguish interstitial ads frequency, default use iv1 public void ShowInterstitialAd(TKGIVAdPositionName _adPos, Action _callback = null, IVADType _IvType = IVADType.IV1) { #if UNITY_EDITOR || NO_AD if (null != _callback) { Debug.Log("Need Show IV, in editor directly invoke callback."); _callback.Invoke(); } return; #endif if (IsNoAllAD || IsRemoveAds) { _callback?.Invoke(); } else { m_sdkInterface.ShowInterstitialAd(_adPos, _callback, _IvType); } } /// /// Show Reward Ad /// /// Name of reward ad placement /// true:reward succ, false: reward failed /// Callback of reward ad show fail public void ShowRewardAd(TKGRVPositionName _adPos, Action _rewardCallback = null, Action _showFailedCallback = null) { #if UNITY_EDITOR || NO_AD if (null != _rewardCallback) { _rewardCallback.Invoke(true); } return; #endif if (IsNoAllAD) { _rewardCallback?.Invoke(true); } else { m_sdkInterface.ShowRewardAd(_adPos, _rewardCallback, _showFailedCallback); } } public void ShowNative(RectTransform pRect, Camera pCam = null, string pAdPos = "") { #if !NO_AD if (!IsNoAllAD && !IsRemoveAds) { m_sdkInterface.ShowNativeAd(pRect, pCam, pAdPos); } #endif } /// /// Is Ready Interstitial /// /// public bool IsReadyInterstitialAd() { return m_sdkInterface.IsReadyInterstitialAd(); } /// /// Is Ready Reward /// /// public bool IsReadyRewardAd() { return m_sdkInterface.IsReadyRewardAd(); } /// /// Is Ready Native Ad /// /// public bool IsReadyNativeAd() { #if NO_AD return false; #else return m_sdkInterface.IsReadyNativeAd(); #endif } /// /// Show banner /// /// bannerAlign public void ShowBanner(TKGBannerAlign _bannerAlign) { #if !NO_AD if (!IsNoAllAD && !IsRemoveAds) { m_sdkInterface.ShowBanner(_bannerAlign); } #endif } /// /// Hide banner /// public void HideBanner() { m_sdkInterface.HideBanner(); } /// /// Remove Native /// public void RemoveNative() { m_sdkInterface.RemoveNativeAd(); } #endregion #region Log Event #region Normal /// /// Log Event /// /// public void LogEvent(string _eventSort) { m_sdkInterface.LogEvent(_eventSort); } /// /// Log Event /// /// /// /// public void LogEvent(string _eventSort, string _key, string _value) { m_sdkInterface.LogEvent(_eventSort, _key, _value); } /// /// Log Event /// /// /// /// /// /// public void LogEvent(string _eventSort, string _key01, string _value01, string _key02, string _value02) { m_sdkInterface.LogEvent(_eventSort, _key01, _value01, _key02, _value02); } /// /// Log Event /// /// /// public void LogEvent(string _eventSort, Dictionary _eventDic = null) { m_sdkInterface.LogEvent(_eventSort, _eventDic); } #endregion #region Level Event /// /// Level start /// /// public void LevelStart(int _level) { LevelStart(_level.ToString()); } /// /// Level start /// /// public void LevelStart(string _level) { m_sdkInterface.LevelStart(_level); } /// /// Level end /// /// /// public bool LevelEnd(int _level, StageResult _stageResult) { return LevelEnd(_level.ToString(), _stageResult); } /// /// Level end,if return false, it means review pops up. /// /// /// public bool LevelEnd(string _level, StageResult _stageResult) { m_sdkInterface.LevelEnd(_level, _stageResult); if (_stageResult == StageResult.StageSucc) { mPassCount++; #if UNITY_IOS//only ios has review popup return !CheckReviewPop(mPassCount); #endif } return true; } private bool CheckReviewPop(int pLevel) { if (mHasPoppedReview) return false; if (mLoginCount == 1 && pLevel == StaticOtherConfig.FirstLoginPopLevel) { Debug.Log("pop first review"); mHasPoppedReview = true; Review(); return true; } else if (mLoginCount > 1 && pLevel == StaticOtherConfig.OtherLoginPopLevel) { Debug.Log("pop other review"); mHasPoppedReview = true; Review(); return true; } return false; } #endregion #region Reward Ad Button Show /// /// Log Reward ad button show /// /// public void LogRewardAdBtnShow(string _pos) { m_sdkInterface.LogRewardAdBtnShow(_pos); } #endregion #region Tracking Event /// /// Log Tracking Event /// /// private void LogTrackingEvent(TrackingEventType _eventType) { //m_sdkInterface.LogTrackingEvent(_eventType); } #endregion #endregion #region Online Config /// /// get config - string /// /// /// public string GetConfigStr(TKGParamKey _key) { return m_sdkInterface.GetConfigStr(_key.ToString()); } /// /// get config - int /// /// /// public int GetConfigInt(TKGParamKey _key) { return m_sdkInterface.GetConfigInt(_key.ToString()); } /// /// get config - bool /// /// /// public bool GetConfigBool(TKGParamKey _key) { return m_sdkInterface.GetConfigBool(_key.ToString()); } #endregion #region Others (common) /// /// review /// public void Review() { m_sdkInterface.Review(); } /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// /// open privacy url /// //public void OpenPrivacyURL() //{ // m_sdkInterface.OpenPrivacyURL(); //} /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop /// /// open user agreement url /// //public void OpenUserTermURL() //{ // m_sdkInterface.OpenUserTermURL(); //} /// /// open more game /// public void OpenMoreGame() { m_sdkInterface.OpenMoreGame(); } /// /// open url by browser /// /// public void OpenUrlByBrowser(string _url) { m_sdkInterface.OpenUrlByBrowser(_url); } public void OpenPolicyPop() { m_sdkInterface.OpenPolicyPop(); } /// /// shake /// /// 0 light,1 medium,2 heavy /// ios 0~1, android any num public void Shake(int _shakeType, float _intensity = 1) { m_sdkInterface.Shake(_shakeType, _intensity); } /// /// share txt /// /// public void Share(string shareText) { m_sdkInterface.ShareTxt(shareText); } /// /// regist APNS /// public void RegistAPNS() { m_sdkInterface.RegistAPNS(); } /// /// shake /// /// notification identifier /// The body of the notification. /// notify after the time interval. format:yyyy-MM-dd HH:mm:ss /// The application badge number. /// The title of the notification. /// The subtitle of the notification. public void RegistNotification(string notiId, string body, string fireDate, int badge = 1, string title = "", string subTitle = "") { m_sdkInterface.RegistNotification(notiId,body,fireDate,badge,title,subTitle); } /// /// remove all notification /// public void RemoveAllNotifications() { m_sdkInterface.RemoveAllNotifications(); } /// /// remove notification by notification identifier /// /// notification identifier public void RemoveNotification(string notiId) { m_sdkInterface.RemoveNotification(notiId); } #endregion #region others /// /// Set user source listener /// only call first can back source /// /// true : origin user,false : not origin user public void SetUserSourceListener(Action _userSourceAction) { m_sdkInterface.SetUserSourceListener(_userSourceAction); } /// /// set common callback /// /// public void SetTKGCommonCallback(Action _commonCallbackAction) { m_sdkInterface.SetTKGCommonCallback(_commonCallbackAction); } #endregion #region h5 public void ShowShareMenu() { ShowShareMenuOption ssmo = new ShowShareMenuOption(); ssmo.menus = new string[] { "shareAppMessage", "shareTimeline" }; WX.ShowShareMenu(ssmo); } public void HideShareMenu() { HideShareMenuOption hsmo = new HideShareMenuOption(); hsmo.menus = new string[] { "shareAppMessage", "shareTimeline" }; WX.HideShareMenu(hsmo); } public void ShareAppMessageOriginImg() { ShareAppMessageOption samo = new ShareAppMessageOption(); samo.title = "高智商玩家的选择,能过20关的都不是一般人"; //samo.imageUrlId = "这是图片路径ID"; //samo.path ="独立分包路径"; WX.ShareAppMessage(samo); } public void ShareAppMessage() { ShareAppMessageOption samo = new ShareAppMessageOption(); samo.title = "靠声音过关的游戏,有点意思~"; samo.imageUrl = "https://touka-pkg.oss-cn-beijing.aliyuncs.com/Temp/chicken_webgl/ShareImg/ShareImg01.png"; //samo.imageUrlId = "这是图片路径ID"; //samo.path ="独立分包路径"; WX.ShareAppMessage(samo); } public void ShowShareImageMenu() { ShowShareImageMenuOption ssimo = new ShowShareImageMenuOption(); ssimo.path = "Share.png"; WX.ShowShareImageMenu(ssimo); } #endregion }