LoginSignup
1
0

Unity 研究期間の成果

Last updated at Posted at 2022-09-03

はじめに

研究期間(実装したことないものを実装する期間)中に実装したものについて、実装方法や参考資料を本記事にまとめました。
他に面白そうな実装物などあれば、是非コメント下さい!

目次

1.AdMob起動時広告の実装
2.Google Play In-App Review APIの実装
3.AdMobネイティブ広告の実装
4.AdMob収益受け取り方法
5.Gitの環境構築
6.NetlifyでHP制作

1.AdMob起動時広告の実装

公式サンプルスクリプト:https://github.com/googleads/googleads-mobile-unity/tree/main/samples/HelloWorld

①下記サイトから "GoogleMobileAds-v7.1.0.unitypackage" をダウンロードする
※v7.2.0は未確認

②Unityのプロジェクト内に①でダウンロードした "GoogleMobileAds-v7.1.0.unitypackage" をインポートする

③下記サイトから "MainScene.cs" と "AppOpenAdManager.cs" をダウンロードする

④Unityのプロジェクト内に②でダウンロードした "MainScene.cs" と "AppOpenAdManager.cs" をインポートする

⑤広告を表示させたいシーン内に空のオブジェクトを作成し、④でインポートした "MainScene.cs" をアタッチする

■備考
実機テストで表示されない場合、新規プロジェクトを作成し、再度実装し直したところ表示されるようになりました。
原因は不明です。

参考環境:下記サイトの "complete" をダウンロードし、Unity Hubで開く

2.Google Play In-App Review APIの実装

①下記サイトから "com.google.play.review-1.8.0.unitypackage" をダウンロードする

②Unityのプロジェクト内に①でダウンロードした "com.google.play.review-1.8.0.unitypackage" をインポートする

③Unityのプロジェクト内に "Review" というC#スクリプトを作成し、中身を下記コードに置き換える

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Google.Play.Review;
using System;
using UnityEngine.Events;

public class Review : MonoBehaviour
{

    [Serializable] public class OnViewedEvent : UnityEvent { }
    [SerializeField] OnViewedEvent _onViewedEvent = default;

    public void StartRate()
    {
        if (Application.platform == RuntimePlatform.Android) {
            // Android
            StartCoroutine(ShowReviewCoroutine());
        } else {
            Debug.Log("Request Store Review is not supported");
            _onViewedEvent.Invoke();
        }
    }

    IEnumerator ShowReviewCoroutine()
    {
        // https://developer.android.com/guide/playcore/in-app-review/unity
        var reviewManager = new ReviewManager();
        var requestFlowOperation = reviewManager.RequestReviewFlow();
        yield return requestFlowOperation;
        if (requestFlowOperation.Error != ReviewErrorCode.NoError) {
            // エラーの場合はここで止まる.
            _onViewedEvent.Invoke();
            yield break;
        }
        var playReviewInfo = requestFlowOperation.GetResult();
        var launchFlowOperation = reviewManager.LaunchReviewFlow(playReviewInfo);
        yield return launchFlowOperation;
        playReviewInfo = null; // Reset the object
        if (launchFlowOperation.Error != ReviewErrorCode.NoError) {
            // エラーの場合はここで止まる.
            _onViewedEvent.Invoke();
            yield break;
        }
        _onViewedEvent.Invoke();
    }

    public void AddViewedCb(UnityAction cb)
    {
        _onViewedEvent.RemoveAllListeners();
        _onViewedEvent.AddListener(cb);
    }
}

④レビューを表示させたいシーン内に空のオブジェクトを作成し、③で作成した "Review.cs" をアタッチする

⑤レビューを表示させたいシーン内にUIのボタンを配置し、クリック時の動作に"Review.cs" の ”StartRate” を登録する

⑥④で作成したオブジェクト内の "Review.cs" にレビュー後の動作を登録する

■備考
実機テストで表示される条件
・Google Play Storeからダウンロードしたアプリのみ実機テスト可能
⇒Unity上の "ビルドして実行" では表示されません
・Google Play StoreのアカウントがGoogle Developersのアカウントであることを確認する
⇒Google Developers以外のテスターメンバーでは表示されなかった

