5次亏损后如何实施广告?
5次亏损后如何实施广告? Unity广告。它可以工作,但是在玩家输了五次之后,我需要这样做,广告就开了!
这是我拥有的代码!
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class AdsManager : MonoBehaviour
{
private int i = 0;
// Use this for initialization
void Start()
{
if (Advertisement.isSupported)
Advertisement.Initialize ("23153718", false);
else
Debug.Log ("Platform is not supported");
i = 0;
}
// Update is called once per frame
void Update ()
{
if (GameManager.singleton.isGameOver == true)
{
if (i == 0)
{
ShowAd();
i++;
}
}
}
public void ShowAd()
{
if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
}
答案 0 :(得分:0)
如果您有多个场景,则可以简单地使用static
作为该值,因为它是整个应用程序都需要考虑的单个值(因此它不依赖于实例)。
使用适当的setter方法,也无需检查Update
中每一帧的计数器,而仅在实际更改时检查。
如果您还用static
constructor替换Start
方法,则可以使整个类static
完全不需要是MonoBehaviour
。
public static class AdsManager
{
private static bool _isSuppoerted;
public static int AdsCounter
{
get;
private set;
}
public static void IncreaseAdsCounter()
{
AdsCounter++;
if (AdsCounter >= 5)
{
ShowAd();
AdsCounter = 0;
}
}
public static void ShowAd()
{
if(!_isSuppoerted)
{
Debug.LogWarning("AdsManager: Platform is not supported!");
return;
}
if (Advertisement.IsReady())
{
Advertisement.Show();
}
// maybe reset the counter also if called from somewhere else?
//AdsCounter = 0;
}
// Use this for initialization
static AdsManager()
{
if (Advertisement.isSupported)
{
Advertisement.Initialize("23153718", false);
_isSuppoerted = true;
}
else
{
Debug.LogWarning("AdsManager: Platform is not supported!");
_isSuppoerted = false;
}
AdsCounter = 0;
}
}
比其他任何脚本都要高,例如在GameManager
中,您可以简单地致电
AdsManager.IncreaseAdsCounter();