Đầu tiên ta đăng ký cái tài khoản quảng cáo trong Google Admob ở link này: https://apps.admob.com
Hổi trước mình dùng quảng cáo Startapp, giờ sang bên này thử thôi :)
Sau khi tải Unity SDK plugin thì import vào project, mở code lên và code như mình đã làm sau đây, đảm bảo chạy 100%:
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class GoogleAdmob : MonoBehaviour {
//ID for Banner:
string adUnitIdBanner;
string adUnitIdInterstitial;
// Create a 320x50 banner at the top of the screen.
BannerView bannerView;
// Create an empty ad request.
// Initialize an InterstitialAd.
public InterstitialAd interstitial;
// Create an empty ad request.
//public Text admobText;
//public float bannerWidth;
// Use this for initialization
void Start () {
//ID for Banner:
#if UNITY_ANDROID
adUnitIdBanner = "thay cái này bằng ID của bạn dành cho quảng cáo banner trên android";
#elif UNITY_IPHONE
adUnitIdBanner = "thay cái này bằng ID của bạn dành cho quảng cáo banner trên IPHONE";
#else
adUnitIdBanner = "unexpected_platform";
#endif
//ID for Interstitial:
#if UNITY_ANDROID
adUnitIdInterstitial = "thay cái này bằng ID của bạn dành cho quảng cáo Interstitial trên android";
#elif UNITY_IPHONE
adUnitIdInterstitial = "thay cái này bằng ID của bạn dành cho quảng cáo Interstitial trên IPHONE";
#else
adUnitIdInterstitial = "unexpected_platform";
#endif
//banner:
//bannerView = new BannerView(adUnitIdBanner, AdSize.Banner, AdPosition.Bottom);
bannerView = new BannerView(adUnitIdBanner, adSize, AdPosition.Bottom);
bannerView.LoadAd(new AdRequest.Builder().Build());
//interstitial:
CreateAndLoadInterstitial ();
}
// Update is called once per frame
void Update () {
}
public void showInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
public InterstitialAd CreateAndLoadInterstitial()
{
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitIdInterstitial);
interstitial.LoadAd(new AdRequest.Builder().Build());
// Register for ad events.
interstitial.AdLoaded += delegate(object sender, System.EventArgs args) {
//admobText.text = "da tai qc";
};
interstitial.AdFailedToLoad += delegate(object sender, AdFailedToLoadEventArgs args) {
//admobText.text = "ko tai dc qc: " + args.Message;
};
//interstitial.AdOpened += delegate(object sender, System.EventArgs args) {};
//interstitial.AdClosing += delegate(object sender, System.EventArgs args) {};
interstitial.AdClosed += delegate(object sender, System.EventArgs args) {
interstitial.Destroy();
interstitial = CreateAndLoadInterstitial();
};
//interstitial.AdLeftApplication += delegate(object sender, System.EventArgs args) {};
return interstitial;
}
}
////////
Chỉ cần gọi hàm showInterstitial() tại bất kỳ đâu thì nó sẽ hiện quảng cáo interstitial, còn việc bắt sự kiện thì xem hàm public InterstitialAd CreateAndLoadInterstitial() là hiểu.
Tham khảo thêm ở đây:
1.Hỗ trọ gắn quảng cáo Admob trong Unity: https://developers.google.com/admob/games#unity&subid=apac-semexp3-r3
2. Hỗ trợ về quảng cáo Interstitial: http://googleadsdeveloper.blogspot.com/2014/05/google-mobile-ads-unity-plugin-21-adds.html
3. Và nhớ copy thư mục "google-play-services_lib" nằm trong "sdk\extras\google\google_play_services\libproject" vào thư mục "Assets/Plugins/Android" trong Project Unity đang làm nhé.
Còn cái Facebook login + share + invite friend mới làm xong hôm qua để hôm nào rảnh rảnh post nốt.