在 Android 中实施奖励广告
奖励用户观看广告可以提高用户参与度,从而带来更高的收入。例如,游戏可以奖励用户游戏内货币、消耗品、额外生命或经验倍增器。有关如何有效设计奖励广告的更多信息,请参阅货币化策略指南。
要奖励用户完成视频广告,请使用IUnityAdsShowListener.onUnityAdsShowComplete
监听器回调方法的UnityAdsShowCompletionState
结果来检查用户是否已看完广告,以及是否应奖励他们。
奖励视频示例
Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext()
. Because this might not be suitable for all implementations, some customization might be required (depending on the integration).
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
是在视图中配置的按钮
Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext()
. Because this might not be suitable for all implementations, some customization might be required (depending on the integration).
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 中实施横幅广告。