点击 网站 跳转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class TestAds : MonoBehaviour
{
private PlayerAdsData _playerAdsData = new PlayerAdsData();
// Start is called before the first frame update
void Start()
{
_playerAdsData.Init();
GameObject.Find("btn1").GetComponent<Button>().onClick.AddListener(
() =>
{
_playerAdsData.ShowAdsByType(AdsType.Inter,(state) => { });
}
);
GameObject.Find("btn2").GetComponent<Button>().onClick.AddListener(
() =>
{
_playerAdsData.ShowAdsByType(AdsType.Video,(state)=> { });
}
);
GameObject.Find("btn3").GetComponent<Button>().onClick.AddListener(
() =>
{
_playerAdsData.ShowAdsByType(AdsType.Banner,(state) => { });
}
);
}
// Update is called once per frame
void Update()
{
_playerAdsData.ReadyAllAds();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public enum AdsType
{
Inter,
Video,
Banner,
}
public class PlayerAdsData
{
private string[] adsPathList = new string[]
{
"Interstitial_Android",//弹窗
"Rewarded_Android",//全屏视频
"Banner_Android",//条幅
};
private string myGameIdAndroid = "4134681";
private string myGameIdIOS = "4134680";
private bool testMode = false;
public void Init()
{
#if UNITY_IOS
Advertisement.Initialize(myGameIdIOS,testMode);
print("苹果实例化完毕");
#else
Advertisement.Initialize(myGameIdAndroid, testMode);
Debug.Log("安卓实例化完毕");
#endif
}
/// <summary>
/// 准备一个类型的广告
/// </summary>
/// <param name="adsType"></param>
public void ReadyAllAds()
{
GameObject.Find("content1").GetComponent<Text>().text = $"{(AdsType)0} {IsReadyByType(AdsType.Inter)}";
GameObject.Find("content2").GetComponent<Text>().text = $"{(AdsType)1} {IsReadyByType(AdsType.Video)}";
GameObject.Find("content3").GetComponent<Text>().text = $"{(AdsType)2} {IsReadyByType(AdsType.Banner)}";
}
/// <summary>
/// 获取一个类型准备好的广告
/// </summary>
/// <param name="adsType"></param>
public void ShowAdsByType(AdsType adsType,Action<bool> callBack=null)
{
if (IsReadyByType(adsType))
{
Advertisement.Show(adsPathList[(int)adsType], new AdsCallBack(callBack));
}
else {
if (callBack != null)
{
callBack(false);
}
}
}
/// <summary>
/// 指定类型的广告是否准备好
/// </summary>
/// <param name="adsType"></param>
/// <returns></returns>
public bool IsReadyByType(AdsType adsType)
{
return Advertisement.isInitialized && Advertisement.IsReady(adsPathList[(int)adsType]);
}
}
public class AdsCallBack : IUnityAdsShowListener
{
private Action<bool> _callBack;
public AdsCallBack(Action<bool> callBack)
{
_callBack = callBack;
}
private void SetBack(string content, bool state)
{
Debug.Log(content);
GameObject.Find("content").GetComponent<Text>().text = content;
if (_callBack != null)
{
_callBack(state);
}
}
public void OnUnityAdsShowClick(string placementId)
{
SetBack("OnUnityAdsShowClick", false);
}
public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
{
HandleShoResult(showCompletionState);
}
public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
{
SetBack("OnUnityAdsShowFailure",false);
}
public void OnUnityAdsShowStart(string placementId)
{
//SetBack("OnUnityAdsShowStart");
}
private void HandleShoResult(UnityAdsShowCompletionState result)
{
switch (result)
{
case UnityAdsShowCompletionState.COMPLETED: {
SetBack("COMPLETED", false);
} break;
case UnityAdsShowCompletionState.SKIPPED: {
SetBack("SKIPPED", false);
} break;
case UnityAdsShowCompletionState.UNKNOWN: {
SetBack("UNKNOWN", false);
} break;
}
}
}