質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2297閲覧

[Unity]アプリのバージョンが取得できない

Ponkiti27

総合スコア9

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/09/30 07:57

前提・実現したいこと

アプリの最新バージョンがあるかどうかチェックし、ストアに誘導するために、プラットホームをAndroidに変更した状態で、現在使用しているアプリのバージョンを取得したいです。

発生している問題・エラーメッセージ

アプリバージョンを取得するために Application.version とういコードを使用したのですが、バージョンを取得することができません(Editor上でしたまだ試せていないため、実機でも取得でききないかは分かりません)。
プラットホームをIOSに変更した場合は正常に Application.version でアプリバージョンの取得ができています。

Application.versionを使用できる状態にするか、その以外の方法でどうにかして現在のアプリバージョンを取得したいです。

該当のソースコード

c#

1private void VersionCheckAndroid() 2 { 3 var url = string.Format("https://play.google.com/store/apps/details?id={0}", Application.identifier); 4 5 VersionNum.GetValueAsync().ContinueWith(task => { 6 if (task.IsFaulted) 7 { 8 // Handle the error... 9 } 10 else if (task.IsCompleted) 11 { 12 DataSnapshot snapshot = task.Result; 13 string andVer = snapshot.Child("Android").GetValue(true).ToString(); 14 if (VersionComparative(andVer)) 15 { 16 StoreURL = url; 17 GameManager.playStageBGM(0); 18 UIView[8].SetActive(true); ; 19 } 20 } 21 }); 22 } 23 24bool VersionComparative(string storeVersionText) 25 { 26 if (string.IsNullOrEmpty(storeVersionText)) 27 { 28 return false; 29 } 30 try 31 { 32 var storeVersion = new Version(storeVersionText); 33 var currentVersion = new Version(Application.version); 34 35 if (storeVersion.CompareTo(currentVersion) > 0) 36 { 37 return true; 38 } 39 } 40 catch (Exception e) 41 { 42 Debug.LogErrorFormat("{0} VersionComparative Exception caught.", e); 43 } 44 return false; 45 }

エラーコード

UnityEngine.UnityException: get_version can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. at (wrapper managed-to-native) UnityEngine.Application.get_version() at StageSelectCon.VersionComparative (System.String storeVersionText) [0x00019] in D:\UnityWork\SpatialBall_2\Assets\Script\System\StageSelectCon.cs:740 VersionComparative Exception caught. UnityEngine.Debug:LogErrorFormat(String, Object[]) StageSelectCon:VersionComparative(String) (at Assets/Script/System/StageSelectCon.cs:749) <>c__DisplayClass82_0:<VersionCheckAndroid>b__0(Task`1) (at Assets/Script/System/StageSelectCon.cs:709) System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()

試したこと

以下のコードを追加したところ
Debug.Log(andVer);
Debug.Log(Application.version);
Debug.Log(andVer);
最初のandVerは出力され Application.version以下のコードが実行されなくなることから、Application.versionがエラーの原因ということが分かったのですが、Application.version以外の方法でアプリバージョンを取得する方法が分かりません。

補足情報(FW/ツールのバージョンなど)

Unity 2019.3.15f1
windiws10 64bitを使用しています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

FireBaseのリアルタイムデータベースからアプリの最新バージョンを取得して、それをアプリの現在のバージョンと比較することでアップデート通知のするかを判断していたのですが、FireBaseのリアルタイムデータベースの値を使う部分

private void VersionCheckAndroid() { var url = string.Format("https://play.google.com/store/apps/details?id={0}", Application.identifier); VersionNum.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { //ここでリアルタイムデータベースの値を使った処理を行う DataSnapshot snapshot = task.Result; string andVer = snapshot.Child("Android").GetValue(true).ToString(); if (VersionComparative(andVer)) { StoreURL = url; GameManager.playStageBGM(0); UIView[8].SetActive(true); ; } } }); }

その部分で使うことができない関数あり、その中にApplication.versionが含まれていたみたいです。
ですので、それ以外の部分で使用できない関数の処理を行うことにしました。それで無事に解決できました。

解決したコードを以下に記しておきます。

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using System; using UnityEngine.Purchasing; using HtmlAgilityPack; using Firebase; using Firebase.Unity.Editor; using Firebase.Database; private string AppVersion; private bool UpdateFlag = false; private DatabaseReference VersionNum; private void Start()   {          FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://spatialball.firebaseio.com/");     AppVersion = Application.version;     VersionNum = FirebaseDatabase.DefaultInstance.GetReference("VersionNum");     VersionCheckAndroid();   } private void FixedUpdate() { if (UpdateFlag) { UpdateFlag = false; OpenUpdateView(); } } private void VersionCheckAndroid() { var url = string.Format("https://play.google.com/store/apps/details?id={0}", Application.identifier); VersionNum.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) {          //ここでリアルタイムデータベースの値を使った処理を行う          DataSnapshot snapshot = task.Result; string andVer = snapshot.Child("Android").GetValue(true).ToString(); if (VersionComparative(andVer)) { StoreURL = url; UpdateFlag = true; } } }); } bool VersionComparative(string storeVersionText) { if (string.IsNullOrEmpty(storeVersionText)) { return false; } try { var storeVersion = new System.Version(storeVersionText); var currentVersion = new System.Version(AppVersion); if (storeVersion.CompareTo(currentVersion) > 0) { return true; } } catch (Exception e) { Debug.LogErrorFormat("{0} VersionComparative Exception caught.", e); } return false; }

投稿2020/09/30 17:52

Ponkiti27

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問