I notice is that your variable has a lower case “m” in “myGameInstance”… whereas your CallToUnity function expects it to be upper case… “MyGameInstance”.
The new issue you’re mentioning is easiest to work around by using window., for example, window.CheckAlert = function(){ alert("MessageReceived"); }. Then in your jslib, say window.CheckAlert(); …at least, this approach has solved the same kind of issue for me anyway.
Something else to be wary of in your original post… in case you still do it, you were expecting the game has always finished loading within 5 seconds. Your timeout may fire before the game has loaded though, in which case that would also likely throw an exception. You could move your setTimeout into the “then” part of your loading, so it doesn’t start counting down until the game has loaded first.
The place where it says loadingBar.style.display = "none"; is where it hides the loading bar because the game has finished loading, so anywhere immediately before or after that line should be appropriate. However, you might want to do it from C# instead (through your .jslib) to be sure any in-game initialization has been done beforehand as well. Otherwise there’s still a chance the game has loaded but maybe the browser has lost focus of the tab/window and stops processing it at the rate you’re expecting, or the Unity logo is still displaying because some kind of other initialization took a bit longer than expected… etc.
Depends on what exactly you want to do - you could search for examples of how to do that in particular… but if you want C# functions to be called from events in JavaScript, as far as I know you have to add your listeners in JS then use SendMessage from those. You can add those listeners either in your page or in your .jslib, for example if you wanted to listen for a click event on a specific DOM element. Given the choice, keeping as much in the .jslib as possible helps make it easier to switch between templates without breaking things. You can access whatever scripts are loaded in the page from your .jslib the same way as usual, but might find it doesn’t always work (or work the same) without adding window. before some things. For example in the page, maybe you call eval(some_js_code), but in .jslib you would need to say window.eval(some_js_code) or it won’t execute within the expected scope.
Edit: Also, one last tip… if you’re going to add lots of functions and data to window, you might consider creating an object to hold all of that, i.e. at startup, window.mydata = {}; then later you do like window.mydata.something =… instead of directly like window.something =… This way it’s much easier to debug with console (you can just type “window.mydata.” and it auto-suggests/drops down a list showing all the stuff you’ve added), also this way keeps the window object tidy and makes resetting/clearing your extra stuff easy by just setting window.mydata = {} again
Hey!
Late reply, but just dropping this here - maybe it’ll be useful for someone.
You can send and receive messages between Unity and HTML from anywhere using this tool: WebPontis: Easy WebGL & WebGPU Integration | Integration | Unity Asset Store
It simplifies everything and integrates Unity into the JavaScript event system.