sdk 接入總結

AppsFlyer 接入

導入AppsFlyerUnityPlugin_v4.17.0

https://github.com/AppsFlyerSDK/Unity

添加腳本:

StartUp.cs:

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


public class StartUp : MonoBehaviour
{
 
    private bool tokenSent;

    // Use this for initialization
    void Start()
    {
        /* Mandatory - set your AppsFlyer’s Developer key. 修改key*/
        AppsFlyer.setAppsFlyerKey("4vrfSsrpHH6Tq4mC3kFFbm");
        /* For detailed logging */
        /* AppsFlyer.setIsDebug (true); */
#if UNITY_IOS
   /* Mandatory - set your apple app ID
      NOTE: You should enter the number only and not the "ID" prefix */
   AppsFlyer.setAppID ("YOUR_APP_ID_HERE");
   AppsFlyer.trackAppLaunch ();
#elif UNITY_ANDROID
   /* Mandatory - set your Android package name 包名*/
   AppsFlyer.setAppID ("com.hopemobi.lightup");
   /* For getting the conversion data in Android, you need to add the "AppsFlyerTrackerCallbacks" listener. key和回掉接收*/
   AppsFlyer.init ("4vrfSsrpHH6Tq4mC3kFFbm", "AppsFlyerTrackerCallbacks");
#endif           
    }
    public string getAppsFlyerId()
    {
        string AppsFlyerUID = AppsFlyer.getAppsFlyerId();
        return AppsFlyerUID;
    } 
    public void purchase()
    {
        System.Collections.Generic.Dictionary<string, string> purchaseEvent = new
System.Collections.Generic.Dictionary<string, string>();
        purchaseEvent.Add("af_currency", "USD");
        purchaseEvent.Add("af_revenue", "0.99");
        purchaseEvent.Add("af_quantity", "1");
        AppsFlyer.trackRichEvent("af_purchase", purchaseEvent);
    }
}

 

 

ADMOB ADS  GoogleMobileAds 接入

導入GoogleMobileAds

https://github.com/googleads/googleads-mobile-unity/releases

添加腳本

ServicesManager.cs

using System.Collections;
using UnityEngine;
using System.Collections.Generic;
#if ADS_ADMOB
using GoogleMobileAds.Api;
#endif
#if ADS_UNITY
using UnityEngine.Monetization;
using UnityEngine.Advertisements;
#endif

public class ServicesManager : MonoBehaviour {

    public static ServicesManager instance { get; set; }

    [HideInInspector] public int rewardedCoins;

    #region ADMOB
    [HideInInspector] public bool enableTestMode;
    [HideInInspector] public string appID;
    [HideInInspector] public string bannerID;
#if ADS_ADMOB
    [HideInInspector] public AdPosition bannerPosition;
#endif
    [HideInInspector] public string interstitialID;
    [HideInInspector] public string rewardedVideoAdsID;

#if ADS_ADMOB
    private BannerView bannerView;
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardVideoAd;
#endif

    #endregion
    #region UnityAds
    [HideInInspector] public bool testMode;
    [HideInInspector] public string gameID;
    [HideInInspector] public string bannerPlacementID;
    [HideInInspector] public string videoAdPlacementID;
    [HideInInspector] public string rewardedVideoAdPlacementID;
    #endregion
    #region IAP
    [HideInInspector] public string noAdsID;
    #endregion


    bool isRewardAdded;

