Skip to main content

官方演示Demo中的脚本源码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class AdTimingSdkDemo : MonoBehaviour
{
//publisher app key
private string appKey;
public string appKeyIos;
public string appKeyAndroid;

//publisher banner placement Id;
private string bannerPlacementId;
public string bannerPidIos;
public string bannerPidAndroid;

Button btn_init, btn_rewardedVideo, btn_interstitial, btn_banner;

Button btn_gdprFalse, btn_gdprTrue;

Text message;

// Start is called before the first frame update
void Start()
{
#if(UNITY_IPHONE || UNITY_IOS)
appKey = appKeyIos;
bannerPlacementId = bannerPidIos;
#elif UNITY_ANDROID
appKey = appKeyAndroid;
bannerPlacementId = bannerPidAndroid;
#endif

Debug.LogError("start");
btn_init = GameObject.Find("InitSdk").GetComponent<Button>();
btn_rewardedVideo = GameObject.Find("RewardedVideo").GetComponent<Button>();
btn_interstitial = GameObject.Find("Interstitial").GetComponent<Button>();
btn_banner = GameObject.Find("Banner").GetComponent<Button>();
message = GameObject.Find("Message").GetComponent<Text>();

btn_gdprTrue = GameObject.Find("GDPRTrue").GetComponent<Button>();
btn_gdprFalse = GameObject.Find("GDPRFalse").GetComponent<Button>();

btn_rewardedVideo.interactable = false;
btn_interstitial.interactable = false;
btn_banner.interactable = false;



AddEvent();

btn_init.onClick.AddListener(InitSdk);

btn_rewardedVideo.onClick.AddListener(ShowRewardedVideo);

btn_interstitial.onClick.AddListener(ShowInterstitial);

btn_banner.onClick.AddListener(LoadAndShowBanner);

btn_gdprTrue.onClick.AddListener(setGdprTrue);
btn_gdprFalse.onClick.AddListener(setGdprFalse);



}

void setGdprTrue() {
UpdateInfo("setGDPR true");
AdTiming.Agent.setGDPRConsent(true);
}


void setGdprFalse() {
UpdateInfo("setGDPR false");
AdTiming.Agent.setGDPRConsent(false);
}


void AddEvent() {
//Add Event For Sdk Init
AdTimingEvents.onSdkInitSuccessEvent += OnInitSuccess;
AdTimingEvents.onSdkInitFailedEvent += OnInitError;
//Add Event For RewardedVideo
AdTimingEvents.onRewardedVideoAvailabilityChangedEvent += RewardedVideoAvailabilityChangedEvent;
AdTimingEvents.onInterstitialShowFailedEvent += RewardedVideoAdShowFailedEvent;
AdTimingEvents.onRewardedVideoClickedEvent += RewardedVideoClickedEvent;
AdTimingEvents.onRewardedVideoClosedEvent += RewardedVideoAdClosedEvent;
AdTimingEvents.onRewardedVideoStartedEvent += RewardedVideoAdStartedEvent;
AdTimingEvents.onRewardedVideoEndedEvent += RewardedVideoAdEndedEvent;
AdTimingEvents.onRewardedVideoShowFailedEvent += RewardedVideoAdShowFailedEvent;
AdTimingEvents.onRewardedVideoRewardedEvent += RewardedVideoAdRewardedEvent;
AdTimingEvents.onRewardedVideoShowedEvent += RewardedVideoAdShowEvent;

//Add Event For Interstitial
AdTimingEvents.onInterstitialAvailabilityChangedEvent += InterstitialAdAvailabilityChangedEvent;
AdTimingEvents.onInterstitialShowedEvent += InterstitialAdShowEvent;
AdTimingEvents.onInterstitialShowFailedEvent += InterstitialAdShowFaiedEvent;
AdTimingEvents.onInterstitialClickedEvent += InterstitialAdClickedEvent;
AdTimingEvents.onInterstitialClosedEvent += InterstitialAdClosedEvent;

//Add Event For Banner
AdTimingEvents.onBannerLoadSuccessEvent += BannerLoadSuccessEvent;
AdTimingEvents.onBannerClickedEvent += BannerClickedEvent;
AdTimingEvents.onBannerLoadFailedEvent += BannerLoadFailedEvent;

}

//Init AdTiming Sdk
void InitSdk() {
//AdTiming.Agent.setGDPRConsent(false);
UpdateInfo("Start Init Sdk");
UpdateInfo("appKey:" + appKey);
AdTiming.Agent.init(appKey);
//btn_init.interactable = false;

var is_init = AdTiming.Agent.isInitialized();
Debug.Log(is_init);
}

//Receive message when sdk init success
void OnInitSuccess() {
UpdateInfo("AdTiming Sdk Init Success");
btn_banner.interactable = true;

}

//Receive message when sdk init failed
void OnInitError(string error) {
UpdateInfo("AdTiming Sdk Init Error " + error);
btn_init.interactable = true;
}

//Update Text and print log
void UpdateInfo(string msg) {
message.text = msg;
Debug.LogError(msg);
}


/**
* RewardedVideo Event
*
*/

// bool buttonInteractable = false;

//在RewardedVideoAvailabilityChangedEvent的回调中更新按钮状态。
//若不在当前回调中设置,可以记录当前状态作为后续设置的依据。
void RewardedVideoAvailabilityChangedEvent(bool canShow) {
//记录状态,用于在需要时设置按钮状态。
//buttonInteractable = canShow;

UpdateInfo("RewardedVideo ad can show :" + canShow);

//在回调中直接设置按钮状态。
btn_rewardedVideo.interactable = canShow;
}


void RewardedVideoAdShowEvent(string scene) {
UpdateInfo("rewardedVideo ad show scene: " + scene);
}

void RewardedVideoAdRewardedEvent(string scene) {
UpdateInfo("rewardedVideo ad rewarded scene: " + scene);
//TODO IF this Event Received, you should reward users.

}

void RewardedVideoAdEndedEvent(string scene) {
UpdateInfo("rewardedVideo ad play ended scene: " + scene);
}

void RewardedVideoAdClosedEvent(string scene) {
UpdateInfo("rewardedVideo ad closed scene: " + scene);
//TODO Here resume your game.
}

void RewardedVideoAdStartedEvent(string scene) {
UpdateInfo("rewardedVideo ad startd event scene : " + scene);
}

void RewardedVideoAdShowFailedEvent(string error) {
UpdateInfo("rewardedVideo ad show failed error : " + error);
//TODO Here resume your game.
}

void RewardedVideoClickedEvent(string scene) {
UpdateInfo("rewardedVideo ad clicked scene: " + scene);
}


/**
* show rewardedVideo
*/


//点击按钮展示激励视频
void ShowRewardedVideo() {
//展示时判断是否有广告可供展示
if (AdTiming.Agent.isRewardedVideoReady())
{
//展示广告
//TODO Here pause game and show Ad
AdTiming.Agent.showRewardedVideo();
}
else {
//无广告时提示向用户反馈相应提示。
UpdateInfo("rewardedVideo Ad not ready");

}
}


/**
* interstiital event
*/

void InterstitialAdAvailabilityChangedEvent(bool canShow) {
UpdateInfo("Interstitial ad can show :" + canShow);
btn_interstitial.interactable = canShow;
}

void InterstitialAdShowFaiedEvent(string error) {
UpdateInfo("Interstitial ad show failed error: " + error);
}

void InterstitialAdShowEvent(string scene) {
UpdateInfo("Interstitial ad show scene:" + scene);
}

void InterstitialAdClickedEvent(string scene) {
UpdateInfo("Interstitial ad clicked scene: " + scene);
}

void InterstitialAdClosedEvent(string scene) {
UpdateInfo("Interstitial ad closed scene: " + scene);
}


/**
* show interstitial
*/

void ShowInterstitial() {
if (AdTiming.Agent.isInterstitialReady())
{
AdTiming.Agent.showInterstitial();

}
else {
UpdateInfo("Interstitial ad not ready");
}
}



/**
* load and show banner
*/

void BannerLoadSuccessEvent() {
btn_banner.interactable = true;
UpdateInfo("banner load success");
AdTiming.Agent.displayBanner(bannerPlacementId);
}

void BannerLoadFailedEvent(string error) {
btn_banner.interactable = true;
UpdateInfo("banner load failed error : " + error);
}

void BannerClickedEvent() {
UpdateInfo("banner ad clicked");
}

void LoadAndShowBanner() {
btn_banner.interactable = false;
UpdateInfo("banner loading...");
AdTiming.Agent.loadBanner("8405", AdSize.BANNER, BannerPostion.BOTTOM);
}



}