So when I **build to webgl in Unity version 5.6.0b1 **, after the application is running in my browser,
If I right click anywhere within the game canvas, the browser(chrome) pulls up a small context menu
I can’t figure out how to turn this off, I don’t know what is causing this.
There’s also some weird pointer behavior when I left-click and drag: the application’s cursor looks like you’re selecting text, or like you’re dragging selected text.
I’ve never had this issue with version 5.4, or earlier.
Steps to replicate:
open newest version of unity beta 5.6.0b1
start a new project. Include Webgl. Default build settings.
build to Webgl and Run.
then from here, right clicking is causing me this issue.
This turns out to be a version specific issue, it should be resolved in the upcoming release.
But still, there is a way to fix this this issue.
Within the index.html file generated by Unity, you will find some javascript code which instantiates a new unity game instance.
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/your-build.json", {onProgress: UnityProgress});
Now we modify the parameters which are passed to the gameInstance.
We want to set an onRuntimeInitialized function, which, when called on runtime, will add an event listener to prevent the context menu from appearing.
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/your-build.json", {onProgress: UnityProgress, Module: {
onRuntimeInitialized: function () {
this.canvas.addEventListener("contextmenu", function (e) {
e.preventDefault(); });
},
}});
Make sure to set "Build/your-build.json", to the appropriate filepath for your build. [line 1 in above code]
My thanks to user alexsuvorov on the unity forums for teaching me this. I’ve essentially coppied out his response from the Unity Forums, and I couldn’t have learned this without him.