Lately I’ve been working on a Unity3D WebGL application and one of the client’s requests was to load a configuration file at startup.
Usually this means the client has no idea where and when this file will be published (i.e.: no hard coding), so the best way to solve this is to pass the url from the HTML wrapper.
SendMessage ("aGameObject", "aMethod", "aParameter");
aGameObject parameter is a GameObject in the scene including the method (you’ll have to add the full hierarchy path in case it has one or more parents). The example below shows a game object with a single parent:
aParameter parameter is a single string value that will be passed as a parameter for
Waiting for the Application Load
There’s a small caveat… if the WebGL application hasn’t been fully loaded, an error will be raised: in my case this will happen all the times as I need the parameter immediately.
The best solution is to request the method call directly from Unity (for instance, from a MonoBehavior
Just to be clear, here’s what’s happening:
that logs some message in the console, calls the
SetAppInitUrl() method to a gameobject called AppController.
The called gameobject will have this script attached:
SetAppInitUrl() method for the callback.
As you’ll have noticed, this kind of messaging is dependent on strings that may change (I’m used to rename things in the scene a lot…).
as a solution, you can pass the gameobject name directly from Unity. For instance: