控制Unity3D游戏中的广告之间的时间

时间:2018-05-18 02:17:26

标签: c# ios iphone xcode unity3d

我使用下面的obj.C代码来处理要展示的广告之间的时间。但现在我需要在C#中为Unity3D使用相同的代码。

-(void)showFullScreenads
{
    static NSTimeInterval gCBLastFail = -999;
    static bool isFirssst = true;

    if(!isFirssst)
    {
        NSTimeInterval intr = [NSDate timeIntervalSinceReferenceDate];

        float diff = intr - gCBLastFail;

        if(diff < 60.0f) // don't show ads if less than 60 sec
        {
            return;
        }
        else
        {
            gCBLastFail = [NSDate timeIntervalSinceReferenceDate];
        }
    }

    gCBLastFail = [NSDate timeIntervalSinceReferenceDate];
    isFirssst = false;

    [self showGoogleAdmobAds];
}

为unity3d寻找相同的样式代码,以控制广告之间的时间。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您不能像在C ++和Object-C中那样在函数中使用static。声明在函数外部使用静态限定符的变量。您可以将NSTimeIntervaltimeIntervalSinceReferenceDate替换为Time.time。如果从每个帧调用的showFullScreenads函数调用此Update函数,则会更好。

是等效的C#函数:

static float gCBLastFail = -999;
static bool isFirssst = true;

void showFullScreenads()
{
    if (!isFirssst)
    {
        float intr = Time.time;

        float diff = intr - gCBLastFail;

        if (diff < 60.0f) // don't show ads if less than 60 sec
        {
            Debug.Log("Add not displayed");
            return;
        }
        else
        {
            gCBLastFail = Time.time;
        }
    }

    gCBLastFail = Time.time;
    isFirssst = false;

    Debug.LogWarning("Add displayed");
    showGoogleAdmobAds();
}

void showGoogleAdmobAds()
{
    //Your admob plugin code to show ad

}