- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Unity3d : How to Control Show and Hide AdMob for Windows Phone 8 (Not Unity-Plugin)
Test on:
- Mac OSX Yosemite
- MS Lumia 535 Dual Sim
- Windows 10 Preview
- Unity3d 64-bit 5.1.2 (For Windows)
[Free source code]
Unity project: http://sh.st/AWmIO
Visual Studio 2013 project: http://sh.st/AWmJQ
Pre-require:
- VMware Fusion (For Mac)
- Visual Studio 2013 or Above (For Windows)
- Windows Phone SDK (For Windows)
- Unity3d 32-bit/64-bit 5.1.2 (For Windows)
- Authenticator (For Windows Phone)
- AdMob (For Windows Phone 8) - download: https://developers.google.com/mobile-ads-sdk/download
[Create Unity project]
1. Create new unity project, then create Game Object > 3D Object > 3D Text and input any detail refer to below image.
[Unity : Create Button for Control Ads]
1. Goto Assets > create > C# script name "CallAdmobVS", then implement ads event handler and ads state for both Banner and Interstitial, see below image.
[Visual Studio : Add GoogleAds library]
1. Open your exported project in Visual Studio by click yourProject.sln
2. Right-click on References > Add Reference...
3. Browse... > GoogleAds.dll then checked box and click OK. Now you're ready to begin implement.
[Visual Studio : Create Ads state UI View]
Open "MainPage.xaml" and add code <TextBlock/> as below image for show Ads state on Visual Studio UI when receive update from Unity button.
3. Inside MainPage() function after line of bridge.Control, let's implement BannerAds by create AdView use for banner only, set your ads alignment, create callback ads event of received and fail to received. After that create AdRequest for load ads. The last thing you need is add banner object to drawing surface object to show on screen and Don't forget to call function to request interstitialAd.
4. On previous step we call function "RequestNewInterstitial()" on the last line, so this step we need to create that function. Set cache flag as false and create new interstitial object with adunitid parameter. For interstitial we need more callback event because it's can be close by itself and cover fullscreen, after that create adrequest and load ads. Do you remember we need drawing surface object and AdView to display ads but it's not need inside this function.
5. Take a break about AdView and drawing surface for interstitial. Start create implement callback function for Banner simple receive and fail to receive but two of them just only print debug log in console.
6. Back to Interstitial to create callback function and lack about cache flag and drawing surface. Inside receive function we need to set "cache as true", On dismiss overlay we change "cache to false" and "remove adview object from drawing surface" because dismiss overlay callback done by interstitial closed or remove from screen by click ads close button. Then call "RequestNewInterstitial()" function again for prepare next interstitial to show on next time.
7. Inside function "DrawingSurfaceBackground_Loaded" we need to reimplement from comment out of code to check callback event between Unity and Visual Studio.
8. From previous step we create callback Unity_Loaded() event then we need to create function of it to get object from "CallAdmobVS" and create callback event for Ads flag changed.
9. Create new function "AdmobStateChanged" to determine show or hide Banner by flag changed.
10. Next create function "InterStateChanged" to create AdView and add interstitial object to drawing surface and then call ShowAd() method. For interstitial we need to detect flag only true event for show because it's close and set flag false by dismiss overlay callback function.
Now you finish control ads from Unity.
[AdMob Control Testing]
1. Connect your "WindowsPhone" to Windows Desktop / Laptop or Mac (Choose connect Windows), Make sure your WP connect to VS and registered for test via usb cable. Goto start > All apps > Windows Phone SDK 8.0/8.1 > Windows Phone Developer > Registration (8.0/8.1).
2. Click [>] "Green Arrow" icon and on the right side leave 3 fields as [ Device ][ Debug ][ ARM ]. your app will launch automatically.
[ Device ][ Debug ][ ARM ] << For testing and debugging in console log
[ Device ][ Master ][ ARM ] << For publish to store
[ Device ][ Release ][ ARM ] << For testing without log
3. After Unity process finish loaded, you should see two button for test show/hide banner and show for interstitial, let's test by yourself.
[Free source code]
Unity project: http://sh.st/AWmIO
Visual Studio 2013 project: http://sh.st/AWmJQ
Test on:
- Mac OSX Yosemite
- MS Lumia 535 Dual Sim
- Windows 10 Preview
- Unity3d 64-bit 5.1.2 (For Windows)
[Free source code]
Unity project: http://sh.st/AWmIO
Visual Studio 2013 project: http://sh.st/AWmJQ
Pre-require:
- VMware Fusion (For Mac)
- Visual Studio 2013 or Above (For Windows)
- Windows Phone SDK (For Windows)
- Unity3d 32-bit/64-bit 5.1.2 (For Windows)
- Authenticator (For Windows Phone)
- AdMob (For Windows Phone 8) - download: https://developers.google.com/mobile-ads-sdk/download
[Create Unity project]
1. Create new unity project, then create Game Object > 3D Object > 3D Text and input any detail refer to below image.
[Unity : Create Button for Control Ads]
1. Goto Assets > create > C# script name "CallAdmobVS", then implement ads event handler and ads state for both Banner and Interstitial, see below image.
2. See in function Start() we need to implement more a bit to check Main Camera object existed.
3. Create function "FixedUpdate" for handling object event relate to ads state use for Banner only.
4. Create button for control ads state from unity process and more code to change flag update ads state to show or hide for Banner but for Interstitial just only show flag because it's can be close by self close button not unity button.
4. Add component "CallAdmobVS" to object "New Text" and drag "Main Camera" under "Hierarchy" to inspector at "Game Camera".
[ Attach script CallAdmobVS to New Text inspector]
[Drag Main Camera and drop in field of CallAdmobVS]
5. Export your project for Windows Phone 8 click Build.
[Visual Studio : Add GoogleAds library]
1. Open your exported project in Visual Studio by click yourProject.sln
2. Right-click on References > Add Reference...
3. Browse... > GoogleAds.dll then checked box and click OK. Now you're ready to begin implement.
[Visual Studio : Create Ads state UI View]
Open "MainPage.xaml" and add code <TextBlock/> as below image for show Ads state on Visual Studio UI when receive update from Unity button.
[Visual Studio : Config GoogleAds requirement]
Open WMAppManifest.xml, goto capabilities then checked box like below list.
[/] ID_CAP_IDENTITY_DEVICE
[/] ID_CAP_IDENTITY_USER
[/] ID_CAP_MEDIALIB_AUDIO
[/] ID_CAP_MEDIALIB_PHOTO
[/] ID_CAP_MEDIALIB_PLAYBACK
[/] ID_CAP_NETWORKING
[/] ID_CAP_SENSORS
[/] ID_CAP_WEBBROWSERCOMPONENT
[Visual Studio : GoogleAds implement]
1. You need to include GoogleAds, Resources and Debugging for develop, debugging and publish. By then Open "MainPage.xaml.cs" follow add their lines of code like below image.
2. Begin declare variable for banner and interstitial, i just place dummy id for both of them like a "ca-app-pub-xxx/xxx" you need to replace with your real AdUnitID, for interstitial you need more variable as isCached for check already ads ready and AdView because of InterstitialAd not implement by AdView but it's need AdView to show overlay of interstitial.
3. Inside MainPage() function after line of bridge.Control, let's implement BannerAds by create AdView use for banner only, set your ads alignment, create callback ads event of received and fail to received. After that create AdRequest for load ads. The last thing you need is add banner object to drawing surface object to show on screen and Don't forget to call function to request interstitialAd.
5. Take a break about AdView and drawing surface for interstitial. Start create implement callback function for Banner simple receive and fail to receive but two of them just only print debug log in console.
6. Back to Interstitial to create callback function and lack about cache flag and drawing surface. Inside receive function we need to set "cache as true", On dismiss overlay we change "cache to false" and "remove adview object from drawing surface" because dismiss overlay callback done by interstitial closed or remove from screen by click ads close button. Then call "RequestNewInterstitial()" function again for prepare next interstitial to show on next time.
7. Inside function "DrawingSurfaceBackground_Loaded" we need to reimplement from comment out of code to check callback event between Unity and Visual Studio.
8. From previous step we create callback Unity_Loaded() event then we need to create function of it to get object from "CallAdmobVS" and create callback event for Ads flag changed.
9. Create new function "AdmobStateChanged" to determine show or hide Banner by flag changed.
10. Next create function "InterStateChanged" to create AdView and add interstitial object to drawing surface and then call ShowAd() method. For interstitial we need to detect flag only true event for show because it's close and set flag false by dismiss overlay callback function.
Now you finish control ads from Unity.
[AdMob Control Testing]
1. Connect your "WindowsPhone" to Windows Desktop / Laptop or Mac (Choose connect Windows), Make sure your WP connect to VS and registered for test via usb cable. Goto start > All apps > Windows Phone SDK 8.0/8.1 > Windows Phone Developer > Registration (8.0/8.1).
2. Click [>] "Green Arrow" icon and on the right side leave 3 fields as [ Device ][ Debug ][ ARM ]. your app will launch automatically.
[ Device ][ Debug ][ ARM ] << For testing and debugging in console log
[ Device ][ Master ][ ARM ] << For publish to store
[ Device ][ Release ][ ARM ] << For testing without log
3. After Unity process finish loaded, you should see two button for test show/hide banner and show for interstitial, let's test by yourself.
[Free source code]
Unity project: http://sh.st/AWmIO
Visual Studio 2013 project: http://sh.st/AWmJQ
It's work. ;)
- Get link
- X
- Other Apps
Comments
