参考サイト:

3.AdMobネイティブ広告の実装

下記動画に沿って実装を行う

※1 AdMobプラグイン/アドオンは下記を利用する

Google Mobile Ads Unity プラグイン

ネイティブ広告アドオン

※2 ネイティブ広告のスクリプトは下記を利用する

using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Api;
using System;

public class AdMobNative : MonoBehaviour
{
	private bool nativeAdLoaded;
	private NativeAd nativeAd;
	private string adUnitId;

	[SerializeField] GameObject adNativePanel;
	[SerializeField] RawImage adIcon;
	[SerializeField] RawImage adChoices;
	[SerializeField] Text adHeadline;
	[SerializeField] Text adCallToAction;
	[SerializeField] Text adAdvertiser;

	void Awake()
	{
		adNativePanel.SetActive(false); //hide ad panel
	}

	void Start()
	{
		//AndroidとiOSで広告IDが違うのでプラットフォームで処理を分けます。
		// 参考
		//【Unity】AndroidとiOSで処理を分ける方法
		// https://marumaro7.hatenablog.com/entry/platformsyoriwakeru

#if UNITY_ANDROID
		adUnitId = "ネイティブ広告ID";//ここにAndroidのネイティブ広告IDを入力
#elif UNITY_IPHONE
        adUnitId = "ネイティブ広告ID";//ここにiOSのネイティブ広告IDを入力
#else
        adUnitId = "unexpected_platform";
#endif

		RequestNativeAd();
	}

	void Update()
	{
		if (this.nativeAdLoaded)
		{
			this.nativeAdLoaded = false;

			Texture2D iconTexture = this.nativeAd.GetIconTexture();
			Texture2D iconAdChoices = this.nativeAd.GetAdChoicesLogoTexture();
			string headline = this.nativeAd.GetHeadlineText();
			string cta = this.nativeAd.GetCallToActionText();
			string advertiser = this.nativeAd.GetAdvertiserText();
			adIcon.texture = iconTexture;
			adChoices.texture = iconAdChoices;
			adHeadline.text = headline;
			adAdvertiser.text = advertiser;
			adCallToAction.text = cta;

			//register gameobjects
			nativeAd.RegisterIconImageGameObject(adIcon.gameObject);
			nativeAd.RegisterAdChoicesLogoGameObject(adChoices.gameObject);
			nativeAd.RegisterHeadlineTextGameObject(adHeadline.gameObject);
			nativeAd.RegisterCallToActionGameObject(adCallToAction.gameObject);
			nativeAd.RegisterAdvertiserTextGameObject(adAdvertiser.gameObject);

			adNativePanel.SetActive(true); //show ad panel
		}
	}

	private void RequestNativeAd()
	{
		AdLoader adLoader = new AdLoader.Builder(adUnitId)
			.ForNativeAd()
			.Build();
		adLoader.OnNativeAdLoaded += this.HandleNativeAdLoaded;
		adLoader.OnAdFailedToLoad += this.HandleNativeAdFailedToLoad;
		adLoader.LoadAd(new AdRequest.Builder().Build());
	}

	private void HandleNativeAdLoaded(object sender, NativeAdEventArgs args)
	{
		Debug.Log("Native ad loaded.");
		this.nativeAd = args.nativeAd;
		this.nativeAdLoaded = true;
	}

	private void HandleNativeAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
	{
		Debug.Log("Native ad failed to load: " + args.LoadAdError);
	}
}

4.AdMob収益受け取り方法

下記サイトを参考にお支払い方法を設定する

5.Gitの環境構築

下記サイトを参考にGitの環境構築を行う

Gitでプッシュするまでの手順は下記サイトを参考にする

また、コミットの際にviエディタが開いた際は、下記サイトの手順を参考に保存/閉じるを行う

6.NetlifyでHP制作

下記サイトを参考に環境を構築する
※Gitの環境構築は完了しているものとする

補足として、VScodeをインストールし、VScodeからリモートリポジトリにプッシュできるようにすると便利である

1
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
1
0