解決したい事
エラーを正してWebGLが起動するようにしたい。
発生している問題
Unityバージョンを
更新前:2019.4.13f1
更新後:2021.3.5f1
に更新した後、WebGL形式でビルドし、chromeで起動したところ、以下のようなエラーにより起動できなくなりました。
(ビルド自体はエラーなく完了します、ブラウザ起動時にブラウザ側でエラーが発生して読み込みが完了しません)
exception thrown: ReferenceError: unityInstance is not defined,ReferenceError: unityInstance is not defined at _StringReturnValueFunction
Uncaught (in promise) ReferenceError: unityInstance is not defined at _StringReturnValueFunction
warning: 2 FS.syncfs operations in flight at once, probably just doing extra work
原因のスクリプトを排除してビルドしたところ問題なく起動できたため、このスクリプト(正確にはjslib内の記述)が原因だと思います。
エラー原因と思われるスクリプトはC#スクリプトからjslibスクリプトを呼び出し、OSの種類を読み取ってC#スクリプトに返す仕組みとなっています。
cs
1using UnityEngine; 2using System.Runtime.InteropServices; 3 4 public class ReadJS : MonoBehaviour 5 { 6 public GameObject[] PCMode_OnlyObjects; 7 public GameObject[] MobileMode_OnlyObjects; 8 9 [SerializeField] 10 private bool isDebugDeviceChecke = false; 11 public enum deviceMode 12 { 13 PC, Mobile, Else, 14 } 15 public static deviceMode DeviceMode; 16 17 [DllImport("__Internal")] 18 private static extern void StringReturnValueFunction(); 19 20 void Awake() 21 { 22 #if UNITY_EDITOR || UNITY_STANDALONE 23 SetPC(); 24 #endif 25 26 #if UNITY_WEBGL && !UNITY_EDITOR 27 StringReturnValueFunction(); 28 #endif 29 } 30 void MobileModeActive() 31 { 32 foreach(var ob in PCMode_OnlyObjects) 33 { 34 if(ob != null) 35 { 36 ob.SetActive(false); 37 } 38 } 39 foreach(var ob in MobileMode_OnlyObjects) 40 { 41 if(ob != null) 42 { 43 ob.SetActive(true); 44 } 45 } 46 } 47 48 void PCModeActive() 49 { 50 foreach(var ob in PCMode_OnlyObjects) 51 { 52 if(ob != null) 53 { 54 ob.SetActive(true); 55 } 56 } 57 foreach(var ob in MobileMode_OnlyObjects) 58 { 59 if(ob != null) 60 { 61 ob.SetActive(false); 62 } 63 } 64 } 65 66 public void ChangeController() 67 { 68 if(DeviceMode == deviceMode.PC) 69 { 70 DeviceMode = deviceMode.Mobile; 71 SetMobile(); 72 } 73 else if(DeviceMode == deviceMode.Mobile) 74 { 75 DeviceMode = deviceMode.PC; 76 SetPC(); 77 } 78 } 79 80 void SetPC()//CheckOS.jslib側で呼び出す 81 { 82 if(isDebugDeviceChecke) 83 Debug.Log("This Devis is PC."); 84 85 DeviceMode = deviceMode.PC; 86 87 if(MobileMode_OnlyObjects != null) 88 PCModeActive(); 89 } 90 91 void SetMobile()//CheckOS.jslib側で呼び出す 92 { 93 if(isDebugDeviceChecke) 94 Debug.Log("This Devis is Mobile."); 95 96 DeviceMode = deviceMode.Mobile; 97 98 if(MobileMode_OnlyObjects != null) 99 MobileModeActive(); 100 } 101 } 102
js
1mergeInto(LibraryManager.library, { 2 3 StringReturnValueFunction: function () { 4 var returnStr = window.navigator.userAgent.toLowerCase(); 5 6 if(String(returnStr).indexOf("windows") != -1 || String(returnStr).indexOf("windows") != -1 ) 7 { 8 unityInstance.SendMessage('DeviceManager', 'SetPC'); 9 }else 10 { 11 unityInstance.SendMessage('DeviceManager', 'SetMobile'); 12 } 13 }, 14 });
試したこと
エラーに関して調べたところ、1つ目のエラーはこちらの記事に倣って、Player Settings -> Publish Settings -> Decompression Fallbackにチェックを入れました。
2つ目のエラーは1つ目のエラーが発生することで表示される内容だと思います。
3つ目のエラーに関しては調べたところ、ファイルダイアログシステムを使うと出るという記事を散見しましたが、
自分の方では使用していませんが、OSを読み取りに行かせている部分か、unityInstanceの書き方がバージョンアップによって出力形式が変更されたことで問題となっているのかなぁと思っています。
参考記事
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。