First two images are not visible and do not open in a larger scale. Could you see to that?
ReplyDeleteThank you for your support, image link was broken.
DeleteI'm fixed it already.
Thanks for the great tutorial :). But I get this following error :( :
ReplyDeleteError 1 The type or namespace name 'CallAdmobVS' could not be found (are you missing a using directive or an assembly reference?)
How to solve the problem? Thanks :)
Please check and Make sure following step don't skip or miss something.
Delete[Unity : Create Button for Control Ads] on step 4 - 5
4. Add component "CallAdmobVS" to object "New Text" and drag "Main Camera" under "Hierarchy" to inspector at "Game Camera".
5. Export your project for Windows Phone 8 with Build.
Then close current project and re-open project in Visual Studio
[Visual Studio : Create Callback for Update Ads status] on step 4 - 5
4. Uncomment and modified callback event, under function "DrawingSurfaceBackground_Loaded" see this line "//UnityApp.SetLoadedCallback(() => { Dispatcher.BeginInvoke(Unity_Loaded); });", change it to "UnityApp.SetLoadedCallback(() => { UnityApp.BeginInvoke(Unity_Loaded); });" as below image.
5. Uncomment function "Unity_Loaded" and add code as below. It's initialize calling Class "CallAdmobVS" from Unity and binding event.
!! This is cause of issue. !!
1. Because of "CallAdmobVS" is C# created in MonoDevelop from Unity3d. If you were not "add component" and "export" correctly this error might be happen in VisualStudio.
2. So on in VisualStudio you need to change code from comment out of unity part NOT ONLY UNCOMMENT. eg. in step 4 if you use "Dispatcher.BeginInvoke(Unity_Loaded);" IT CAUSE OF AN ERROR in "UnityEngine.Object.FindObjectOfType()".
Thanks a lot, I can build and run the project. I can see banner and Text on the application, but I cannot see any button. And I got this error:
DeleteFindObjectsOfType can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Issue:
Delete"FindObjectsOfType" can only be called from the main thread.
Cause:
"Dispatcher.BeginInvoke" is not main thread.
"UnityApp.BeginInvoke" is main thread.
Solved: check this
1. take a look in "MainPage.xaml.cs"
2. in function "DrawingSurfaceBackground_Loaded"
2.1 change this line "UnityApp.SetLoadedCallback(() => { Dispatcher.BeginInvoke(Unity_Loaded); });" to be like this "UnityApp.SetLoadedCallback(() => { UnityApp.BeginInvoke(Unity_Loaded); });"
It's could be work.
It works, thanks a lot :D
DeleteI have trying this tutorial to display an interstitial ads when game is over to no avail. Please help.
ReplyDeleteOK, let's discuss have any problem you find? what's step number?
DeleteIt manages to show the interstitial but only when the game launches not when the game is over. I think the steps I should take in visual studio is where I get lost.
Delete1. If you need to show ads when game over by user tap on "button" in unity, let's try on this tutorial as ads banner every step carefully.
Delete2. when you success same as this tutorial backup project, then modified your code to be ads interstitial and move your button to game over scene.
I think you can refine your great work but try to create as my tutorial first to understand how can it work, if you found any error on any step please don't hesitate to ask me like above comment of "Meli Oktavia".
Hi,
ReplyDeleteUnityApp.SetLoadedCallBack(() => { UnityApp.BeingInvoke(Unity_Loaded);}); is not working in windows phone 8.1. DrawingSurfaceBackground_Loaded doesn't even appear. Can you help please?
I'm testing with Lumia 535, it's a Windows Phone 8.1 so work well.
DeleteCan you show me have any error or log message in your VisualStudio.
Thanks for answering,
DeleteI'm using visualstudio 2015 and unity 5.3.2 which doesnt have anymore the option to build to windows phone 8. So I'm building to Windows Store -> phone 8.1. "using UnityApp" doesnt work. So in MainPage() I dont have any of the code you have like: "var bridge = new UnityBridge(); UnityApp.SetBridge(bridge); bridge.control = DrawingSurfaceBackground. And I dont have the DrawingSurfaceBackground_Loaded function where you call UnityApp.SetLoadedCallBack ... etc. What can I do?
Yes, Unity 5.3.x has removed support "windows phone 8" and so on "var bridge = new UnityBridge(); ... " generate from Unity support export version.
DeleteThe Windows Store export have something similar issue like your problem.
I'm never test on Unity 5.3.x, so this time if you want to got anything work as my tutorial.
Solution 1, Uninstall your current unity and install version below 5.3.x (5.1.2 as my sample)
-- OR --
Solution 2, Install multiple unity version "Link"
This comment has been removed by the author.
ReplyDeleteI commented the Start() in Class CallAdmobVS in Unity. Could it be the problem?
DeleteSorry. I found the reason. But the ad just appear when the phone connected. Is it right?
DeleteYes, you need to connect your phone when develop any admob but if you want it work on publish version please change to Master build not debug or released
DeleteIs it including the Debug in Visual Studio?
DeleteIn your Interstitial Ad tutorial, when I disconnect the phone the ad still appear, but in Ad banner it doesn't.
I am getting Null Pointer Exception at interScript.InterStateChanged += InterStateChanged;
ReplyDeleteI am using unity 4.7.0
Please check by print out after line "var interScript" for make sure, it's not null. If your code in Unity and Export correctly it's should not be null.
DeleteBy the way Unity 4.7 and Unity 5.x have many things change, it's may be cause of that problem or not, I'll check by myself.
DeleteBut soon I've got an email from google for terminate to support Admob for windows phone, We're known as it's not popular mobile os. You can give up this platform now.
Porawee, I need some help. Ads only show if the script is in first scene of game. If the script is in second scene then in Unity_Loaded() we have null exception. Can you please tell when is Unity_Loaded() called? Is there a way we can call ads in second or third scene?
DeleteI haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. hide my wp
ReplyDeleteI am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place hide my wp
ReplyDeleteThis particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! centralina aggiuntiva golf 5 2.0 tdi
ReplyDeleteI curious more interest in some of them hope you will give more information on this topics in your next articles. บาคาร่า365
ReplyDeleteBy facilitating your own Minecraft server you can change the servers settings and play with companions. Minecraft Server List
ReplyDelete