Making Dialogue System find the VRTK Camera
Posted: Mon Jul 02, 2018 12:18 am
My game is (currently) for the HTC Vive (will be launching to other VR headsets once the core gameplay is complete). Anyway, using the VR Toolkit (VRTK) means that the VR camera is not ready by the time the Dialogue System scripts begin execution. Even changing the scripts in the Script Execution Order in the project settings don't help. So, I've changed the AssignMainCameraToCanvas class which comes with your VR example in the following way: it begins a repeating method call every second and does its main bit once it finds a ready VR camera, making it work successfully with VRTK.
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:
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;
}
}
}
}