I'm certain this is not the ideal way to do it, but at the moment I'm not sure that VRTK provides a "I'm ready" Unity Event hook for me to listen to. I'm asking in their Slack but haven't heard back yet. This method, though crude, will work for now.
Here's the changed code:
Code: Select all
using UnityEngine;
using UnityEngine.SceneManagement;
namespace PixelCrushers.DialogueSystem.Demo
{
/// <summary>
/// Add this to any world space or screen space - camera canvas to
/// make it automatically assign the main camera to itself.
/// </summary>
public class AssignMainCameraToCanvas : MonoBehaviour
{
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
AssignMainCamera();
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene arg0, LoadSceneMode arg1)
{
InvokeRepeating ("AssignMainCamera", 0.0f, 1.0f);
}
public void AssignMainCamera()
{
if (Camera.main) {
CancelInvoke ("AssignMainCamera");
var canvas = GetComponent<Canvas> ();
if (canvas == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay || canvas.worldCamera != null) {
return;
}
canvas.worldCamera = Camera.main;
}
}
}
}