How to check is the game loaded in index.html?
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
[if(gameinstance.MethodToGetIsTheGameLoaded())...][1]
is there such method?
Answer by Hellium · Jul 06, 2018 at 11:31 AM
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 ) ;
If this answer helped you solve your problem, would you please accept it for future readers? (Just click on the "Accept" button at the bottom of the answer). Thanks in advance, and good luck !
Sure :) Also It would be very good if you link me some guide how to create .jslib (I wrote my functions in index.html and use Application.ExternalCall() ). I saw that I have to create .jslib, I understand how to call functions by [DllImport("__Internal")] from C#, but don't understand, how to create .jslib file and associate it with my game, with code which is generated in index.html by Unity or with GameInstance and etc. So I need to understand the process of creating such a file and adding it to the project. Somehow can't find a guide on youtube.
Your answer
Follow this Question
Related Questions
Gear VR Loading screen freeze 0 Answers