0
0

UnityでAdmob実装とストアへのリリースなど

Last updated at Posted at 2023-12-03

【完走したい】楽しくいろいろやる Advent Calendar 20237日目の記事です。
広告のつけ方とかリリースの仕方など学びました。

少しずつアプリを作っていましたが、途中で
スクリーンショット 2023-10-18 002502.png
これが出て、直せずに新しく作り直していたら時間がかかりすぎて
スクリーンショット 2023-09-18 140853.png

こうなりました。
ということで、さっさとアプリをリリースします。

下準備

せっかくなのでAdmobのつけ方も簡単に説明します。
Google Admob
アカウントを作りましょう。
次に、Unityに色々入れます。(急ぎで書いてるので端折ります。調べれば出ます)
テスト用のIDを入れてちゃんとテスト広告が出るとか、テスト端末でちゃんと動くかとかよく調べたらOKです。
スクリーンショット 2023-09-18 141821.png
こんな感じです。
ちなみに、私は広告をつけるまでに3か月くらいかかりました。(androidmanifest.xmlの設定がうまくいってなかったため)
広告をつける際に、数字が/で区切られているタイプのものと~で区切られているものの2種類存在すると思います。使いかたを書いておきます。私はなぜ2つあるのかわからず1日かかってしまいました。
/で区切られているものは、広告を表示するための下にある実装したコードで使いました。
~で区切られているものはAndroidManifest.xmlで使います。
スクリーンショット 2023-11-18 004710.png
これには~のほうを使いましょう。


広告をつけるまではここで終わりです。次に、ゲームを制作後からリリースまでの過程を書いておきます。

大前提として、Android用にGooglePlayにアプリをリリースするためにはGooglePlayConsoleへの登録が必須でした。
GooglePlayConsole
25ドルあれば簡単にアカウントを作れました。
本人確認などを済ませたら画面に従って色々やって、アプリをリリースすればOKみたいです。
プライバシーポリシーとかを作る際は、わざわざサイトを作らなくてもGoogleドキュメントとかでOKみたいです。無料だし簡単です。

審査について

アプリ初リリース時、審査に6日間かかりました。
気長に待ちましょう。

詰まったポイント

広告実装
公式を元に実装してみたコード↓

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System;
//using UnityEditor.PackageManager.Requests;

public class AdsRewardNew1 : MonoBehaviour
{
    public GameObject Twall;
    public GameObject AdsReadyF;
    public NewMain NewMain;
    public SoundBtn SoundBtn;
    public GameObject AdsFailed;
    public GameObject Hantei;

#if UNITY_ANDROID
    private string _adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#elif UNITY_IPHONE
  private string _adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#else
  private string _adUnitId = "unused";
#endif

    private RewardedAd _rewardedAd;
    // Start is called before the first frame update
    void Start()
    {
        AdsFailed.SetActive(false);
       
    }
    // These ad units are configured to always serve test ads.


    /// <summary>
    /// Loads the rewarded ad.

    /// </summary>
    public void OnClick()
    {
        // this.gameObject.transform.position = new Vector3(100, 100, 100);
        
        SoundBtn.SoundA = false;
        AdsReadyF.SetActive(true);
        Twall.SetActive(true);
        LoadRewardedAd();
    }
    public void LoadRewardedAd()
    {
        // Clean up the old ad before loading a new one.
        if (_rewardedAd != null)
        {
           
            _rewardedAd.Destroy();
            _rewardedAd = null;
        }

        Debug.Log("Loading the rewarded ad.");

        // create our request used to load the ad.
        var adRequest = new AdRequest();

        // send the request to load the ad.
        RewardedAd.Load(_adUnitId, adRequest,
            (RewardedAd ad, LoadAdError error) =>
            {
                // if error is not null, the load request failed.
                if (error != null || ad == null)
                {
                    AdsFailed.SetActive(true);
                    Hantei.SetActive(true);
                    SoundBtn.SoundA = true;
                    this.gameObject.transform.position = new Vector3(0, -4, 40);
                    AdsReadyF.SetActive(false);
                    Twall.SetActive(false);
                  //  this.gameObject.SetActive(false);
                    Debug.LogError("Rewarded ad failed to load an ad " +
                                   "with error : " + error);
                    return;
                }

                Debug.Log("Rewarded ad loaded with response : "
                          + ad.GetResponseInfo());

                _rewardedAd = ad;
                ShowRewardedAd();
            });
    }
    //reward callback
    //メモ:74行目と76行目の_rewardedAdは元々_が入っていなかった
    public void ShowRewardedAd()
    {
        const string rewardMsg =
            "Rewarded ad rewarded the user. Type: {0}, amount: {1}.";

        if (_rewardedAd != null && _rewardedAd.CanShowAd())
        {
            _rewardedAd.Show((Reward reward) =>
            {
                AdsFailed.SetActive(false);
                Hantei.SetActive(false);

                SoundBtn.SoundA = true;
                this.gameObject.transform.position = new Vector3(0, -4, 40);
                AdsReadyF.SetActive(false);
                Twall.SetActive(false);
                // TODO: Reward the user.
                NewMain.AdsRewardAct();
                Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
            });
        }
    }
    //125行目の ad.OnAdFullScreenContentClosed += ()=>はもともと=>が入っていなかった
    private void RegisterEventHandlers(RewardedAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));
        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Rewarded ad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("Rewarded ad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Rewarded ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
           
            Debug.Log("Rewarded ad full screen content closed.");
            _rewardedAd.Destroy();
          
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Rewarded ad failed to open full screen content " +
                           "with error : " + error);
        };
    }
    
        private void RegisterReloadHandler(RewardedAd ad)
    {
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += ()=>
    {
            Debug.Log("Rewarded Ad full screen content closed.");

            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Rewarded ad failed to open full screen content " +
                           "with error : " + error);

            // Reload the ad so that we can show another as soon as possible.
            LoadRewardedAd();
        };
    }
    // Update is called once per frame
   
}

「//」でコメントアウトした部分(日本語で書いてあるもの)に、変更した部分(間違ってると思われる部分)を記載しています。
手を加えていない状態(公式のものを上から順番に書き写したもの)では以下のようなエラーが出ました。

スクリーンショット 2023-11-18 002051.png

見切れてますが、5個のエラーが出ています。

本日学んだこと アニメーションの管理

おそらく初めて学んだこと
・アニメーションの管理
アニメーションは

 public Animator Animator;

とか書いて、

 Animator.speed = 0;

とかでスピード調節できます。
初期化するには

WinChest1.SetActive(false);
WinChest1.SetActive(true);
Animator.Play("idle");
Animator.speed=0;

とかでいけます。何故かいったんオブジェクトを非表示→表示を行わないと、ちゃんと初期化されませんでした。
スピードは、初期化後に0にしておくと、初期化された状態で止まってくれます。

・BGMのつけ方
ゲームを作るにあたり音楽をつけようと考えました。簡単にできました。

 public AudioClip BGMA;
    public AudioClip BGMB;

AudioClipでBGMをくっつけて、
image.png

AudioSource.PlayOneShot(BGM名);

こんなもんで簡単に行けました
止めるときもStop()とかで適当に止めとけばいいと思います。

まとめ

初心者でも時間とやる気があればゲームはリリースできる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0