今天工作要求接Unity Ads 广告.
首先因为游戏用的是Unity4.7.1 所以看了下, 根本没有UnityEngine.Advertisements 这个名称空间. 无奈之下只好自己研究, Unity Ads插件的android源码. 不过也确实很简单.
public void InitUnityAds(String gameId,boolean testMode)
{
UnityAds.initialize(this,gameId,new YoyoAdListener(this),testMode);
}
public void ShowAd(String placementId)
{
UnityAds.show(this, placementId);
}
public boolean IsReady(String placementId)
{
return UnityAds.isReady(placementId);
}
public boolean IsSupport()
{
return UnityAds.isSupported();
}
public boolean IsInitialized()
{
return UnityAds.isInitialized();
}
就是根据上面这几个方法就可以了, 首先判断是否支持, 然后初始化, 然后判断广告是否已经好了, 是的话就播放.
不过有一个比较坑的是, 最终测试表明, 如果不挂VPN 的话, 在中国根本用不了. UnityAds.IsReady(string placementId) 永远都是返回false
看源码的时候还写了一些什么PlayerMetaData 和MediationMetaData , 不过这些都是没有用的东东, 直接无视把
记得要注册这个
<activity
android:name="com.unity3d.ads.adunit.AdUnitActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<activity
android:name="com.unity3d.ads.adunit.AdUnitTransparentActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:hardwareAccelerated="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity
android:name="com.unity3d.ads.adunit.AdUnitTransparentSoftwareActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:hardwareAccelerated="false"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity
android:name="com.unity3d.ads.adunit.AdUnitSoftwareActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:hardwareAccelerated="false"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />