- Home /
Stop autoplay in WebGL
Hello, I am building my website and want to put a WebGL game which I built a few days ago. However, once I embedded it in my website(and got it to run locally(Chrome sucks)), it started playing as soon as I launched the site. How do I stop that autoplay? I tried inspecting one of itch.io's games but to no avail. Does anyone know how to do that?
Answer by Bunny83 · Jun 28, 2020 at 04:18 PM
This has nothing to do with Unity but with how you setup your website. The way how the Unity player is embedded has changed drastically over time. If I look at one of my latest WebGL builds it's usually this line inside the HTML:
<script src="Release/UnityLoader.js"></script>
as that loader script essentially kicks of the actual player with the method at the very end of that file. So you have to insert / load this script dynamically through some javascript on that webpage. Again, this has nothing to do with Unity. You can't have Unity to react to anything if it isn't started yet.
Here's a small example how you can dynamically load a script through javascript. Though instead of loading the "script" tag into the "head" of the site you usually want to load it into the "body". Of course regardless of the loading script you could create the actual container div and the module config also dynamically. However that depends on what you actually need / want.
Note that in older Unity versions there was an explicit "loading line" like this one:
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/WebGL.json");
</script>
which actually kicked off the player. On my Github pages I have several WebGL examples. However they all start automatically and I barely even edit the actual HTML generated by Unity since they are just examples. If you want to embed your player in your page that's up to you. The generated HTML file essentially gives you "an example". Most probably just load a new page with the player on instead of manipulating the current page dynamically. Though if that's what you're looking for, you have to do this yourself in javascript.
Your answer
Follow this Question
Related Questions
WEB GL optimization checkbox level disappeared from Build settings in 5.4? 1 Answer
How can I get a string to work as the key to a dictionary filled with the text of a www object? 0 Answers
Virtual Joysitck for WebGL???? 0 Answers
WebGL build crashes but game runs fine in editor and windows build. 0 Answers
WebGL: Interacting with browser scripting unable to call java script method from C# 2 Answers