code ソーシャル

Unityアプリcodetips(54)

スポンサーリンク

//tips

//unityで発生するLibrary/PackageCache/について

Unity公式ページで確認したところMacOS: ~/Users/Library/Unity/cacheでのエラーの場合、パッケージインストールが失敗しており、失敗要因は許可の問題が原因である可能性あると指摘している。

https://docs.unity3d.com/ja/2019.4/Manual/upm-errors.html

自分のプロジェクトファイルを確認したところproject > assets >unity adsの中にUnityAdsのパッケージ自体は存在しているのでデータ自体はインストールができていると考える。

下記のようにプロジェクトのバージョンが異なる場合はプロジェクト自体を新たに作り直す方法がある。

All right i think i found the solution. I download 2019.2.12f1 and run old project and got that error:
1-go to old project folder -> library ->packagecase
2-open unity hub with new version ->create new project
3- open new project folder -> library -> packagecase ->ctrl +A
4-delect all folders in packagecase in old project -> ctrl + v and yeah that will work
5- you can delece new project if u want

私の場合は、2019.4.1fで変わらずなので効果はない。

一旦、フォルダのUnityAds一式を削除してみたところ

Assets/script/ads/testads.cs(20,13): error CS0103: The name 'Advertisement' does not exist in the current context

が表示され、Advertisement.IsReady()の部分がエラーとして新たに表示された。

これからAdvertisementとしての役割は果たしていたことがわかる。

Advertisementのバージョンを2020年1月まで古くして取得、Unityを再起動するとエラーが発生せずに、起動完了。

次にバージョンを最新にして再度再起動したところこちらも無事に起動完了。

エラーの要因は、おそらくasset storeから過去にダウンロードしたUnityADsであったと推測できた。

Advertisementは認識されているので下記コードを実行するが、やはり何も表示しない。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;

public class testads : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))

{
FunctionTest();
}

}

void FunctionTest()
{

if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
}

やはりUnityDashboardが連動を認識しておらず、それがGameIDsの表示がない要因なのではないかと考える。

 

※UnityDashboard内に間違って作ったプロジェクトの消し方はDevelop→projectクリック→setting→general。その一番下にあるarchiveをクリックすると消せる。

https://qiita.com/NIGHTNOISE/items/7f869059a6b17c585a2e

Unity のサービスを始めるには、プロジェクトを Unity Services Project ID に紐づける必要がある。Unity Services Project ID は、すべての Unity Services で使用されるオンラインID で、Unity 内の Services ウィンドウで生成可能。

Create a Unity Project IDで設定する。

設定ができているか確認するためには、ツールバーのクラウドボタンをクリックする。もしプロジェクトが Services ID に紐づけられていない場合は、Create a Unity Project IDを要求する画面が出てくる。

https://docs.unity3d.com/ja/2019.4/Manual/SettingUpProjectServices.html

表示されるservicesのsettingでunity project IDが確認できる。

色々調べるうちにgameIDの記載場所が変更されていることがわかった。

Operate→project選択→monetization→placements、このplacementsに各種idが表示されている。

下記を追加添付して再度テスト。

using UnityEngine;
using UnityEngine.Advertisements;//Advertisementクラスを使うのに必要

public class testads2 : MonoBehaviour
{

private void Start()
{
//ゲームIDをAndroidとそれ以外(iOS)で分ける
#if UNITY_ANDROID
string gameID = "3810449";
#else
string gameID = "3810448";
#endif

//広告の初期化
Advertisement.Initialize(gameID);
}

private void Update()
{
//広告が使えるようになったかの確認
Debug.Log(Advertisement.IsReady());
}

}

こちらで無事表示できるようになった。

Sceneにスクリプトを追加する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;

public class testads : MonoBehaviour
{
private void Start()
{
//ゲームIDをAndroidとそれ以外(iOS)で分ける
#if UNITY_ANDROID
string gameID = "3810449";
#else
string gameID = "3810448";
#endif

//広告の初期化
Advertisement.Initialize(gameID);
}

public void FunctionTest()
{

if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
}

今のままでは広告表示しかできないので、操作性を高め、Advertiseのクローズボタンとポイント付与の連動を考える。

下記のようなスクリプトを追加して検証を行なったが動作が確認できない。

if (Advertisement.IsReady("rewardedVideoZone"))
{
ShowOptions options = new ShowOptions
{
resultCallback = Result
};
Advertisement.Show("rewardedVideoZone", options);
}

private void Result(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("広告表示成功");
// ここで報酬をユーザーに付与する
break;
case ShowResult.Skipped:
Debug.Log("スキップされました");
break;
case ShowResult.Failed:
Debug.LogError("失敗しました。");
break;
}
}

 

