// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-12-07 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 平台 /// partial class KPlatform : MonoBehaviour { public class ShareInfo { public string title; public string imgUrl; public string imgUrlId; public string data; } [System.Serializable] public class InviteInfo { public string userId; public string type; public override string ToString() { return $"userId={userId}&type={type}"; } } #region Field private IPlatformWapper _platform; #endregion #region Property /// /// /// public IPlatformWapper platformWapper => _platform; /// /// /// public string platformName { get { return _platform?.name; } } /// /// /// public string appId { get { return _platform?.appId; } } public string envVersion { get { #if SDK_WECHAT_WASM if (_platform is KPlatform.WechatPlatform wp) { return wp.envVersion; } #endif return ""; } } #endregion #region Method #endregion #region Unity && Instance public static event System.Action ApplicationFocusEvent; public static KPlatform Instance; private void Awake() { Instance = this; Init(); } // Update is called once per frame private void Update() { #if UNITY_ANDROID if (Input.GetKeyDown(KeyCode.Escape)) { UI.MessageBox.ShowMessage("退出游戏", "确定要离开吗", () => Application.Quit(), () => { }); } #endif } private void OnApplicationFocus(bool focus) { ApplicationFocusEvent?.Invoke(focus); } private void OnApplicationQuit() { } #endregion #region 初始化 /// /// /// public bool initSuccess { get; private set; } /// /// /// public void Init() { #if UNITY_EDITOR Invoke(nameof(OnInitSuccess), 0.01f); #elif SDK_OHAYOO _platform = this.gameObject.AddComponent(); #elif SDK_ONESDK _platform = this.gameObject.AddComponent(); #elif SDK_WECHAT_WASM _platform = this.gameObject.AddComponent(); #else Invoke(nameof(OnInitSuccess), 0.01f); #endif } /// /// /// public void OnInitSuccess() { initSuccess = true; if (_platform != null) { Debug.Log("OnInitSuccess"); //_platform.NormalLogin(); LoadRewardAd(2, "init"); KStatistics.Instance.ReportEvent_GameInit(1, "sdk", 0, 0); } else { this.OnLoginSuccess(SystemInfo.deviceUniqueIdentifier, "", 0, 1); } } #endregion #region 信息 /// /// /// public string platform { get { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { return wechatPlatform.platform; } #endif return Application.platform.ToString(); } } private string _deviceId = string.Empty; /// Gets or sets the open identifier. public string deviceId { get { return !string.IsNullOrEmpty(_deviceId) ? _deviceId : SystemInfo.deviceUniqueIdentifier; } set { _deviceId = value; } } /// Gets or sets the channel. public int channel { get { #if UNITY_EDITOR return 25; #elif SDK_WECHAT_WASM return 3; #else return 26; #endif } } /// /// /// public string subChanel { get { #if UNITY_IOS return "ios"; #elif UNITY_ANDROID switch (Application.identifier) { case "com.kunpo.litaibai.android.ohayoo": return "aos"; case "com.kunpo.litaibai.android.ohayoo.vapp": return "vapp"; case "com.kunpo.litaibai.android.ssjj": return "4399"; case "com.kunpo.litaibai.android.qqers": return "7723"; case "com.kunpo.litaibai.android.cczs": return "cczs"; case "com.kunpo.litaibai.android.uc": return "uc"; case "com.kunpo.litaibai.android.mi": return "mi"; case "com.kunpo.litaibai.android.vivo": return "vivo"; case "com.kunpo.litaibai.android.nearme.gamecenter": return "oppo"; case "com.kunpo.litaibai.android.huawei": return "huawei"; default: return ""; } #else return ""; #endif } } public int safeAreaTop { get { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { return wechatPlatform.safeAreaTop * 2; } #endif return (int)Screen.safeArea.y; } } InviteInfo _inviteInfo; public InviteInfo inviteInfo { get { return _inviteInfo; } set { _inviteInfo = value; if (ServerProxy.Instance && ServerProxy.Instance.isLoginSuccess) { ServerProxy.Instance.Invite(inviteInfo.userId, (error, message) => { if (error != ErrorCode.NETWORK_ERROR) _inviteInfo = null; }); } } } public int benchmarkLevel { get { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { return wechatPlatform.benchmarkLevel; } #endif return 30; } } #endregion #region 登录 /// /// 账号登录方式 /// Login type. /// public enum LoginType { /// /// 通过游客方式登录 /// The login type VISITOR. /// LOGIN_TYPE_VISITOR = 1, /// /// 通过头条登录 /// The login type toutiao. /// LOGIN_TYPE_TT = 2, /// /// 通过抖音授权登录 /// The login type douyin. /// LOGIN_TYPE_DY = 3, /// /// 通过手机号登录 /// The login type phone. /// LOGIN_TYPE_PHONE = 4, /// /// 通过苹果方式登录 /// The login type APPLE. /// LOGIN_TYPE_APPLE = 5 } /// /// /// public LoginType loginType { get; set; } /// /// /// public LoginType lastLoginType { get; set; } /// /// ==1 成功 2失败 3 cancel /// public int loginStatus { get; private set; } private string _openId = string.Empty; /// Gets or sets the open identifier. public string openId { get => _openId; private set { _openId = value; } } private string _openToken = string.Empty; /// Gets or sets the open token. public string openToken { get => _openToken; private set { _openToken = value; } } /// /// /// public string loginTypeText { get { #if SDK_WECHAT_WASM return "点击复制"; #else switch (loginType) { case LoginType.LOGIN_TYPE_VISITOR: return "游客"; case LoginType.LOGIN_TYPE_TT: return "头条"; case LoginType.LOGIN_TYPE_DY: return "抖音"; case LoginType.LOGIN_TYPE_PHONE: return "手机号"; case LoginType.LOGIN_TYPE_APPLE: return "苹果"; } return "游客"; #endif } } /// /// 正常登陆 NormalLogin = 1,// 账号绑定 AccountBind = 2,// 切换账号 SwitchAccount = 3, /// public int loginInvokeMode { get; private set; } public bool supportSwitchAccount { get { #if SDK_WECHAT_WASM || SDK_ONESDK return false; #else return true; #endif } } /// /// /// public void NormalLogin() { loginStatus = 0; if (_platform != null) _platform.NormalLogin(); } /// /// /// public void SwitchAccount() { if (_platform != null) _platform.SwitchAccount(); } /// /// /// public void VisitorBindAccount() { if (_platform != null) _platform.VisitorBindAccount(); } /// /// /// /// public bool IsVisitor() { if (_platform != null) return _platform.IsVisitor(); return true; } /// /// 注销帐号 /// public void Logoff(System.Action onSuccess, System.Action onFail) { if (_platform != null) _platform.Logoff(onSuccess, onFail); else onSuccess?.Invoke(""); } /// /// /// /// /// /// 通过游客方式登录 LOGIN_TYPE_VISITOR = 1, 通过OneSdk方式登录 = 100 /// // 正常登陆 NormalLogin = 1,// 账号绑定 AccountBind = 2,// 切换账号 SwitchAccount = 3, public void OnLoginSuccess(string openId, string openToken, int loginType, int loginInvokeMode) { this.openId = openId; this.openToken = openToken; this.lastLoginType = this.loginType; this.loginType = (LoginType)loginType; this.loginInvokeMode = loginInvokeMode; loginStatus = 1; ServerProxy.Instance.OnSDKLogin(openId, openToken, loginType, loginInvokeMode); ArchiveProxy.Instance.OnSDKLogin(openId, openToken, loginType, loginInvokeMode); KStatistics.Instance.ReportEvent_GameLogin(openId, loginType.ToString()); } /// /// /// /// /// /// // 正常登录 Login = 1, // 账号绑定 AccountBind = 2,// 切换账号 SwitchAccount = 3, public void OnLoginFailure(int error, string errorMsg, int apiLoginType) { loginStatus = 2; ServerProxy.Instance.OnSDKLoginFail(error, errorMsg, apiLoginType); } /// /// /// /// public void OnLoginCancel(string errorMsg) { loginStatus = 3; } #endregion #region 广告 private int _adDelay; private bool _autoPlayAd; private bool _rewardVerify; float _lastPlayAd = -20f; string _adType = ""; string _adScene = ""; string _stageId = ""; Callback2 _adCallback; /// /// /// /// /// /// public void PlayRewardAd(string type, string scene, Callback2 callback) { PlayRewardAd(type, scene, "", callback); } public void PlayRewardAd(string adType, string adScene, string stageId, Callback2 callback) { #if DEBUG_MY && !UNITY_WEBGL if (KGMOptions.BlockAd) { Debug.Log(adScene + " 广告播放成功"); MissionProxy.Instance.OnEvent(MissionProxy.观看视频次数); GlobalNotifier.PostNotification(GlobalDefine.EVENT_PLAY_AD_SUCCESS); callback?.Invoke(0, ""); return; } #endif if (_platform != null) { SetAdParams(adType, adScene, stageId, callback); PlayRewardAd(); } else { Debug.Log(adScene + "广告播放成功"); MissionProxy.Instance.OnEvent(MissionProxy.观看视频次数); GlobalNotifier.PostNotification(GlobalDefine.EVENT_PLAY_AD_SUCCESS, "广告播放成功"); callback?.Invoke(0, ""); } } /// /// /// /// /// /// /// private void SetAdParams(string adType, string adScene, string stageId, Callback2 callback) { _adType = adType; _adScene = adScene; _stageId = stageId; _adCallback = callback; } /// /// /// private void ResetAdParams() { _adType = ""; _adScene = ""; _stageId = ""; _adCallback = null; } private void PauseGame(bool pause) { if (pause) { Launch.PauseGame(true); KUIWindow.OpenWindow(); } else { Launch.PauseGame(false); KUIWindow.CloseWindow(); } } private void PlayRewardAd() { if (Time.realtimeSinceStartup - _lastPlayAd < 20f) { GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "请勿短时间内频繁观看广告,请稍后再看"); return; } #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { // step3. 广告曝光上报 // 与广告关联,将以下代码片段埋入如激励广告的曝光(广告组件中的show事件) // step3. 广告曝光上报 wechatPlatform.ReportUserBehaviorBranchAnalytics("BCBgAAoXHx5d1nii-BqWRt", _adScene, 1); } #endif int showResult = _platform.ShowRewardAd(_adScene); if (showResult == 0) { _autoPlayAd = false; _lastPlayAd = Time.realtimeSinceStartup; PauseGame(true); } else if (showResult == 2)//没有加载广告 请先加载广告 { _autoPlayAd = true; LoadRewardAd(0, "play"); GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "加载广告,请耐心等待."); } else if (showResult == 1) { _autoPlayAd = true; GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "广告正在加载中,请耐心等待."); } } private void LoadRewardAd(int delay, string source) { StartCoroutine(LoadAdCO(delay)); } private IEnumerator LoadAdCO(int delay) { if (delay > 0) { yield return new WaitForSeconds(delay); } _platform?.LoadRewardVideoAd_V(); } public void OnRewardedAdShow() { KStatistics.Instance.ReportEventShowAd("reward", _adType, _adScene, _stageId); } public void OnRewardClick() { KStatistics.Instance.ReportEventClickAd("reward", _adScene, _stageId); } private void OnRewardVerify(bool rewardVerify, float rewardAmount, string rewardName) { _rewardVerify = rewardVerify; } public void OnVideoComplete() { } public void OnRewardedAdClosed() { PauseGame(false); if (_rewardVerify) { _adCallback?.Invoke(0, null); MissionProxy.Instance.OnEvent(MissionProxy.观看视频次数); GlobalNotifier.PostNotification(GlobalDefine.EVENT_PLAY_AD_SUCCESS, "广告播放成功"); KStatistics.Instance.ReportEventShowEndAd("reward", _adType, _adScene, "success", _stageId); #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { // step3. 广告曝光上报 // 与广告关联,将以下代码片段埋入如激励广告的曝光(广告组件中的show事件) // step3. 广告曝光上报 wechatPlatform.ReportUserBehaviorBranchAnalytics("BCBgAAoXHx5d1nii-BqWRt", _adScene, 2); } #endif } else { _adCallback?.Invoke(-1, "广告播放失败,无法获得奖励"); KStatistics.Instance.ReportEventShowEndAd("reward", _adType, _adScene, "failure", _stageId); } _rewardVerify = false; ResetAdParams(); LoadRewardAd(2, "ad_closed"); } public void OnVideoError() { _adCallback?.Invoke(1, "广告播放错误,请检查网络连接"); KStatistics.Instance.ReportEventShowEndAd("reward", _adType, _adScene, "error", _stageId); ResetAdParams(); GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "广告播放错误,请检查网络连接"); GlobalNotifier.PostNotification(GlobalDefine.EVENT_PLAY_AD_ERROR, "广告播放错误,请检查网络连接"); } public void OnSkippedVideo() { if (_rewardVerify) return; _adCallback?.Invoke(2, "跳过广告播放,无法获得奖励"); KStatistics.Instance.ReportEventShowEndAd("reward", _adType, _adScene, "skip", _stageId); ResetAdParams(); GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "跳过广告播放,无法获得奖励"); GlobalNotifier.PostNotification(GlobalDefine.EVENT_PLAY_AD_SKIP, "跳过广告播放,无法获得奖励"); } /// /// /// public void OnRewardVideoAdLoad() { _adDelay = 0; if (_autoPlayAd) { _autoPlayAd = false; if (_platform?.ShowRewardAd(_adScene) == 0) { PauseGame(true); } } GlobalNotifier.PostNotification(GlobalDefine.EVENT_AD_LOAD_SUCCESS, "广告加载成功"); } /// /// // 广告加载失败,对应参数为,错误码 + 错误信息 /// public void OnRewardVideoAdLoadError(int error, string message) { _adDelay += 30; LoadRewardAd(_adDelay, "ad_error"); GlobalNotifier.PostNotification(GlobalDefine.EVENT_AD_LOAD_SUCCESS, "广告加载失败"); } #endregion #region 兑换 /// /// /// /// public void Redeem(string redeemCode) { if (!string.IsNullOrEmpty(redeemCode)) { _platform?.Redeem(redeemCode); } } /// /// /// /// private void OnRedeemSuccess(string json) { if (!string.IsNullOrEmpty(json)) { var packet = new GamePacket { id = 90, data = json }; GlobalNotifier.PostNotification(GlobalDefine.EVENT_NETWORK_PACKET, packet, "sdk"); } } /// /// /// /// /// private void OnRedeemError(int code, string error) { GlobalNotifier.PostNotification(GlobalDefine.EVENT_SHOW_TOAST, error); } #endregion #region Ab Test public string GetAbConfig(string key, string defaultValue) { #if (UNITY_EDITOR || DEBUG_MY) return KGMOptions.AB_Version; #else if (initSuccess && _platform != null) { var result = _platform.GetAbConfig(key, defaultValue); return result; } return null; #endif } /// /// 默认 /// /// /// 默认 null /// public bool IsAbConfig(string key, string value) { #if !UNITY_WEBGL && (UNITY_EDITOR || DEBUG_MY) return KGMOptions.AB_Version == value; #elif UNITY_ANDROID //Debug.Log("key" + _platform.GetAbConfig(key, "{\"key\":\"0\"}")); return value == _platform.GetAbConfig(key, "{\"key\":\"0\"}"); #elif UNITY_IOS var result = _platform.GetAbConfig(key, "0"); if (string.IsNullOrEmpty(value)) return result == "0"; return result == value; #else return false; #endif } #endregion #region 统计 /// /// /// /// /// public void SetEventCommonHeader(string key, string value) { _platform?.SetEventCommonHeader(key, value); } /// /// /// /// /// public void ReportEvent(string label, string paramJson) { _platform?.ReportEvent(label, paramJson); } #endregion #region 支付 public bool QueryPay() { return false; #if SDK_OHAYOO return false; #elif SDK_WECHAT_WASM #if UNITY_EDITOR return true; #else if (_platform is WechatPlatform wechatPlatform) { return wechatPlatform.QueryPay(); } return false; #endif #else return true; #endif } public void Pay(object payInfo, Callback2 callback) { #if UNITY_EDITOR callback?.Invoke(0, "测试环境 支付成功"); #elif SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.Pay(payInfo, callback); } else { callback?.Invoke(0, "测试环境 支付成功"); } #endif } #endregion #region 分享 public void Share(ShareInfo shareInfo, Callback2 callback) { #if UNITY_EDITOR callback?.Invoke(0, "测试环境,分享成功"); #elif SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.Share(shareInfo, callback); } #endif } public void JumpToApp(string appId, string extraData) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.JumpToApp(appId, extraData); } #endif } #endregion #region 客服 public void OpenCustomerServiceConversation() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.OpenCustomerServiceConversation(); } #else Application.OpenURL("https://docs.qq.com/form/page/DTXpuUmpaQkdsU1RW#/fill"); #endif } public void OpenFeedback() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.OpenCustomerServiceConversation(); } #else Application.OpenURL("https://docs.qq.com/form/page/DTXpuUmpaQkdsU1RW#/fill"); #endif } #endregion #region 微信开放域 public void ShowOpenData(Texture texture, RectInt rect) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.ShowOpenData(texture, rect.x, rect.y, rect.width, rect.height); } #endif } public void HideOpenData() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.HideOpenData(); } #endif } public void SetUserCloundStorage(IDictionary kvData, Callback2 callback) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.SetUserCloudStorage(kvData, callback); } #endif } public void RemoveUserCloudStorage(ICollection keys, Callback2 callback) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.RemoveUserCloudStorage(keys, callback); } #endif } #endregion #region 交互 /// /// 显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框 /// /// /// 是否显示透明蒙层,防止触摸穿透 public void ShowLoading(string title, bool mask) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.ShowLoading(title, mask); } #endif } /// /// 隐藏 loading 提示框 /// public void HideLoading() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.HideLoading(); } #endif } /// /// /// public void ShowToast() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.ShowToast(); } #endif } public void HideToast() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.HideToast(); } #endif } public void ShowModal() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.ShowModal(); } #endif } public void CreateGameClubButton() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.CreateGameClubButton(); } #endif } #endregion #region 其它 /// /// 应用内引导评分 /// public void InAppRating() { if (_platform != null) _platform.InAppRating(); } /// /// 验证敏感词. /// /// 输入的敏感词信息. /// 返回的敏感词结果. public void VerifySensitiveWords(string sensitiveWords, System.Action verifyResult) { if (_platform != null) _platform.VerifySensitiveWords(sensitiveWords, verifyResult); else { verifyResult?.Invoke(0); } } public bool IsOppoLeisureChannel() { #if SDK_ONESDK if (_platform is OneSDKPlatform oneSDK) { return oneSDK.IsOppoLeisureChannel(); } #endif return false; } public void OppoLeisureJump() { #if SDK_ONESDK if (_platform is OneSDKPlatform oneSDK) { oneSDK.OppoLeisureJump(); } #endif } public void OpenWebView(string url) { if (_platform != null) { _platform.OpenWebView(url); } else { Application.OpenURL(url); } } /// /// /// /// /// show toast public bool SetClipboardData(string data) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.SetClipboardData(data); } return false; #else GlobalUtils.CopyToClipboard(data); return true; #endif } public void GetClipboardData(Callback3 callback) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.GetClipboardData(callback); } #else callback?.Invoke(0, "", GlobalUtils.GetClipboard()); #endif } /// /// heavy、medium、light /// public void VibrateShort(int type) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.VibrateShort(type); } #else Handheld.Vibrate(); #endif } /// /// /// public void VibrateLong() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.VibrateLong(); } #else Handheld.Vibrate(); #endif } public void PlayMusic(string music, float volume) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.PlayMusic(music, volume); } #endif } public void PauseMusic(bool pause) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.PauseMusic(pause); } #endif } public void PreLoadAudio(string[] audios) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.PreLoadShortAudio(audios); } #endif } public void PlayAudio(string audio, float volume) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.PlayShortAudio(audio, volume); } #endif } /// /// /// public void GetUserNickName(Callback2 callback) { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.GetUserNickName(callback); } #endif } public void ClearCache() { #if SDK_WECHAT_WASM if (_platform is WechatPlatform wechatPlatform) { wechatPlatform.ClearCache(); } #endif } #endregion #region 隐私政策 public bool supportPrivacyProtocol { get { #if SDK_WECHAT_WASM || SDK_ONESDK return false; #else return true; #endif } } /// /// /// public void OpenUserProtocol() { #if !SDK_WECHAT_WASM _platform?.OpenUserProtocol(); #endif //Application.OpenURL("https://kunpo.cc/tos"); } /// /// /// public void OpenPrivacyProtocol() { #if !SDK_WECHAT_WASM Kunpo.Privacy.PrivacyProxy.OpenPrivacyTerms(); #endif //Application.OpenURL("https://kunpo.cc/privacy"); //_platform?.OpenPrivacyProtocol(); } /// /// /// public void OpenIdentifyProtocol() { _platform?.OpenIdentifyProtocol(); } public void OpenLogoffProtocol() { #if !SDK_WECHAT_WASM Kunpo.Privacy.PrivacyProxy.OpenLogoff(); #endif } #endregion } }