在 Android 中实现奖励广告
对观看广告的用户进行奖励可以增加用户参与度,从而带来更高收入。例如,游戏可以用游戏币、消耗品、额外生命数量或经验倍增法宝来奖励用户。如需了解如何有效设计奖励广告的更多信息,请参阅变现策略指南。
要奖励用户看完视频广告,请使用 IUnityAdsShowListener.onUnityAdsShowComplete
监听器回调方法的 UnityAdsShowCompletionState
结果来检查用户是否看完广告并应该获得奖励。
奖励视频广告示例
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAdsShowOptions;
import com.unity3d.ads.example.R;
import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;
public class ShowRewardedAd extends AppCompatActivity implements IUnityAdsInitializationListener {
private String unityGameID = "1234567";
private Boolean testMode = true;
private String adUnitId = "rewardedVideo";
private IUnityAdsLoadListener loadListener = new IUnityAdsLoadListener() {
@Override
public void onUnityAdsAdLoaded(String placementId) {
UnityAds.show((Activity)getApplicationContext(), adUnitId, new UnityAdsShowOptions(), showListener);
}
@Override
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
Log.e("UnityAdsExample", "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
}
};
private IUnityAdsShowListener showListener = new IUnityAdsShowListener() {
@Override
public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
Log.e("UnityAdsExample", "Unity Ads failed to show ad for " + placementId + " with error: [" + error + "] " + message);
}
@Override
public void onUnityAdsShowStart(String placementId) {
Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
}
@Override
public void onUnityAdsShowClick(String placementId) {
Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
}
@Override
public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
if (state.equals(UnityAds.UnityAdsShowCompletionState.COMPLETED)) {
// Reward the user for watching the ad to completion
} else {
// Do not reward the user for skipping the ad
}
}
};
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
// Initialize the SDK:
UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);
}
@Override
public void onInitializationComplete() {
DisplayRewardedAd();
}
@Override
public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
Log.e("UnityAdsExample", "Unity Ads initialization failed with error: [" + error + "] " + message);
}
// Implement a function to load a rewarded ad. The ad will start to show after the ad has been loaded.
public void DisplayRewardedAd () {
UnityAds.load(adUnitId, loadListener);
}
}
奖励视频广告按钮
使用按钮让玩家选择观看广告是奖励视频广告的常见实现方式。可使用以下示例代码来创建奖励广告按钮。只要事先加载了广告,就会在按下广告按钮时展示广告。有关配置按钮的信息,请参阅有关按钮的 Android 开发者文档。
实现按钮后,请添加一个包含以下代码的脚本,其中 showAdButton
是 View 中配置的按钮:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAdsShowOptions;
import com.unity3d.ads.example.R;
import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;
public class RewardedAdButton extends AppCompatActivity implements IUnityAdsInitializationListener {
private String unityGameID = "1234567";
private Boolean testMode = true;
private String adUnitId = "rewardedVideo";
private Button rewardedButton;
private IUnityAdsLoadListener loadListener = new IUnityAdsLoadListener() {
@Override
public void onUnityAdsAdLoaded(String placementId) {
UnityAds.show((Activity)getApplicationContext(), adUnitId, new UnityAdsShowOptions(), showListener);
}
@Override
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
Log.e("UnityAdsExample", "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
}
};
private IUnityAdsShowListener showListener = new IUnityAdsShowListener() {
@Override
public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
Log.e("UnityAdsExample", "Unity Ads failed to show ad for " + placementId + " with error: [" + error + "] " + message);
// Re-enable the button if the user should be allowed to watch another rewarded ad
rewardedButton.setEnabled(true);
}
@Override
public void onUnityAdsShowStart(String placementId) {
Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
}
@Override
public void onUnityAdsShowClick(String placementId) {
Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
}
@Override
public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
if (state.equals(UnityAds.UnityAdsShowCompletionState.COMPLETED)) {
// Reward the user for watching the ad to completion
} else {
// Do not reward the user for skipping the ad
}
// Re-enable the button if the user should be allowed to watch another rewarded ad
rewardedButton.setEnabled(true);
}
};
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
// Initialize the SDK:
UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);
// Find the button in the view hierarchy and set its click function to load ads:
rewardedButton = findViewById (R.id.showAdButton);
rewardedButton.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
DisplayRewardedAd ();
rewardedButton.setEnabled(false);
}
});
}
@Override
public void onInitializationComplete() {
rewardedButton.setEnabled(true);
}
@Override
public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
Log.e("UnityAdsExample", "Unity Ads initialization failed with error: [" + error + "] " + message);
}
// Implement a function to load an rewarded ad. The ad will start to show after the ad has been loaded.
public void DisplayRewardedAd () {
UnityAds.load(adUnitId, loadListener);
}
}
后续步骤:请参阅在 Android 中实现横幅广告。