Thứ Bảy, 18 tháng 7, 2015

Hướng dẫn tạo nút Share Facebook và gắn quảng cáo Admob vào Unity

Code phần share facebooK (nhớ có file Util.cs để chạy phần lấy UserName)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class FBHolder : MonoBehaviour {


void Awake()
{
FB.Init (SetInit, onHideUnity);
}

private void SetInit()
{
print ("FB Init done");
if (FB.IsLoggedIn) //logged in OK
{

print ("FB is Logged in");
}
else
{

//FBLogin();
}
}
private void onHideUnity(bool isGameShown)
{
if (!isGameShown)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}



void FBLogin()
{
FB.Login ("email,publish_actions", AuthCallBack);
}

void AuthCallBack(FBResult result)
{
if (FB.IsLoggedIn)
{
//DealWithFBMenus(true);
print("FB login worked");
FBfeed();
}
else
{
//DealWithFBMenus(false);
print("FB login Failed");
}
}


public void ShareWithFriends()
{

//check login or not yet:
if (FB.IsLoggedIn) //logged in OK -> goto share to FB
{
print ("logged in OK -> goto share to FB");
FBfeed();
}
else //not login yet, so go to login and share
{
print("not login yet, so go to login and share");
FBLogin();

}
}
void FBfeed()
{
FB.Feed(
linkCaption: "I'm playing this awesome game",
picture: "http://robovina.com/uploads/news/2015_03/autobase1.png",
linkName: "Check out this game",
//link: "http://apps.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ? FB.UserId : "Guest")
link: "https://play.google.com/store/apps/details?id=den.pin",
mediaSource: "https://www.youtube.com/watch?v=dAXW1R_aoz4"
);
}

}

Code phần gắn quảng cáo Admob:

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;


public class GoogleAdmod : 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.
AdRequest requestBannerView;
// Initialize an InterstitialAd.
InterstitialAd interstitial;
// Create an empty ad request.
AdRequest requestInterstitial;

// Use this for initialization
void Start () {
//ID for Banner:
#if UNITY_ANDROID
adUnitIdBanner = "ID cho vào đây";
#elif UNITY_IPHONE
adUnitIdBanner = "ID cho vào đây";
#else
adUnitIdBanner = "unexpected_platform";
#endif
//ID for Interstitial:
#if UNITY_ANDROID
adUnitIdInterstitial = "ID cho vào đây";
#elif UNITY_IPHONE
adUnitIdInterstitial = "ID cho vào đây";
#else
adUnitIdInterstitial = "unexpected_platform";
#endif
//banner:
bannerView = new BannerView(adUnitIdBanner, AdSize.Banner, AdPosition.Top);
requestBannerView = new AdRequest.Builder().Build();
bannerView.LoadAd(requestBannerView);
//interstitial:
interstitial = new InterstitialAd(adUnitIdInterstitial);
requestInterstitial = new AdRequest.Builder().Build();
interstitial.LoadAd(requestInterstitial);
//RequestBanner ();
//RequestInterstitial ();

}

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

}



public void showBanner()
{
bannerView.Show ();
}

public void showInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
interstitial = new InterstitialAd(adUnitIdInterstitial);
requestInterstitial = new AdRequest.Builder().Build();
interstitial.LoadAd(requestInterstitial);
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
}

Link video demo: https://www.youtube.com/watch?v=VjOml67Nub0
Link tải demo :  https://drive.google.com/file/d/0B3MbGKsNEy3AS0JUZVdMckNZNlU/view

Không có nhận xét nào:

Đăng nhận xét