https://docs.unity3d.com/540/Documentation/ScriptReference/Advertisements.Advertisement.IsReady.html

“rewardedvideo”の部分を抜こうとしてもうまくいかないので、“rewardedvideo”をうまく処理する必要がある。

“After some searching I found the answer myself. (I guess I didn't search well enough.) For the people that want to know: Go to UnityAds. Click on AndroidGooglePlay (or appstore where ever you need it on). And then set the rewardedvideo to enabled (it is enabled when it says yes). And that's it. NOTE: the components between quotation marks need to be the same as your integration id. So in my case (since my integration id is rewardedVideo)”

“Try with "rewardedVideo" zone id instead. ”

https://forum.unity.com/threads/advertisement-isready-rewardedvideozone-is-always-false.382149/?fbclid=IwAR3f7hrbKI8Ouly6L_O3olKWQK7zrGKQ91uwdrkGF8eh6wCJWshS7_r-QMo

またresultCallbackもうまく使用されていないという表示が出ているのでこちらの使い方も確認する。

さらに、参考にしているコードがunityの旧式のものが多いので現在のものに合っていない可能性がある。

いくつかのサンプルコードを試したところ下記のものでresultcallbackが成功した。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class testads3 : MonoBehaviour
{
private void Start()
{
//ゲームIDをAndroidとそれ以外(iOS)で分ける
#if UNITY_ANDROID
string gameID = "3810449";
#else
string gameID = "3810448";
#endif

//広告の初期化
Advertisement.Initialize(gameID);
}

public void ShowRewardedAd()
{
Debug.Log(Advertisement.IsReady());

if (Advertisement.IsReady("rewardedVideo"))
{
ShowOptions options = new ShowOptions
{
resultCallback = Result
};
Advertisement.Show("rewardedVideo", options);
}
}

private void Result(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("広告表示成功");
// ここで報酬をユーザーに付与する
break;
case ShowResult.Skipped:
Debug.Log("スキップされました");
break;
case ShowResult.Failed:
Debug.LogError("失敗しました。");
break;
}
}
}

次のそもそものテスト用の広告のクローズボタンの設定を変更できるかを確認する。

広告の設定はunityのダッシュボードの方ででき、gameidが書いてあるページの右側のrewardvideoをクリックすると詳細設定ができる画面に移行できる。

https://gist.github.com/yasuyuki-kamata/bb68dbcda103a28f23d4

現段階では、rewardvideoの閉じるボタンを操作できないため、広告が表示されたらポイントが加算されるようにする。

新たな広告の追加は、gameidが表示されるページの右上にあるAdd Placementボタンでできる。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class testads3 : MonoBehaviour
{
public int point_num;

private void Start()
{
//ゲームIDをAndroidとそれ以外(iOS)で分ける
#if UNITY_ANDROID
string gameID = "3810449";
#else
string gameID = "3810448";
#endif

//広告の初期化
Advertisement.Initialize(gameID);
}

public void ShowRewardedAd()
{
Debug.Log(Advertisement.IsReady());

if (Advertisement.IsReady("rewardedVideo"))
{
ShowOptions options = new ShowOptions
{
resultCallback = Result
};
Advertisement.Show("rewardedVideo", options);
}
}

private void Result(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("広告表示成功");
// ここで報酬をユーザーに付与する

point_num = PlayerPrefs.GetInt("CPoint", 2);
point_num += 1;

PlayerPrefs.SetInt("CPoint", point_num);
PlayerPrefs.Save();
//PlayerPrefs.DeleteKey("CPoint"); //CPointのデータのみ初期化

break;
case ShowResult.Skipped:
Debug.Log("スキップされました");
break;
case ShowResult.Failed:
Debug.LogError("失敗しました。");
break;
}
}
}

 

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.