반응형

2022.01.18 - [Unity] - Unity3D - 유니티3D with AdMob 광고

Unity3D 앱에 배너 광고를 넣어 보자.

 

Empty 오브젝트를 만들고 스크립트를 추가한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#define UNITY_ANDROID
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using System;
using GoogleMobileAds.Api;
 
public class NewBehaviourScript : MonoBehaviour
{
    private BannerView bannerView;
 
    // Start is called before the first frame update
    void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        // Before loading ads, have your app initialize the Google Mobile Ads SDK
        // by calling MobileAds.initialize() which initializes the SDK and calls back
        // a completion listener once initialization is complete (or after a 30-second
        // timeout). This needs to be done only once, ideally at app launch.
        MobileAds.Initialize(initStatus =>
        {
            Dictionary<string, AdapterStatus> map = initStatus.getAdapterStatusMap();
            foreach (KeyValuePair<string, AdapterStatus> keyValuePair in map)
            {
                string className = keyValuePair.Key;
                AdapterStatus adapterStatus = keyValuePair.Value;
                switch (adapterStatus.InitializationState)
                {
                    case AdapterState.Ready:
                        // The adapter initialization ready.
                        MonoBehaviour.print($"Adapter: {className} is {adapterStatus.Description}");
                        // Adapter: ExampleClass is Ready
                        break;
 
                    case AdapterState.NotReady:
                        // The adapter initialization not ready.
                        MonoBehaviour.print($"Adapter: {className} is {adapterStatus.Description}");
                        break;
 
                    default:
                        break;
                }
            }
        });
 
        this.RequestBanner();
    }
 
    private void RequestBanner()
    {
        #if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/2934735716";
        #else
            string adUnitId = "unexpected_platform";
        #endif
 
        // Create a 320x50 banner at the top of the screen.
        this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
 
        // Called when an ad request has successfully loaded.
        this.bannerView.OnAdLoaded += this.HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.bannerView.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
        // Called when an ad is clicked.
        this.bannerView.OnAdOpening += this.HandleOnAdOpened;
        // Called when the user returned from the app after an ad click.
        this.bannerView.OnAdClosed += this.HandleOnAdClosed;
        // Removed OnAdLeavingApplication event for all formats.
        //this.bannerView.OnAdLeavingApplication += this.HandleOnAdLeavingApplication;
 
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
 
        // Load the banner with the request.
        this.bannerView.LoadAd(request);
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
 
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }
 
    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
            + args.ToString());
    }
 
    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }
 
    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
    }
 
    //public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    //{
    //    MonoBehaviour.print("HandleAdLeavingApplication event received");
    //}
 
    private void OnDestroy()
    {
        this.bannerView.Destroy();
    }
}
 

 

스크립트 소스를 입력하고 저장한다.

 

게임을 실행하면 화면 상단에 배너가 표시된다.

 

반응형
Posted by J-sean
:
반응형

 

2022.0118 - [Unity] - Unity3D - 유니티3D with AdMob 광고의 내용을 진행하다 보면 예상치 못한 에러를 만날 수 있다. 몇 가지 에러를 해결해 보자.

 

Resolving Android Dependencies 과정 중 발생하는 에러

환경 변수에 JAVA_HOME이 등록되지 않아 발생하는 에러다.

 

유니티 설치 폴더에 있는 OpenJDK 경로를 JAVA_HOME 변수로 등록한다.

 

Google.IOSResolver.dll을 로드할 수 없어 발생하는 에러

안드로이드 앱을 개발하기 위해 iOS Build Support를 설치하지 않아 발생하는 에러다.

 

Unity Hub를 실행한다.

 

 

설치되어 있는 Unity 아이콘 오른쪽 점(...)을 클릭하고 Add Modules를 선택한다.

 

iOS Build Support를 선택하고 DONE을 클릭해 설치한다.

 

반응형
Posted by J-sean
:
반응형

애드몹을 이용해 유니티에서 수익을 창출해 보자.

 

AdMob Unity 플러그인을 다운로드한다.

Google AdMob

 

유니티를 실행하고 플랫폼을 안드로이드로 바꾼다.

 

Assets - Import Package - Custom Package... 를 선택한다.

 

위에서 다운로드한 플러그인을 선택한다.

 

 

Import를 클릭한다.

 

No Thanks를 클릭한다.

※ API를 업데이트 하면 앱 완성 후 유니티에서만 정상 실행되고 실제 기기에서는 강제 종료되는 에러가 발생하기도 한다.

 

Enable을 클릭한다.

 

Assets - Google Mobile Ads - Settings... 를 선택한다.

 

 

App ID를 입력한다.

안드로이드 테스트 App ID

ca-app-pub-3940256099942544~3347511713

 

Empty 오브젝트를 하나 만들고 스크립트를 추가한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#define UNITY_ANDROID
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using System;
using GoogleMobileAds.Api;
 
public class NewBehaviourScript : MonoBehaviour
{
    private InterstitialAd interstitial;
 
    // Start is called before the first frame update
    void Start()
    {
        RequestInterstitial();
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
 
    private void RequestInterstitial()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/4411468910";
        #else
            string adUnitId = "unexpected_platform";
        #endif
 
        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);
 
        // Called when an ad request has successfully loaded.
        this.interstitial.OnAdLoaded += HandleOnAdLoaded;
        // Called when an ad request failed to load.
        this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
        // Called when an ad is shown.
        this.interstitial.OnAdOpening += HandleOnAdOpened;
        // Called when the ad is closed.
        this.interstitial.OnAdClosed += HandleOnAdClosed;
        // Removed OnAdLeavingApplication event for all formats.
        //this.interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;
 
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }
 
    public void HandleOnAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdLoaded event received");
    }
 
    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
            + args.ToString());
    }
 
    public void HandleOnAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdOpened event received");
    }
 
    public void HandleOnAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleAdClosed event received");
    }
 
    //public void HandleOnAdLeavingApplication(object sender, EventArgs args)
    //{
    //    MonoBehaviour.print("HandleAdLeavingApplication event received");
    //}
 
    private void OnDestroy()
    {
        this.interstitial.Destroy();
    }
 
    public void AdShow()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
    }
}
 

 

테스트 광고 단위 ID를 이용한 스크립트 소스를 입력하고 저장한다.

 

버튼을 하나 만든다.

 

 

On Click()에 위에서 만든 Empty 오브젝트와 AdShow()를 선택한다.

 

게임을 실행하고 버튼을 클릭한다.

 

테스트 광고가 표시된다.

 

※ 위 내용 진행 중 에러가 발생한다면 아래 링크를 참고한다.

2022.01.18 - [Unity] - Unity3D - 유니티3D with AdMob Troubleshooting

 

반응형
Posted by J-sean
: