public void StartAd(final boolean testing, final String deviceID, final boolean positionAtTop)
{
Log.d("AdsPlugin", "StartAd was called");
activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable()
{
@Override
public void run()
{
Log.d("AdsPlugin", "runOnUiThread started");
layout = new LinearLayout(activity.getApplicationContext());
layout.setGravity(((positionAtTop) ? Gravity.TOP : Gravity.BOTTOM) | Gravity.CENTER_HORIZONTAL);
activity.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
adView = new AdView(activity);
adView.setAdUnitId(pubID);
adView.setAdSize(adSize);
adView.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
if (callbackHandlerName != null)
{
UnityPlayer.UnitySendMessage(callbackHandlerName, "onAdLoaded", "");
}
}
@Override
public void onAdFailedToLoad (int errorCode)
{
if (callbackHandlerName != null)
{
UnityPlayer.UnitySendMessage(callbackHandlerName, "onAdFailedToLoad", Integer.toString(errorCode));
}
}
});
if (testing)
{
adView.loadAd(new AdRequest.Builder().addTestDevice(deviceID).build());
}
else
{
adView.loadAd(new AdRequest.Builder().build());
}
}
});
}
public void ShowAd(String unused)
{
activity.runOnUiThread(new Runnable()
{
@Override
public void run()
{
layout.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.invalidate();
layout.requestLayout();
}
});
}
public void HideAd(String unused)
{
activity.runOnUiThread(new Runnable()
{
@Override
public void run()
{
layout.removeView(adView);
layout.invalidate();
layout.requestLayout();
}
});
}
}
AdMob
The code below was designed to be used by a Unity plugin that I have written. It's incomplete, only shoing the important bits. The C# files on Unity's end basically use AndroidJavaObject to interface with this java code. The code can't be copy-pasted as a whole to run, but it may shed some light on how to utilize the new AdMob.
Subscribe to:
Posts (Atom)
No comments:
Post a Comment