3

I'm using a Unity WebGL App in a Bootstrap modal (dialogbox) of a website. As long as this modal isn't visible (display:none), there is always this error in the console log: "Screen position out of view frustum". How can I handle or avoid this error?

Patrick Münster
  • 459
  • 2
  • 5
  • 17

2 Answers2

3

The code for my solution is very simple. In Unity I've written a C# script which gets the camera as public variable mainCamera and two functions disableCamera() and enableCamera(). Cause my WebGL View is hidden on startup, I set the camera by calling disableCamera() in the Start() function. Just assign your main camera to the mainCamera variable by drag and drop in the Unity editor. Drag this Script to any object in the scene. For me, the object is a Canvas called Controller.

[Header ("Camera")]
public Camera mainCamera;

// Use this for initialization
void Start () {
   disableCamera();     
}

public void disableCamera() {
    mainCamera.enabled = false;
}

public void enableCamera() {
    mainCamera.enabled = true;
}

On my website I've loaded the Unity WebGL App and call the function enableCamera() from JavaScript whenever I need it. My Unity WebGL app is loaded like that:

 var myApp = UnityLoader.instantiate("myDivContainerID", "pathToUnityBuild.json", etc....);

Now you can call the function to activate or deactivate the camera by using the SendMessage methode of the GameInstance (myApp). It can call a function of a script assigned to an object in Unity. In this case it's called Controller:

myApp.SendMessage('Controller','enableCamera');

SendMessage accepts 3 parameters, so you can call a function with a value if you want. E.g.:

myApp.SendMessage('Controller','enableCamera', 'true');

And that's it!

Patrick Münster
  • 459
  • 2
  • 5
  • 17
1

Ok, I've got a dirty solution for this issue in my case. I've written a function in Unity to disable my main camera. So every time I'm hiding the container with the Unity WebGL (modal or dialogbox, etc.), I'm going to call this function do disable the main camera from JavaScript and the error disappears. By showing the container (modal or dialogbox, etc.) I'm simply enabling the camera again. Not an elegant solution but works for me. But I would appreciate a better solution.

Patrick Münster
  • 459
  • 2
  • 5
  • 17
  • Hey, weirdly I've run into this at almost the exact same time. Actually my team is on the web app side with an outside party providing the WebGL. Do you mind sharing both sides of your workaround code and I'll see about getting you some more upvotes?! :) Seriously this post is the only relevant hit anywhere! – conorom Mar 22 '18 at 14:50
  • Yes, of course! I did not expect that anyone needed this "dirty" solution. But I haven't found a better one till today. Cause of performance issues I've switched to **PlayCanvas** anyway. – Patrick Münster Mar 23 '18 at 23:27