question

protonzero avatar image
protonzero asked

Amazon Ads with Unity 5

I have a very simple test script based on this thread: https://forums.developer.amazon.com/forums/thread.jspa?threadID=5947&tstart=0 The full script: http://pastebin.com/6CLkKVtZ [b]Steps[/b] 1. Create new empty Unity Project 2. Import AmazonMobileAds.unitypackage 3. Create a C# script named AmazonAdsTest.cs and paste the pastebin contents 4. Run the program [b]Expected[/b] 1. Press 'Init' button 2. Press 'CreateInterstitialAd' button - Debug output "INTERSTITIAL 1" 3. Press 'LoadInterstitialAd' button - Debug output "loadingStarted: true" 4. Press 'Play' button - Debug output "adShown: true" [b]Actual Results in Unity Editor[/b] 1. Press 'Init' button 2. Press 'CreateInterstitialAd' button - Debug output "FLOATING 0" [b]<- Problem[/b] 3. Press 'LoadInterstitialAd' button - Debug output "loadingStarted: false" [b]Actual Results After Build and Run on Android Device (Watching log using logcat)[/b] 1. Press 'Init' button 2. Press 'CreateInterstitialAd' button - Debug output "INTERSTITIAL 1" 3. Press 'LoadInterstitialAd' button - Debug output "loadingStarted: False" [b]<- Problem[/b] - Debug output: KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[com.amazon.mas.cpt.ads.IDelegator]].get_Item (System.String key) [0x00000] in :0 at com.amazon.mas.cpt.ads.AmazonMobileAdsImpl.FireEvent (System.String jsonMessage) [0x00000] in :0 at com.amazon.mas.cpt.ads.AmazonMobileAdsImpl+AmazonMobileAdsBase.UnityFireEvent (System.String jsonMessage) [0x00000] in :0 [b]Unity Version:[/b] 5.0.3f2 [b]Android OS:[/b] 4.4.2 (Samsung S3) [b]Amazon SDK Version:[/b] Downloaded today from https://developer.amazon.com/public/resources/development-tools/sdk No Clearly marked version number on that page or the download. I thought that maybe there is some Amazon wrapper magic that happens. So I deployed an app to Amazon 'Live App Testing' and downloaded it as a tester after it was published, but still have the same problem. I've tried using my ApplicationKey as well instead of the testing one, same problem. What am I missing?
mobile ads
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justin avatar image
justin answered
Hi protonzero, Could you please try the following script? You'll want to instantiate a new instance of Ad each time you call mobileAds.createInterstitialAd(). [code]using System.Collections; using com.amazon.mas.cpt.ads; public class MobileAdsTest : MonoBehaviour { IAmazonMobileAds mobileAds = AmazonMobileAdsImpl.Instance; ApplicationKey key = new ApplicationKey(); ShouldEnable shouldEnable = new ShouldEnable(); void Start () { key.StringValue = "sample-app-v1_pub-2"; shouldEnable.BooleanValue = true; mobileAds.SetApplicationKey (key); mobileAds.EnableLogging (shouldEnable); mobileAds.EnableTesting (shouldEnable); //Optional mobileAds.EnableGeoLocation(shouldEnable); } void OnGUI () { //Create a new slot for interstitial if(GUI.Button(new Rect(20,40,80,20), "Create Ads")) { Ad interstialAd = mobileAds.CreateInterstitialAd(); string adType = interstialAd.AdType.ToString(); long identifier = interstialAd.Identifier; Debug.Log (identifier + " " + adType); } // Load interstitial if(GUI.Button(new Rect(20,80,80,20), "Load Ads")) { LoadingStarted response = mobileAds.LoadInterstitialAd(); bool loadingStarted = response.BooleanValue; Debug.Log (loadingStarted); } // Display interstitial if(GUI.Button(new Rect(20,120,80,20), "Show Ads")) { AdShown shownInterstitialAd = mobileAds.ShowInterstitialAd(); bool adShown = shownInterstitialAd.BooleanValue; Debug.Log("adShown: " + adShown); } } }[/code]
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

protonzero avatar image
protonzero answered
I tried out that script in the Editor and on my Android phone, I still get the same results. The Dictionary error can be ignored. Like you say, it is caused if you press the "Load Ads" button twice in a row without pressing "Create Ads" in between. But that isn't the main problem, the main problem is: 1. Run your script on an Android phone 2. Press the "Create Ads" button 3. Press the "Load Ads" button That is as far as it gets. The log will show that 'loadingStarted ' will be false. I also took a deeper look and can see that this SDK is not meant to be tested inside the Unity editor. AmazonMobileAdsUnityEditor directly inherits from AmazonMobileAdsDefault but doesn't override NativeCreateInterstitialAdJson. So in the Unity Editor when I requested an Interstitial Ad, it just returned an Ad object initialized with empty json so Ad.AdType defaults to FLOATING. Being able to test in the Unity Editor is a 'nice to have', but I can easily live without it if there is some way to make an Ad show up on the phone itself.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justin avatar image
justin answered
Hey protonzero, I just tested the source above and was able to successfully display ads on a Nexus 7. Are you using the manifest provided by the sample code in the Assets/Plugins/Android/ directory? (MobileAdsSampleAndroidManifest.xml) If not, please remove any existing AndroidManifest.xml files in that directory and try renaming MobileAdsSampleAndroidManifest to AndroidManifest.xml. There are two primary permissions required in using these samples that need to be added to your manifest: [code] [/code] I did notice some KeyNotFoundExceptions when loading the interstitial, but they are displaying as desired. I hope this helps!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

protonzero avatar image
protonzero answered
Good call. I renamed AmazonMobileAdsSampleAndroidManifest to AndroidManifest and I can see an advertisement now. Thanks for the help!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.