Unity Ads 实战

4 篇文章 0 订阅

  点击 网站 跳转

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;
        }
    }



}

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值