Ortaz
1
How to check is the gameInstance in index.html is loaded?
I have code:
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webGLDom.json", {onProgress: UnityProgress});
function SendToPlayer(func,what){
gameInstance.SendMessage("SocialManager",func,what);
}
function handleVisibilityChange() {
if (!document.visibilityState == "hidden") {
SendToPlayer("OnGameVisible","true");
}
}
document.addEventListener('visibilitychange', handleVisibilityChange, false);
But If I open another tab when the game is loading, I have an error. So I need to except such situation by adding code
![120241-безымянныи.png|997x782](upload://o4eKAh4wbLF7ye22gwynTcbTrmX.png)
is there such method?
1st solution : JS-side only
var gameIsRunning = false ;
var settings =
{
onProgress: UnityProgress,
Module:
{
preRun: [ function() { console.log("About to run....") ; } ],
postRun: [ function() { console.log("The game is running!") ; gameIsRunning = true ; } ],
}
};
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webGLDom.json", settings ) ;
postRun
may be called too soon. You may want the second solution
2nd solution : plugin JS + call function in Awake
// gameStateController.jslib, in your Unity plugins
mergeInto(LibraryManager.library,
{
OnGameStarted: function ()
{
window.gameIsRunning = true ;
}
}
// GameStateController.cs, in your Unity scripts
public class GameStateController : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void OnGameStarted();
private void Awake()
{
OnGameStarted() ;
}
}
// In your JS code
window.gameIsRunning = false ;
var settings =
{
onProgress: UnityProgress
};
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webGLDom.json", settings ) ;