如何在unity3d中实现亚马逊广告

时间:2016-08-11 07:13:20

标签: unity3d amazon ads

我想在Unity3d游戏中将亚马逊移动广告添加到Android和ios

并在此代码中遇到此问题

public void createIad(){
    CreateInterstitialAd ();
}

Ad CreateInterstitialAd(){

IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
AdInterstitial = mobileAds.CreateInterstitialAd();
string adType = AdInterstitial.AdType.ToString();
long identifier = AdInterstitial.Identifier;
    /*
    LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
    bool loadingStarted = LSObject.BooleanValue;
    */
    return AdInterstitial;
}

1 个答案:

答案 0 :(得分:1)

在developer.amazon.com上注册开发者帐户

注册后,转到主仪表板页面,然后点击Apps&服务标签 并创建一个新的应用程序

填写所有必要信息并复制您的应用程序密钥 enter image description here

通过此链接下载亚马逊移动广告sdk并导入广告插件

https://developer.amazon.com/public/resources/development-tools/sdk-thank-you?product=apps_games_services_unity

然后在Unity中添加此代码以初始化广告代码

“将代码放在脚本中后,请不要忘记将app键粘贴到此脚本app key参数”

using UnityEngine;

使用System.Collections; 使用com.amazon.mas.cpt.ads;

公共课AdTest:MonoBehaviour {

public string androidKey;

public string iosKey;

private IAmazonMobileAds mobileAds;

private static AdTest instance2;

public static AdTest Instance

{
    get { return instance2; }
}
void Awake() {

    DontDestroyOnLoad (transform.gameObject);
    // If no Player ever existed, we are it.
    if (instance2 == null)
        instance2 = this;
    // If one already exist, it's because it came from another level.
    else if (instance2 != this) {
        Destroy (gameObject);
        return;
    }


    //CloseFloatingAd ();
    //DisplayInterstitial ();
}


// Use this for initialization
void Start () {
    SetAppKey ();

    //Delete this function before releasing your app
    EnableTesting ();
    //

    //DisplayInterstitial ();

}

// Update is called once per frame
void Update () {

}


public void SetAppKey(){

    // Create a reference to the mobile ads instance

    mobileAds = AmazonMobileAdsImpl.Instance;



    // Create new key

    ApplicationKey key = new ApplicationKey ();

    //zum Testen
    //key.StringValue = androidKey;

    // Set key based on OS

    #if UNITY_ANDROID

    key.StringValue = androidKey;

    #elif UNITY_IPHONE

    key.StringValue = iosKey;

    #endif



    // Pass in the key

    mobileAds.SetApplicationKey (key);

}

public void EnableTesting(){

    //Create should enable instance

    ShouldEnable enable = new ShouldEnable ();

    enable.BooleanValue = true;

    mobileAds.EnableTesting (enable);

    mobileAds.EnableLogging (enable);   

}



Ad AdObject;
/*
public Ad CreateFloatingBannerAd(Placement input){

    IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
    Placement placement = new Placement ();
    placement.Dock = Dock.BOTTOM;

    placement.HorizontalAlign = HorizontalAlign.CENTER;

    placement.AdFit = AdFit.FIT_AD_SIZE;

    Ad response = mobileAds.CreateFloatingBannerAd (placement);

    string adType = response.AdType.ToString ();
    long identifier = response.Identifier;

}*/


public void CloseFloatingAd(){

    if (AdTest.Instance.AdObject != null) {
        IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
        mobileAds.CloseFloatingBannerAd (AdObject);
        CreateFloatingBannerAd ();

    }
}

LoadingStarted LoadInterstitialAd(){
    IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;
    LoadingStarted response = mobileAds.LoadInterstitialAd ();
    bool loadingStarted = response.BooleanValue;
    return response;
}

public void createBanner(){
    CreateFloatingBannerAd ();
}


Ad CreateFloatingBannerAd(){

    // Configure placement for the ad

    Placement placement = new Placement ();

    //placement.Dock = Dock.TOP;
    placement.Dock = Dock.BOTTOM;

    placement.HorizontalAlign = HorizontalAlign.CENTER;

    placement.AdFit = AdFit.FIT_AD_SIZE;

    AdObject = mobileAds.CreateFloatingBannerAd(placement);
    return AdObject;
}

public void DisplayFloatingAd(){

    // Configure placement for the ad

    Placement placement = new Placement ();

    //placement.Dock = Dock.TOP;
    placement.Dock = Dock.BOTTOM;

    placement.HorizontalAlign = HorizontalAlign.CENTER;

    placement.AdFit = AdFit.FIT_AD_SIZE;



    // This method returns an Ad object, which you must save and keep track of

    AdObject = mobileAds.CreateFloatingBannerAd(placement);



    // This method returns a LoadingStarted object

    LoadingStarted newResponse = mobileAds.LoadAndShowFloatingBannerAd(AdObject);

}

Ad AdInterstitial;
AdShown AdSObject;

public void createIad(){
    CreateInterstitialAd ();
}

Ad CreateInterstitialAd(){

    Debug.Log ("CreateInterstitialAd()+++++++");
IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;

AdInterstitial = mobileAds.CreateInterstitialAd();

string adType = AdInterstitial.AdType.ToString();
long identifier = AdInterstitial.Identifier;
    /*
    LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
    bool loadingStarted = LSObject.BooleanValue;
    */
    return AdInterstitial;
}



public void DisplayInterstitial(){

    CreateInterstitialAd ();

    IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance;

    LoadingStarted LSObject = mobileAds.LoadInterstitialAd();
    bool loadingStarted = LSObject.BooleanValue;


        AdSObject = mobileAds.ShowInterstitialAd ();
        //bool adSwohn = AdSObject.BooleanValue;


}

}

您还可以通过此脚本调用实例来加载广告并显示它们 如下所示

public void DisplayBanner () {
    AdTest.Instance.DisplayFloatingAd ();
}
public void CreateBanner () {
    AdTest.Instance.createBanner();
}
public void DisplayInterstitial () {
    AdTest.Instance.DisplayInterstitial();
}
public void CreateInterstitial () {
    AdTest.Instance.createIad();
}

我的问题无关紧要,因为我编写本教程是因为每个人都需要有关此主题的帮助

我希望这可能有帮助