1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using UnityEngine;
- using GoogleMobileAds;
- using GoogleMobileAds.Api;
- using System;
- using System.Drawing;
- using UnityEngine.UIElements;
-
- public class ADS : MonoBehaviour
- {
- private BannerView bannerView;
- #if UNITY_ANDROID
- private string _adUnitId = "ca-app-pub-3570555734266765/8354908522";
- #elif UNITY_IPHONE
- private string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
- #else
- private string _adUnitId = "unused";
- #endif
- // Start is called before the first frame update
- void Start()
- {
- MobileAds.Initialize(initStatus => { });
- this.RequestBanner();
- }
-
-
- private void RequestBanner()
- {
- // 建立一個橫幅廣告.
- this.bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Bottom);
- // 建立廣告請求.
- AdRequest request = new AdRequest.Builder().Build();
-
- // 載入橫幅廣告.
- this.bannerView.LoadAd(request);
- }
-
- public void HideBanner()
- {
- if (this.bannerView != null)
- {
- this.bannerView.Hide();
- }
- }
-
- // 顯示廣告的方法
- public void ShowBanner()
- {
- if (this.bannerView != null)
- {
- this.bannerView.Show();
- }
- }
-
- // 銷毀廣告的方法
- public void DestroyBanner()
- {
- if (this.bannerView != null)
- {
- this.bannerView.Destroy();
- }
- }
- }
|