    private void Awake()
    {
        instance = this;
    }
    // Use this for initialization
    void Start ()
    {
       DontDestroyOnLoad(this.gameObject);

        if (enableTestMode)
        {//廣告id 設置
            appID = "ca-app-pub-4262431636794617~9368741258";
            bannerID = "ca-app-pub-4262431636794617/6495165907";
            interstitialID = "ca-app-pub-4262431636794617/7672516205";
            rewardedVideoAdsID = "ca-app-pub-4262431636794617/9723964478";
        }

       InitializeAdmob();
       InitializeUnityAds();
	}
    #region Admob
    private void RequestBannerAdmob()
    {
#if ADS_ADMOB
        bannerView = new BannerView(bannerID,AdSize.Banner,AdPosition.Bottom);

        AdRequest request = new AdRequest.Builder().Build();

        bannerView.LoadAd(request);
#endif
    }
    private void RequestInterstialAdmob()
    {
#if ADS_ADMOB
        this.interstitial = new InterstitialAd(interstitialID);

        AdRequest request = new AdRequest.Builder().Build();

        this.interstitial.LoadAd(request);
#endif
    }
    private void RequestRewardedVideoAdAdmob()
    {
        isRewardAdded = false;

#if ADS_ADMOB
        this.rewardVideoAd = RewardBasedVideoAd.Instance;

        this.rewardVideoAd.OnAdRewarded += HandleRewardBasedVideoRewarded;

        AdRequest request = new AdRequest.Builder().Build();

        this.rewardVideoAd.LoadAd(request, rewardedVideoAdsID);
#endif
    }
    public void InitializeAdmob()
    {
#if ADS_ADMOB
        MobileAds.Initialize(appID);

        this.RequestInterstialAdmob();
        this.RequestRewardedVideoAdAdmob();
#endif
    }
    public void InitializeBannerAdmob()
    {
        #if ADS_ADMOB
        MobileAds.Initialize(appID);
#endif

        this.RequestBannerAdmob();
    }
    public void ShowBannerAdmob()
    {
        #if ADS_ADMOB
        this.bannerView.Show();
#endif
    }
    public void DestroyBannerAdmob()
    {
#if ADS_ADMOB
        this.bannerView.Destroy();
#endif
    }
    public void ShowInterstitialAdmob()
    {
#if ADS_ADMOB
        if (this.interstitial.IsLoaded())
        {
            Debug.Log("Interstitial was loaded succesfully!");

            this.interstitial.Show();
            UrlController.instance.UploadGameMove("Admob", "InterstitialAdmob", new Dictionary<string, object>
            {
                { "InterstitialAdmob","" },
            });

        }
#endif
    }
    public bool ShowRewardedVideoAdAdmob()
    {
#if ADS_ADMOB
        if (rewardVideoAd.IsLoaded())
        {
            Debug.Log("Rewarded was loaded succesfully!");
            
            rewardVideoAd.Show();
            UrlController.instance.UploadGameMove("Admob", "VideoAdAdmob", new Dictionary<string, object>
            {
                { "VideoAdAdmob","" },
            });
            return true;
        }
#endif
        return false;
    }
#if ADS_ADMOB
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;

        if (isRewardAdded == false)
        {
//             int coins = PlayerPrefs.GetInt("amountMoney");
// 
//             coins += 45;
// 
//             PlayerPrefs.SetInt("amountMoney", coins);
//             PlayerPrefs.Save();

            isRewardAdded = true;
        }
        Invoke("RequestRewardedVideoAdAdmob",1);
    }

    private void RequestRewarded()
    {
        RequestRewardedVideoAdAdmob();
    }

#endif
    #endregion
    #region UnityAds
    public void InitializeUnityAds()
    {
#if ADS_UNITY
        Monetization.Initialize(gameID, testMode);
        Advertisement.Initialize(gameID, testMode);
#endif
    }
#if ADS_UNITY
    private IEnumerator RequestInterstialUnityAds()
    {
        while (!Monetization.IsReady(videoAdPlacementID))
        {
            yield return new WaitForSeconds(0.25f);
        }

        ShowAdPlacementContent ad = null;
        ad = Monetization.GetPlacementContent(videoAdPlacementID) as ShowAdPlacementContent;

        if (ad != null)
        {
            ad.Show();
        }
    }

    IEnumerator RequestRewardedVideoAdUnityAds()
    {
        while (!Monetization.IsReady(rewardedVideoAdPlacementID))
        {
            yield return null;
        }

        ShowAdPlacementContent ad = null;
        ad = Monetization.GetPlacementContent(rewardedVideoAdPlacementID) as ShowAdPlacementContent;

        if (ad != null)
        {
            ad.Show(AdFinished);
        }

    }
    IEnumerator RequestBannerUnityAds()
    {

        while (!Advertisement.IsReady("banner"))
        {
            yield return new WaitForSeconds(0.5f);
        }
        Advertisement.Banner.Show(bannerPlacementID);

    }
    void AdFinished(UnityEngine.Monetization.ShowResult result)
    {
        if (result == UnityEngine.Monetization.ShowResult.Finished)
        {
            if (isRewardAdded == false)
            {
               int coins = PlayerPrefs.GetInt("amountMoney");

               coins += 45;

               PlayerPrefs.SetInt("amountMoney", coins);
               PlayerPrefs.Save();

               isRewardAdded = true;
            }
        }
    }
#endif
    public void ShowBannerUnityAds()
    {
#if ADS_UNITY
        StartCoroutine(RequestBannerUnityAds());
#endif
    }
    public void ShowInterstitialUnityAds()
    {
        #if ADS_UNITY
        StartCoroutine(RequestInterstialUnityAds());
#endif
    }
    public void ShowRewardedVideoUnityAds()
    {
        #if ADS_UNITY
        StartCoroutine(RequestRewardedVideoAdUnityAds());
#endif
    }
#endregion
}

重置sdk信息

生成下面文件:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章