在Chartboost非页内广告出现时设置

时间:2014-06-12 17:46:16

标签: c# unity3d chartboost

不知怎的,我需要设置我的Chartboost插页式广告会在我想要的时候出现,因为它只是随机弹出(即使在游戏中,当玩家正在玩游戏时)。我试图在C#中的UNITY3D中这样做。

我的CBScript.cs中有代码,其中包含从Chartboost加载我的广告所需的所有信息。我在我的Hierarchy视图中将此脚本添加到我的空GameObject中。

CBScript.cs:

    using UnityEngine;
    using System.Collections;
    using System;
    using Chartboost;


    public class CBScript : MonoBehaviour {

        #if UNITY_ANDROID || UNITY_IPHONE

        public void Update() {
            #if UNITY_ANDROID
            // Handle the Android back button (only if impressions are set to not use activities)
            if (Input.GetKeyUp(KeyCode.Escape)) {
                // Check if Chartboost wants to respond to it
                if (CBBinding.onBackPressed()) {
                    // If so, return and ignore it
                    return;
                } else {
                    // Otherwise, handle it ourselves -- let's close the app
                    Application.Quit();
                }
            }
            #endif
        }

        void OnEnable() {
            // Initialize the Chartboost plugin
            #if UNITY_ANDROID
            // Replace these with your own Android app ID and signature from the Chartboost web portal
            CBBinding.init("ID", "Signature");
            #elif UNITY_IPHONE
            // Replace these with your own iOS app ID and signature from the Chartboost web porta

l
        CBBinding.init("ID", "Signature");
        #endif
    }

    void OnApplicationPause(bool paused) {
        #if UNITY_ANDROID
        // Manage Chartboost plugin lifecycle
        CBBinding.pause(paused);
        #endif
    }

    void OnDisable() {
        // Shut down the Chartboost plugin
        #if UNITY_ANDROID
        CBBinding.destroy();
        #endif
    }
    // UNITY_ANDROID || UNITY_IPHONE
    #endif
}

现在我要告诉大家们,我是如何将这段代码实现到我的一个游戏功能中的。我真的希望我的Chartboost插页式广告会在播放器丢失并且屏幕上显示游戏后显示。所以这是我的GUI脚本,ShowEnd函数的片段:

//Shows the end menu after a crash
    public void ShowEnd()
    {
        //Save the mission and activate the finish menu
        MissionManager.Instance.Save();
        EnableDisable(finishMenu, true);

        //Get the current coin and distance data
        int currentDist = (int)LevelGenerator.Instance.distance;
        int currentCoins = LevelManager.Instance.Coins();

        //Apply the data to the finish menu
        finishTexts[0].text = currentDist + "M";
        finishTexts[1].text = currentCoins.ToString();

        //If the current distance is greater than the best distance
        if (currentDist > SaveManager.GetBestDistance())
            //Set the current distance as the best distance
            SaveManager.SetBestDistance(currentDist);

        //Add the collected coins to the account
        SaveManager.SetCoins(SaveManager.GetCoins() + currentCoins);

        //Show the finish menu
        StartCoroutine(FadeScreen(0.4f, 0.7f));
        StartCoroutine(MoveMenu(finishMenu.transform, 0, -14.8f, 0.55f, false));

        OnGUI ();

    }

这是OnGUI();函数,可以捕获并实际显示插页式广告:

void OnGUI(){
        #if UNITY_ANDROID
        // Disable user input for GUI when impressions are visible
        // This is only necessary on Android if we have disabled impression activities
        //   by having called CBBinding.init(ID, SIG, false), as that allows touch
        //   events to leak through Chartboost impressions
        GUI.enabled = !CBBinding.isImpressionVisible();
        #endif

        GUI.matrix = Matrix4x4.Scale(new Vector3(2, 2, 2));
        CBBinding.cacheInterstitial("Default");
        CBBinding.showInterstitial("Default");
    }
像我说的那样,CB非页内广告随机出现。我希望我的广告只在Game Over屏幕后立即显示。我知道我现在做错了什么,但我不知道如何解决它。 谢谢你,谢谢。

1 个答案:

答案 0 :(得分:2)

我建议你应该在游戏启动时缓存插页式广告,可能是在你的一个游戏对象的OnEnable函数中。我建议你在你附加GUI的游戏对象的OnEnable中这样做。

void OnEnable() {
    CBBinding.cacheInterstitial("Default");
}

在OnGUI()函数中,首先应检查插页式广告,然后调用showInterstitial()

void OnGUI(){
        #if UNITY_ANDROID
        // Disable user input for GUI when impressions are visible
        // This is only necessary on Android if we have disabled impression activities
        //   by having called CBBinding.init(ID, SIG, false), as that allows touch
        //   events to leak through Chartboost impressions
        GUI.enabled = !CBBinding.isImpressionVisible();
        #endif

        GUI.matrix = Matrix4x4.Scale(new Vector3(2, 2, 2));
        if (CBBinding.hasInterstitial("Default"))
            CBBinding.showInterstitial("Default");
    }