196 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			196 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | ||
| using System.Collections.Generic;
 | ||
| using System.Linq;
 | ||
| using UnityEngine;
 | ||
| using UnityEngine.UI;
 | ||
| using UnityEngine.SceneManagement;
 | ||
| using Touka.GameLogic;
 | ||
| 
 | ||
| namespace Touka
 | ||
| {
 | ||
|     public class ToukaInterstitialTimer : ToukaSingletonMonoBehaviour<ToukaInterstitialTimer>
 | ||
|     {
 | ||
|         private int IV1Times = 0;
 | ||
|         private int IV2Times = 0;
 | ||
| 
 | ||
|         #region 插屏时间
 | ||
|         public float startedTime = 0;
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 是否达到了可展示插屏的条件
 | ||
|         ///
 | ||
|         /// 条件:前N次触达插屏点不展示插屏。达到前N次后,第N+1次展示插屏,后续每隔M次展示一次插屏。插屏之间间隔不少于15s。
 | ||
|         ///
 | ||
|         /// 1. 前N次触达不弹出插屏;  在线参数-IV1_PreTimesN_OnlineParam
 | ||
|         /// 2. 后续每隔M关弹出一次插屏;    在线参数-IV1_PerM_OnlineParam
 | ||
|         /// 3. 插屏与上次间隔时间不少于15s; 在线参数-IV1_MinTimeL_OnlineParam
 | ||
|         /// 三个条件均允许在线参数控制。
 | ||
|         ///
 | ||
|         /// *IVType 区分不同点位插屏的条件控制。如果整个游戏,只有一种条件控制,那都用IV1就可以。如果某些点位,要有第二种条件控制,那对应的点位,传IV2就可以。
 | ||
|         /// 目前通用封装,只支持两种不同的条件控制。逻辑相同,只是3个条件参数,用不同的在线参数控制。如需多个条件,请自行进行扩展
 | ||
|         /// </summary>
 | ||
|         /// <param name="_ivType"> 根据传入 IVType 区分不同插屏条件控制 </param>
 | ||
|         /// <returns></returns>
 | ||
|         public bool CanShow(ToukaSDKManager.IVType _ivType, bool _justCheck = false)
 | ||
|         {
 | ||
|             // 【1】前N次不展示插屏
 | ||
|             if (!CheckIfReachFirstNTimes(_ivType))
 | ||
|             {
 | ||
|                 Debug.Log("[ToukaInterstitialTimer]Check 「前N次不展示插屏」 条件是否满足 : False,直接返回。不播插屏。");
 | ||
|                 return false;
 | ||
|             }
 | ||
| 
 | ||
|             // 达到N次后,第N+1次,直接展示插屏
 | ||
|             bool canShowDirectly = false;
 | ||
|             if (_ivType == ToukaSDKManager.IVType.IV1)
 | ||
|             {
 | ||
|                 if (ToukaUtils.IfFirstCheckPlayerPrefs(StaticStringsPlayerPrefs.TKInner_HasFirstShowIV1))
 | ||
|                 {
 | ||
|                     canShowDirectly = true;
 | ||
|                 }
 | ||
|             }
 | ||
|             else if (_ivType == ToukaSDKManager.IVType.IV2)
 | ||
|             {
 | ||
|                 if (ToukaUtils.IfFirstCheckPlayerPrefs(StaticStringsPlayerPrefs.TKInner_HasFirstShowIV2))
 | ||
|                 {
 | ||
|                     canShowDirectly = true;
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             if (!canShowDirectly)         // 非第N+1次,不能直接展示广告; 【2】要判断是否达到了间隔次数;
 | ||
|             {
 | ||
|                 int currTimes = 0;      // 当前达到的间隔次数
 | ||
|                 int needTimes = 0;      // 需要达到的间隔次数
 | ||
| 
 | ||
|                 if (_ivType == ToukaSDKManager.IVType.IV1)      // IV1 需要达到的间隔次数
 | ||
|                 {
 | ||
|                     if (_justCheck)
 | ||
|                     {
 | ||
|                         currTimes = IV1Times + 1;
 | ||
|                     }
 | ||
|                     else
 | ||
|                     {
 | ||
|                         currTimes = ++IV1Times;
 | ||
|                     }
 | ||
|                     needTimes = TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV1_PerM.ToString());
 | ||
|                 }
 | ||
|                 else if (_ivType == ToukaSDKManager.IVType.IV2)     // IV2需要达到的间隔次数
 | ||
|                 {
 | ||
|                     if (_justCheck)
 | ||
|                     {
 | ||
|                         currTimes = IV2Times + 1;
 | ||
|                     }
 | ||
|                     else
 | ||
|                     {
 | ||
|                         currTimes = ++IV2Times;
 | ||
|                     }
 | ||
|                     needTimes = TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV2_PerM.ToString());
 | ||
|                 }
 | ||
| 
 | ||
|                 if (currTimes < needTimes)   // 没有达到间隔次数,直接返回
 | ||
|                 {
 | ||
|                     Debug.Log("[ToukaInterstitialTimer] Check 「2次插屏间隔次数」 条件是否满足:False,直接返回。不播插屏。当前次数:" + currTimes + ", 需要达到次数:" + needTimes);
 | ||
|                     return false;
 | ||
|                 }
 | ||
|                 else        // 达到间隔次数了,清空间隔次数,可以直接播插屏广告
 | ||
|                 {
 | ||
|                     if (!_justCheck)
 | ||
|                     {
 | ||
|                         if (_ivType == ToukaSDKManager.IVType.IV1)
 | ||
|                         {
 | ||
|                             IV1Times = 0;
 | ||
|                         }
 | ||
|                         else if (_ivType == ToukaSDKManager.IVType.IV2)
 | ||
|                         {
 | ||
|                             IV2Times = 0;
 | ||
|                         }
 | ||
|                         Debug.Log("[ToukaInterstitialTimer]达到间隔次数,可进行最小时间间隔检测。当前次数:" + currTimes + ", 需要达到次数:" + needTimes);
 | ||
|                     }
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             // 【3】判断是否达到最小时间间隔
 | ||
|             float ivIntervalTime = TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV1_MinTimeL.ToString());      // IV1 最小时间间隔
 | ||
|             if (_ivType == ToukaSDKManager.IVType.IV2)
 | ||
|             {
 | ||
|                 ivIntervalTime = TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV2_MinTimeL.ToString());         // IV2 最小时间间隔
 | ||
|             }
 | ||
| 
 | ||
|             bool canShowFlg = Time.time - startedTime > ivIntervalTime;         // 达到最小间隔时间,可以展示插屏;否则不可以
 | ||
|             Debug.Log("[ToukaInterstitialTimer] Check 「2次插屏最小间隔时间」 条件是否满足:" + canShowFlg);
 | ||
|             Debug.Log("[ToukaInterstitialTimer] 判断插屏间隔时间 : " + (Time.time - startedTime) + " , 当前时间:" + Time.time + " , 上次开始时间 :" + startedTime + " ,所需要的间隔时间: " + ivIntervalTime);
 | ||
| 
 | ||
|             // 可展示插屏且非纯检查,清空累计次数
 | ||
|             if (canShowFlg && !_justCheck)
 | ||
|             {
 | ||
|                 if (_ivType == ToukaSDKManager.IVType.IV1)
 | ||
|                 {
 | ||
|                     IV1Times = 0;
 | ||
|                 }
 | ||
|                 else if (_ivType == ToukaSDKManager.IVType.IV2)
 | ||
|                 {
 | ||
|                     IV2Times = 0;
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             return canShowFlg;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 检测是否达到N次 - 前N次不弹出插屏广告
 | ||
|         /// </summary>
 | ||
|         /// <param name="_ivType"></param>
 | ||
|         /// <returns></returns>
 | ||
|         public bool CheckIfReachFirstNTimes(ToukaSDKManager.IVType _ivType)
 | ||
|         {
 | ||
|             bool canShow = false;
 | ||
|             if (_ivType == ToukaSDKManager.IVType.IV1)
 | ||
|             {
 | ||
|                 int reachTimes01 = ToukaUtils.GetPlayerPrefsIntByKey(StaticStringsPlayerPrefs.TKInner_IV1ReachTimes, 0);
 | ||
| 
 | ||
|                 if (reachTimes01 >= TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV1_PreTimesN.ToString()))
 | ||
|                 {
 | ||
|                     Debug.Log("[ToukaInterstitialTimer]插屏 IV1 触达次数够了, reachTimes01 : " + reachTimes01 + " ,需要达到次数:" + TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV1_PreTimesN.ToString()));
 | ||
|                     canShow = true;
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     Debug.Log("[ToukaInterstitialTimer]插屏,iv1, 触达次数不够。当前已触达插屏点次数:" + reachTimes01);
 | ||
|                 }
 | ||
|                 reachTimes01++;
 | ||
|                 ToukaUtils.SavePlayerPrefsIntByKeyValue(StaticStringsPlayerPrefs.TKInner_IV1ReachTimes, reachTimes01);
 | ||
|             }
 | ||
|             else if (_ivType == ToukaSDKManager.IVType.IV2)
 | ||
|             {
 | ||
| 
 | ||
|                 int reachTimes02 = ToukaUtils.GetPlayerPrefsIntByKey(StaticStringsPlayerPrefs.TKInner_IV2ReachTimes, 0);
 | ||
| 
 | ||
|                 if (reachTimes02 >= TKGSDKManager.Instance.GetConfigInt(ToukaInnerParamKey.IV2_PreTimesN.ToString()))
 | ||
|                 {
 | ||
|                     Debug.Log("[ToukaInterstitialTimer]插屏 IV2 触达次数够了, reachTimes02 : " + reachTimes02);
 | ||
|                     canShow = true;
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     Debug.Log("[ToukaInterstitialTimer]插屏,IV2, 触达次数不够。当前已触达插屏点次数:" + reachTimes02);
 | ||
|                 }
 | ||
|                 reachTimes02++;
 | ||
|                 ToukaUtils.SavePlayerPrefsIntByKeyValue(StaticStringsPlayerPrefs.TKInner_IV2ReachTimes, reachTimes02);
 | ||
|             }
 | ||
| 
 | ||
|             Debug.Log("[ToukaInterstitialTimer]Check 前N次不弹出插屏广告,是否到达N次了 :" + canShow);
 | ||
|             return canShow;
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 开始播插屏/激励视频时进行计时
 | ||
|         /// </summary>
 | ||
|         public void StartTimer()
 | ||
|         {
 | ||
|             startedTime = Time.time;
 | ||
|             Debug.Log("[ToukaInterstitialTimer] 开始播插屏/激励视频,开始播广告计时:" + startedTime);
 | ||
|         }
 | ||
|         #endregion
 | ||
| 
 | ||
|     }
 | ||
| } |