chicken_dy/Assets/TKGSDK/Common/GameInterface/TKGSDKManager.cs

741 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Touka;
using WeChatWASM;
public class TKGSDKManager : TKGSingleton<TKGSDKManager>
{
#region webgl
static string cdnPath = "https://touka-pkg.oss-cn-beijing.aliyuncs.com/Temp/chicken_webgl/"; // Appid: wx1a9ad77bb8f3b66a
//static string cdnPath = "https://touka-artifacts.oss-cn-beijing.aliyuncs.com/WXResource/Chicken_WebGL/"; // Appid: wxb8f1bd49ef160a9a
string[] shareTipsArr = new string[10] { "靠声音过关的游戏,有点意思~",
"你对声音敏感吗?快来试试吧!",
"高智商玩家的选择能过20关的都不是一般人",
"声音解谜,敢来测测你的游戏水平吗?",
"这游戏脑洞太大,快来帮帮我~",
"这游戏有点难度敢不敢来跟我PK一下",
"好友邀请你PK一局点击加入",
"这游戏笑的我肚子痛~",
"这奇葩结局,必须要跟你分享一下!",
"这声音,建议躲在被窝里玩"};
#endregion
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();
}
/// <summary>
/// init sdk
/// </summary>
/// <param name="_initCallback"></param>
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
}
}
/// <summary>
/// Set game focus
/// </summary>
/// <param name="_gameFocusAction"></param>
public void SetGameFocusListener(Action<bool> _gameFocusAction)
{
m_sdkInterface.SetGameFocusListener(_gameFocusAction);
}
// reward click callback
public void SetRewardClickListener(Action _clickCallback = null)
{
m_sdkInterface.SetRewardClickListener(_clickCallback);
}
/// <summary>
/// Get channel
/// </summary>
/// <returns></returns>
public AppChannel GetChannel()
{
return m_sdkInterface.GetChannel();
}
/// <summary>
/// Set log enable
/// </summary>
public void SetLogEnable(bool _enable)
{
StaticOtherConfig.IsDebugLog = _enable;
m_sdkInterface.SetLogEnable(_enable);
}
#region Ads
// 去广告接口调用
public void PurchasedRemoveAds()
{
m_sdkInterface.PurchasedRemoveAds();
}
/// <summary>
/// Show Interstitial Ad
/// </summary>
/// <param name="_adPos"> Name of interstitial ad placement. </param>
/// <param name="_callback"> Callback of interstitial ad close and show interstitial failed</param>
/// <param name="_IvType"> IVADType for distinguish interstitial ads frequency, default use iv1</param>
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);
}
}
/// <summary>
/// Show Reward Ad
/// </summary>
/// <param name="_adPos"> Name of reward ad placement</param>
/// <param name="_rewardCallback"> true:reward succ, false: reward failed</param>
/// <param name="_showFailedCallback">Callback of reward ad show fail</param>
public void ShowRewardAd(TKGRVPositionName _adPos, Action<bool> _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
}
/// <summary>
/// Is Ready Interstitial
/// </summary>
/// <returns></returns>
public bool IsReadyInterstitialAd()
{
return m_sdkInterface.IsReadyInterstitialAd();
}
/// <summary>
/// Is Ready Reward
/// </summary>
/// <returns></returns>
public bool IsReadyRewardAd()
{
return m_sdkInterface.IsReadyRewardAd();
}
/// <summary>
/// Is Ready Native Ad
/// </summary>
/// <returns></returns>
public bool IsReadyNativeAd()
{
#if NO_AD
return false;
#else
return m_sdkInterface.IsReadyNativeAd();
#endif
}
/// <summary>
/// Show banner
/// </summary>
/// <param name="_bannerAlign">bannerAlign</param>
public void ShowBanner(TKGBannerAlign _bannerAlign)
{
#if !NO_AD
if (!IsNoAllAD && !IsRemoveAds)
{
m_sdkInterface.ShowBanner(_bannerAlign);
}
#endif
}
/// <summary>
/// Hide banner
/// </summary>
public void HideBanner()
{
m_sdkInterface.HideBanner();
}
/// <summary>
/// Remove Native
/// </summary>
public void RemoveNative()
{
m_sdkInterface.RemoveNativeAd();
}
#endregion
#region Log Event
#region Normal
/// <summary>
/// Log Event
/// </summary>
/// <param name="_eventSort"></param>
public void LogEvent(string _eventSort)
{
m_sdkInterface.LogEvent(_eventSort);
}
/// <summary>
/// Log Event
/// </summary>
/// <param name="_eventSort"></param>
/// <param name="_key"></param>
/// <param name="_value"></param>
public void LogEvent(string _eventSort, string _key, string _value)
{
m_sdkInterface.LogEvent(_eventSort, _key, _value);
}
/// <summary>
/// Log Event
/// </summary>
/// <param name="_eventSort"></param>
/// <param name="_key01"></param>
/// <param name="_value01"></param>
/// <param name="_key02"></param>
/// <param name="_value02"></param>
public void LogEvent(string _eventSort, string _key01, string _value01, string _key02, string _value02)
{
m_sdkInterface.LogEvent(_eventSort, _key01, _value01, _key02, _value02);
}
/// <summary>
/// Log Event
/// </summary>
/// <param name="_eventSort"></param>
/// <param name="_eventDic"></param>
public void LogEvent(string _eventSort, Dictionary<string, string> _eventDic = null)
{
m_sdkInterface.LogEvent(_eventSort, _eventDic);
}
#endregion
#region Level Event
/// <summary>
/// Level start
/// </summary>
/// <param name="_level"></param>
public void LevelStart(int _level)
{
LevelStart(_level.ToString());
}
/// <summary>
/// Level start
/// </summary>
/// <param name="_level"></param>
public void LevelStart(string _level)
{
m_sdkInterface.LevelStart(_level);
}
/// <summary>
/// Level end
/// </summary>
/// <param name="_level"></param>
/// <param name="_win"></param>
public bool LevelEnd(int _level, StageResult _stageResult)
{
return LevelEnd(_level.ToString(), _stageResult);
}
/// <summary>
/// Level end,if return false, it means review pops up.
/// </summary>
/// <param name="_level"></param>
/// <param name="_win"></param>
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
/// <summary>
/// Log Reward ad button show
/// </summary>
/// <param name="_pos"></param>
public void LogRewardAdBtnShow(string _pos)
{
m_sdkInterface.LogRewardAdBtnShow(_pos);
}
#endregion
#region Tracking Event
/// <summary>
/// Log Tracking Event
/// </summary>
/// <param name="_eventType"></param>
private void LogTrackingEvent(TrackingEventType _eventType)
{
//m_sdkInterface.LogTrackingEvent(_eventType);
}
#endregion
#endregion
#region Online Config
/// <summary>
/// get config - string
/// </summary>
/// <param name="_key"></param>
/// <returns></returns>
public string GetConfigStr(TKGParamKey _key)
{
return m_sdkInterface.GetConfigStr(_key.ToString());
}
/// <summary>
/// get config - int
/// </summary>
/// <param name="_key"></param>
/// <returns></returns>
public int GetConfigInt(TKGParamKey _key)
{
return m_sdkInterface.GetConfigInt(_key.ToString());
}
/// <summary>
/// get config - bool
/// </summary>
/// <param name="_key"></param>
/// <returns></returns>
public bool GetConfigBool(TKGParamKey _key)
{
return m_sdkInterface.GetConfigBool(_key.ToString());
}
#endregion
#region Others (common)
/// <summary>
/// review
/// </summary>
public void Review()
{
m_sdkInterface.Review();
}
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// <summary>
/// open privacy url
/// </summary>
//public void OpenPrivacyURL()
//{
// m_sdkInterface.OpenPrivacyURL();
//}
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// 使用 OpenPolicyPop 接口 === Use OpenPolicyPop
/// <summary>
/// open user agreement url
/// </summary>
//public void OpenUserTermURL()
//{
// m_sdkInterface.OpenUserTermURL();
//}
/// <summary>
/// open more game
/// </summary>
public void OpenMoreGame()
{
m_sdkInterface.OpenMoreGame();
}
/// <summary>
/// open url by browser
/// </summary>
/// <param name="_url"></param>
public void OpenUrlByBrowser(string _url)
{
m_sdkInterface.OpenUrlByBrowser(_url);
}
public void OpenPolicyPop()
{
m_sdkInterface.OpenPolicyPop();
}
/// <summary>
/// shake
/// </summary>
/// <param name="_shakeType">0 light,1 medium,2 heavy</param>
/// <param name="_intensity">ios 0~1, android any num</param>
public void Shake(int _shakeType, float _intensity = 1)
{
m_sdkInterface.Shake(_shakeType, _intensity);
}
/// <summary>
/// share txt
/// </summary>
/// <param name="_shareTxt"></param>
public void Share(string shareText)
{
m_sdkInterface.ShareTxt(shareText);
}
/// <summary>
/// regist APNS
/// </summary>
public void RegistAPNS()
{
m_sdkInterface.RegistAPNS();
}
/// <summary>
/// shake
/// </summary>
/// <param name="notiName">notification identifier</param>
/// <param name="body">The body of the notification.</param>
/// <param name="fireDate">notify after the time interval. format:yyyy-MM-dd HH:mm:ss </param>
/// <param name="badge">The application badge number.</param>
/// <param name="title">The title of the notification.</param>
/// <param name="subTitle">The subtitle of the notification.</param>
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);
}
/// <summary>
/// remove all notification
/// </summary>
public void RemoveAllNotifications()
{
m_sdkInterface.RemoveAllNotifications();
}
/// <summary>
/// remove notification by notification identifier
/// </summary>
/// <param name="notiId">notification identifier</param>
public void RemoveNotification(string notiId)
{
m_sdkInterface.RemoveNotification(notiId);
}
#endregion
#region others
/// <summary>
/// Set user source listener
/// only call first can back source
/// </summary>
/// <param name="_userSourceAction"> true : origin userfalse : not origin user </param>
public void SetUserSourceListener(Action<bool, string> _userSourceAction)
{
m_sdkInterface.SetUserSourceListener(_userSourceAction);
}
/// <summary>
/// set common callback
/// </summary>
/// <param name="_commonCallbackAction"></param>
public void SetTKGCommonCallback(Action<CommonCallbackCode, string> _commonCallbackAction)
{
m_sdkInterface.SetTKGCommonCallback(_commonCallbackAction);
}
#endregion
#region h5
public enum WayOfReward
{
None,
Share,
RewardAd,
}
public enum ShareType
{
None,
ShareAppOriginImg,
ShareAppFixedImg,
}
public WayOfReward GetRewardWay()
{
return WayOfReward.Share;
}
public void ShareShareAppMessageByType(ShareType _shareType = ShareType.ShareAppOriginImg)
{
Debug.Log("ShareShareAppMessageByType, type : " + _shareType);
//for(int i = 0; i < 30; i++)
//{
// int tipIndex = GetRandomIndex();
// string title = GetRandomShareTips(tipIndex);
// string imgurl = string.Format("{0}ShareImg/ShareImg{1}.png", cdnPath, (tipIndex + 1));
// Debug.Log("title : " + title + " , imgUrl : " + imgurl);
//}
int tipIndex = GetRandomIndex();
string img2 = string.Format("{0}ShareImg/ShareImg{1}.png", cdnPath, (tipIndex + 1));
Debug.Log("img2 : " + img2);
if (_shareType == ShareType.ShareAppOriginImg)
{
#if !UNITY_EDITOR
ShareAppMessageOriginImg();
#endif
}else if(_shareType == ShareType.ShareAppFixedImg)
{
#if !UNITY_EDITOR
ShareAppMessageFixedImg();
#endif
}
}
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);
}
/// <summary>
/// 直接调用分享 分享当前界面截图
/// </summary>
public void ShareAppMessageOriginImg()
{
ShareAppMessageOption samo = new ShareAppMessageOption();
int tipIndex = GetRandomIndex();
samo.title = GetRandomShareTips(tipIndex);
//samo.imageUrlId = "这是图片路径ID";
//samo.path ="独立分包路径";
WX.ShareAppMessage(samo);
}
/// <summary>
/// 分享随机大图
/// </summary>
public void ShareAppMessageFixedImg()
{
ShareAppMessageOption samo = new ShareAppMessageOption();
int tipIndex = GetRandomIndex();
samo.title = GetRandomShareTips(tipIndex);
string img2 = string.Format("{0}ShareImg/ShareImg{1}.png", cdnPath, (tipIndex+1));
samo.imageUrl = img2; // string.Format("{0}/ShareImg/ShareImg{1}.png", cdnPath, (tipIndex+1));
//samo.imageUrlId = "这是图片路径ID";
//samo.path ="独立分包路径";
WX.ShareAppMessage(samo);
}
/// <summary>
/// 随机一个分享文本
/// </summary>
/// <returns></returns>
private string GetRandomShareTips(int _index)
{
return shareTipsArr[_index];
}
/// <summary>
/// 随机一个数 0-9
/// </summary>
/// <returns></returns>
private int GetRandomIndex()
{
int randomIndex = UnityEngine.Random.Range(0, shareTipsArr.Length);
return randomIndex;
}
public void ShowShareImageMenu()
{
ShowShareImageMenuOption ssimo = new ShowShareImageMenuOption();
ssimo.path = "Share.png";
WX.ShowShareImageMenu(ssimo);
}
#endregion
}