• unity sdk -AppLovin MAX 广告聚合平台接入+Firebase统计


    unity版本:2019.4.40f1

    Android Studio :4.2.1

    sdk版本:5.4.6

    max对应unity的官方文档:

    MAX Mediation Documentationhttps://dash.applovin.com/documentation/mediation/unity/getting-started/integration

    1、阅读官方文档 下载sdk 全部导入

     2、集成器设置

     a、集成max SDK 的更新 

    b、其他中介平台组加入(我这边加入了 google admob 和Mintegral和Pangle 三个中介平台)

     c、Applovin  key 添加,其他部分根据需求一般保持不变

     d、Android 配置设置

     3、Firebase接入

    Firebase接入文档https://mp.csdn.net/mp_blog/creation/editor/126500929

    按以上文档接入,需要注意 以上已经接入了google admob的sdk,所以Firebase sdk里面的google部分会冲突,所以冲突部分不需要导入

    设置完所有平台和其他插件后Force Resolve 下,就会下载所需的.arr文件

    例如: 

     

    4、代码初始化调用

    1. using System.Collections;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System;
    5. public class ServicesManager_MAX : MonoBehaviour
    6. {
    7. public static ServicesManager_MAX instance { get; set; }
    8. public bool openShowOpenAD = true;
    9. public bool openShowBanner = false;
    10. public string appIDAndroid;
    11. public string appOpenIDAndroid;
    12. public string bannerIDAndroid;
    13. public string interstitialID;
    14. public string rewardedVideoAdsID;
    15. bool isRewardAdded;
    16. [Header("IOS")]
    17. public string appIDIos;
    18. public string appOpenIDIos;
    19. public string bannerIDIos;
    20. public string interstitialIDIos;
    21. public string rewardedVideoAdsIDIos;
    22. private string sdkKey;
    23. private string appOpenID;
    24. private string bannerID;
    25. private string InterstitialsID;
    26. private string VideoID;
    27. private void Awake()
    28. {
    29. if (instance == null)
    30. {
    31. instance = this;
    32. DontDestroyOnLoad(this.gameObject);
    33. }
    34. else
    35. {
    36. GameObject.Destroy(this.gameObject);
    37. }
    38. }
    39. #if ADS_MAX
    40. void Start()
    41. {
    42. Application.targetFrameRate = 60;
    43. if (Application.platform == RuntimePlatform.IPhonePlayer)
    44. {
    45. sdkKey = appIDIos;
    46. bannerID = bannerIDIos;
    47. InterstitialsID = interstitialIDIos;
    48. VideoID = rewardedVideoAdsIDIos;
    49. }
    50. else
    51. {
    52. sdkKey = appIDAndroid;
    53. bannerID = bannerIDAndroid;
    54. InterstitialsID = interstitialID;
    55. VideoID = rewardedVideoAdsID;
    56. }
    57. //---test--start--测试id
    58. // sdkKey = "ENTER_MAX_SDK_KEY_HERE";
    59. // bannerID = "ENTER_BANNER_AD_UNIT_ID_HERE";
    60. //
    61. // InterstitialsID = "ENTER_INTERSTITIAL_AD_UNIT_ID_HERE";
    62. // VideoID ="ca-app-pub-ENTER_REWARD_AD_UNIT_ID_HERE/5224354917";
    63. //
    64. //---test---end-
    65. InitMAX();
    66. }
    67. void InitMAX()
    68. {
    69. MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration sdkConfiguration) =>
    70. {
    71. if (sdkConfiguration.ConsentDialogState == MaxSdkBase.ConsentDialogState.Applies)
    72. {
    73. //MaxSdk.SetHasUserConsent(true);
    74. //MaxSdk.SetHasUserConsent(false);
    75. // Show user consent dialog
    76. //MaxSdk.UserService.ShowConsentDialog();
    77. }
    78. else if (sdkConfiguration.ConsentDialogState == MaxSdkBase.ConsentDialogState.DoesNotApply)
    79. {
    80. // No need to show consent dialog, proceed with initialization
    81. }
    82. else
    83. {
    84. // Consent dialog state is unknown. Proceed with initialization, but check if the consent
    85. // dialog should be shown on the next application initialization
    86. }
    87. // AppLovin SDK is initialized, start loading ads
    88. InitAD();
    89. CheckFirebase();
    90. };
    91. MaxSdk.SetSdkKey(sdkKey);
    92. //MaxSdk.SetUserId("USER_ID");
    93. MaxSdk.InitializeSdk();
    94. //MaxSdk.InitializeSdk(new[] { "YOUR_AD_UNIT_ID_1", "YOUR_AD_UNIT_ID_2" });
    95. //MaxSdk.SetVerboseLogging(true);
    96. }
    97. void InitAD()
    98. {
    99. if (openShowBanner)
    100. {
    101. InitializeBannerAds();
    102. }
    103. if (openShowOpenAD)
    104. {
    105. InitializeOpenAd();
    106. }
    107. //
    108. Invoke("InitializeInterstitialAds", 0.5f);
    109. Invoke("InitializeRewardedAds", 1f);
    110. }
    111. void CheckFirebase()
    112. {
    113. Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
    114. {
    115. var dependencyStatus = task.Result;
    116. if (dependencyStatus == Firebase.DependencyStatus.Available)
    117. {
    118. // Create and hold a reference to your FirebaseApp,
    119. // where app is a Firebase.FirebaseApp property of your application class.
    120. //app = Firebase.FirebaseApp.DefaultInstance;
    121. // Set a flag here to indicate whether Firebase is ready to use by your app.
    122. Debug.Log("Create and hold a reference to your FirebaseApp");
    123. }
    124. else
    125. {
    126. UnityEngine.Debug.LogError(System.String.Format(
    127. "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    128. // Firebase Unity SDK is not safe to use here.
    129. }
    130. });
    131. }
    132. #endif
    133. private void Update()
    134. {
    135. AdOpenTime += Time.deltaTime;
    136. }
    137. //-----------------------------------横幅---------------------------------------------
    138. #if ADS_MAX
    139. public void InitializeBannerAds()
    140. {
    141. // Banners are automatically sized to 320×50 on phones and 728×90 on tablets
    142. // You may call the utility method MaxSdkUtils.isTablet() to help with view sizing adjustments
    143. MaxSdk.CreateBanner(bannerID, MaxSdkBase.BannerPosition.TopCenter);
    144. //MaxSdk.SetBannerExtraParameter(bannerAdUnitId, "adaptive_banner", "true")
    145. // Set background or background color for banners to be fully functional
    146. MaxSdk.SetBannerBackgroundColor(bannerID,new Color(1,1,1,0));
    147. MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerAdLoadedEvent;
    148. MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerAdLoadFailedEvent;
    149. MaxSdkCallbacks.Banner.OnAdClickedEvent += OnBannerAdClickedEvent;
    150. MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnBannerAdRevenuePaidEvent;
    151. MaxSdkCallbacks.Banner.OnAdExpandedEvent += OnBannerAdExpandedEvent;
    152. MaxSdkCallbacks.Banner.OnAdCollapsedEvent += OnBannerAdCollapsedEvent;
    153. }
    154. private void OnBannerAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    155. {
    156. //Invoke("ShowBannerAdmob", 1);
    157. ShowBannerAdmob("", "");
    158. }
    159. private void OnBannerAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) {}
    160. private void OnBannerAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
    161. private void OnBannerAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
    162. private void OnBannerAdExpandedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
    163. private void OnBannerAdCollapsedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
    164. #endif
    165. public void ShowBannerAdmob(string eventName, object data)
    166. {
    167. Debug.Log("ShowBannerAdmob loaded succesfully");
    168. #if ADS_MAX
    169. MaxSdk.ShowBanner(bannerID);
    170. #endif
    171. }
    172. public void HideBanner()
    173. {
    174. #if ADS_MAX
    175. MaxSdk.HideBanner(bannerID);
    176. #endif
    177. }
    178. //-----------------------------------自制开屏(用插屏当开屏)---------------------------------------------
    179. #if ADS_MAX
    180. bool isOnceOpenAd = false;
    181. void InitializeOpenAd()
    182. {
    183. isOnceOpenAd = true;
    184. }
    185. #endif
    186. //-----------------------------------插页---------------------------------------------
    187. #if ADS_MAX
    188. int retryAttemptInterstitial;
    189. DateTime lastNow = DateTime.Now;
    190. public void InitializeInterstitialAds()
    191. {
    192. // Attach callback
    193. MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
    194. MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent;
    195. MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent;
    196. MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickedEvent;
    197. MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent;
    198. MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent;
    199. // Load the first interstitial
    200. LoadInterstitial();
    201. }
    202. private void LoadInterstitial()
    203. {
    204. MaxSdk.LoadInterstitial(InterstitialsID);
    205. }
    206. private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    207. {
    208. // Interstitial ad is ready for you to show. MaxSdk.IsInterstitialReady(adUnitId) now returns 'true'
    209. // Reset retry attempt
    210. retryAttemptInterstitial = 0;
    211. DateTime newTime = DateTime.Now;
    212. TimeSpan ts1 = new TimeSpan(lastNow.Ticks);
    213. TimeSpan ts2 = new TimeSpan(newTime.Ticks);
    214. TimeSpan tsSub = ts1.Subtract(ts2).Duration();
    215. if (tsSub.TotalSeconds < 5 && isOnceOpenAd)
    216. {
    217. Debug.Log("OpenAd loaded succesfully");
    218. isOnceOpenAd = false;
    219. ShowInterstitialAdmob("ShowInterstitialAdmob", "");
    220. }
    221. }
    222. private void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
    223. {
    224. // Interstitial ad failed to load
    225. // AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds)
    226. print("OnInterstitialLoadFailedEvent Name: " + errorInfo.WaterfallInfo.Name + " and Test Name: " + errorInfo.WaterfallInfo.TestName);
    227. retryAttemptInterstitial++;
    228. double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptInterstitial));
    229. Invoke("LoadInterstitial", (float)retryDelay);
    230. }
    231. private void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
    232. private void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
    233. {
    234. // Interstitial ad failed to display. AppLovin recommends that you load the next ad.
    235. LoadInterstitial();
    236. }
    237. private void OnInterstitialClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
    238. private void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    239. {
    240. // Interstitial ad is hidden. Pre-load the next ad.
    241. LoadInterstitial();
    242. }
    243. #endif
    244. //带回掉的展示插屏
    245. public void ShowInterstitialAdmob(string eventName, object data)
    246. {
    247. #if ADS_MAX
    248. click = (System.Action)data;
    249. if (MaxSdk.IsInterstitialReady(InterstitialsID))
    250. {
    251. Debug.Log("Interstitial was loaded succesfully! interstialId=" + InterstitialsID[0]);
    252. MaxSdk.ShowInterstitial(InterstitialsID);
    253. }
    254. else
    255. {
    256. Debug.Log("Interstitial Loding " + InterstitialsID);
    257. }
    258. #endif
    259. }
    260. //限制时间的回掉插屏
    261. float AdOpenTime = 120f;
    262. public void ShowInterstitialAdmobTime(string eventName, object data)
    263. {
    264. if (AdOpenTime < (int)data)
    265. {
    266. return;
    267. }
    268. #if ADS_MAX
    269. click = null;
    270. if (MaxSdk.IsInterstitialReady(InterstitialsID))
    271. {
    272. AdOpenTime = 0;
    273. Debug.Log("Interstitial was loaded succesfully! interstialId=" + InterstitialsID[0]);
    274. MaxSdk.ShowInterstitial(InterstitialsID);
    275. }
    276. else
    277. {
    278. Debug.Log("Interstitial Loding " + InterstitialsID);
    279. }
    280. #endif
    281. }
    282. //-----------------------------------激励视频---------------------------------------------
    283. #if ADS_MAX
    284. System.Action click;
    285. int retryAttemptRewarded=0;
    286. public void InitializeRewardedAds()
    287. {
    288. // Attach callback
    289. MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
    290. MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
    291. MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;
    292. MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
    293. MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent;
    294. MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;
    295. MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
    296. MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
    297. // Load the first rewarded ad
    298. LoadRewardedAd();
    299. }
    300. private void LoadRewardedAd()
    301. {
    302. MaxSdk.LoadRewardedAd(VideoID[0]);
    303. }
    304. private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    305. {
    306. // Rewarded ad is ready for you to show. MaxSdk.IsRewardedAdReady(adUnitId) now returns 'true'.
    307. // Reset retry attempt
    308. retryAttemptRewarded = 0;
    309. }
    310. private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
    311. {
    312. // Rewarded ad failed to load
    313. // AppLovin recommends that you retry with exponentially higher delays, up to a maximum delay (in this case 64 seconds).
    314. retryAttemptRewarded++;
    315. double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptRewarded));
    316. Invoke("LoadRewardedAd", (float)retryDelay);
    317. }
    318. private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
    319. private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
    320. {
    321. // Rewarded ad failed to display. AppLovin recommends that you load the next ad.
    322. LoadRewardedAd();
    323. }
    324. private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { }
    325. private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    326. {
    327. // Rewarded ad is hidden. Pre-load the next ad
    328. LoadRewardedAd();
    329. }
    330. private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo)
    331. {
    332. if (click != null)
    333. {
    334. click();
    335. }
    336. // The rewarded ad displayed and the user should receive the reward.
    337. }
    338. private void OnRewardedAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    339. {
    340. // Ad revenue paid. Use this callback to track user revenue.
    341. }
    342. #endif
    343. ///
    344. /// 展示视频广告
    345. ///
    346. /// 标签页
    347. /// 奖励回掉
    348. /// 失败回掉
    349. public void ShowRewardedAd(string eventName, object data)
    350. {
    351. #if ADS_MAX
    352. click = (System.Action)data;
    353. isRewardAdded = false;
    354. if (MaxSdk.IsRewardedAdReady(VideoID))
    355. {
    356. AdOpenTime = 0;
    357. Debug.Log("Rewarded was loaded succesfully! " + VideoID);
    358. MaxSdk.ShowRewardedAd(VideoID);
    359. }else
    360. {
    361. Debug.Log("Rewarded Loding " + VideoID);
    362. }
    363. #endif
    364. }
    365. }

    5、打包发布

    由于unity2019,自带版本的gradle较低,跟sdk有冲突(需要升级gradle)

    unity中会显示类似这些报错:

    Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
    62 actionable tasks: 62 executed

    android studio 中显示如下的错误:

     插件版本与所需gradle版本的关系

    Android Gradle 插件版本说明  |  Android 开发者  |  Android Developers

    解决方案:我这边用的是  插件版本4.1.1  gradle 版本6.5

    方案1、unity中解决

    Gradle Distributions 版本下载https://services.gradle.org/distributions/下载gradle-6.5-all.zip

    下载完成后,我们需要把解压出来的gradle文件中的lib文件复制替换到你使用的unity对应位置,如D:\Program Files\2019.4.40f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle中的lib

     在发布设置中勾选 base gradle Template

    将 Plugins 中Android 中的baseProjectTemplate 中 classpath 修改成  4.1.1(根据你下载gradle 版本对应的插件版本)

     方案2、导出安卓工程在 android studio 中设置版本

    6、剩余问题(关于欧盟等其他地区隐私问题,目前也没搞懂放哪,有研究懂的兄弟可以回复我)

     

  • 相关阅读:
    PAT甲级 1072 Gas Station(30) (Dijkstrla)
    如何通用系统平台这个黑科技把网店做大,需要注意什么?
    2022年最新湖北水利水电施工安全员考试题库及答案
    创建并启动华为HarmonyOS本地与远程模拟器及远程真机
    VR智慧景区,为游客开启智慧旅游新时代
    怎样学习C#上位机编程?
    嵌入式分享合集74
    Python 中元组和列表的根本区别是什么?
    Android内存优化内存抖动的概念和危害
    计算机图形学(七)-深度缓存、着色shadding、着色模型、着色频率、渲染管线
  • 原文地址:https://blog.csdn.net/weixin_41843959/article/details/126666037