You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
6.5 KiB

using UnityEngine;
#if true
public class SampleAdsMgr : MonoBehaviour
{
public enum ADRewardType
{
AdAtkBuff,
AdGoldBuff,
AdExpBuff,
}
public static void ShowRewardedAd(ADRewardType adRewardType)
{
switch (adRewardType)
{
case ADRewardType.AdAtkBuff:
AdBuffMgr.SOnAdBuffAttack();
break;
case ADRewardType.AdGoldBuff:
AdBuffMgr.SOnAdBuffGold();
break;
case ADRewardType.AdExpBuff:
AdBuffMgr.SOnAdBuffExp();
break;
default:
break;
}
}
public static void ShowRewardedShopAd(AdsMgr.eRewardType rewtype, int irewid)
{
if (DataHandler.GetShop(1).buyCnt == 1) //광고제거 상품을 샀다면 바로 보상을 지급한다.
{
AdsMgr.SPlayRewardAds(rewtype, irewid);
}
else
{
AdsMgr.SPlayRewardAds(rewtype, irewid);
}
}
}
#else
public class SampleAdsMgr : MonoBehaviour
{
static SampleAdsMgr curMgr;
public enum ADRewardType
{
AdAtkBuff,
AdGoldBuff,
AdExpBuff,
}
//https://developers.google.com/admob/unity/quick-start?hl=ko 공식문서 참조
public void Start()
{
if (curMgr != null)
{
Destroy(gameObject);
}
else
{
curMgr = this;
}
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>//광고를 한번 초기화하기. 초기화는 한번만 이뤄지면 된다.
{
// This callback is called once the MobileAds SDK is initialized.
});
LoadRewardedAd();//광고를 불러오는 부분. 광고를 실제로 틀어주기 위해서는 불러오는 작업이 선행되어야 한다.
//모바일에서는 광고를 불러오기를 시작할때 불러오는속도가 다소 늦어서 게임을 시작할때 불러오도록 조정.
}
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5224354917";//테스트 광고. 테스트 과정에서 테스트가 아닌 실제 광고를 넣으면 정지당할 수 있음에 주의할 것.
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
private string _adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
/// <summary>
/// Loads the rewarded ad.
/// </summary>
public static void LoadRewardedAd()//광고를 불러오는 부분. 광고를 틀어주는 부분과는 다르다.
{
// Clean up the old ad before loading a new one.
if (curMgr._rewardedAd != null)
{
curMgr._rewardedAd.Destroy();
curMgr._rewardedAd = null;
}
Debug.Log("Loading the rewarded ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
RewardedAd.Load(curMgr._adUnitId, adRequest,
(RewardedAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
curMgr._rewardedAd = ad;
});
}
public static void ShowRewardedAd(ADRewardType adRewardType)//광고를 틀어주는 부분. 광고가 끝나면 보상을 얻는 코드를 여기서 작성한다.
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (curMgr._rewardedAd != null && curMgr._rewardedAd.CanShowAd())
{
curMgr._rewardedAd.Show((Reward reward) =>//여기서 시청이 제대로 이뤄졌다면 보상을 제공해주는 코드를 작성한다.
{//시청 끝나자마자 코드를 실행하면 게임이 뻑나기 때문에 코드는 0.2초 후에 실행되도록 조정.
MobileAds.RaiseAdEventsOnUnityMainThread = true;
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
curMgr.StartCoroutine(curMgr.adRewardCor(adRewardType));
LoadRewardedAd();
});
}
}
IEnumerator adRewardCor(ADRewardType adRewardType)
{
yield return new WaitForSeconds(0.2f);
switch (adRewardType)
{
case ADRewardType.AdAtkBuff:
AdBuffMgr.SOnAdBuffAttack();
break;
case ADRewardType.AdGoldBuff:
AdBuffMgr.SOnAdBuffGold();
break;
case ADRewardType.AdExpBuff:
AdBuffMgr.SOnAdBuffExp();
break;
default:
break;
}
}
public static void ShowRewardedShopAd(eRewardType rewtype, int irewid)//상점 광고품목을 샀을때.
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (DataHandler.GetShop(1).buyCnt == 1)//광고제거 상품을 샀다면 바로 보상을 지급한다.
{
// TODO: Reward the user.
//Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
curMgr.StartCoroutine(curMgr.adShopRewardCor(rewtype, irewid));
}
else
{
if (curMgr._rewardedAd != null && curMgr._rewardedAd.CanShowAd())
{
curMgr._rewardedAd.Show((Reward reward) =>//아니라면 광고를 틀고, 광고가 무사히 끝났을때 보상을 제공해준다.
{
MobileAds.RaiseAdEventsOnUnityMainThread = true;
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
curMgr.StartCoroutine(curMgr.adShopRewardCor(rewtype, irewid));//역시 0.2 초 후에 보상이 제공된다.
LoadRewardedAd();
});
}
}
}
IEnumerator adShopRewardCor(eRewardType rewtype, int irewid)
{
yield return new WaitForSeconds(0.2f);
AdsMgr.SPlayRewardAds(rewtype, irewid);
}
}
#endif