Skip to content

Commit

Permalink
Fix a bunch of in-game Canvas UIs (#20)
Browse files Browse the repository at this point in the history
* Show dialogue canvas in VR

* Show button prompts

* Ability to set scale of each canvas
  • Loading branch information
Raicuparta authored Jan 7, 2020
1 parent 7c19d07 commit 55375a4
Showing 1 changed file with 59 additions and 23 deletions.
82 changes: 59 additions & 23 deletions NomaiVR/Modules/Menus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,85 @@ namespace OWML.NomaiVR
{
public class Menus : MonoBehaviour
{
Camera _camera;

// List all the canvas elements that need to be moved to world space during gameplay.
static readonly CanvasInfo[] _canvasInfos = {
new CanvasInfo("PauseMenu", 0.0005f),
new CanvasInfo("DialogueCanvas"),
new CanvasInfo("ScreenPromptCanvas", 0.0015f),
};

void Start() {
NomaiVR.Log("Start Menus");

SceneManager.sceneLoaded += OnSceneLoaded;
MoveAllCanvasesToWorldSpace();

// Main menu camera
_camera = GameObject.FindObjectOfType<Camera>();
FixMainMenuCanvas();
}

void OnDisable() {
SceneManager.sceneLoaded -= OnSceneLoaded;
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
MovePauseMenuToWorldSpace();
_camera = GameObject.Find("PlayerCamera").GetComponent<Camera>();
FixGameCanvases();
}

void MoveAllCanvasesToWorldSpace() {
Canvas[] canvases = Object.FindObjectsOfType<Canvas>();
void MoveCanvasToWorldSpace(CanvasInfo canvasInfo) {
GameObject canvas = GameObject.Find(canvasInfo.name);

foreach (Canvas canvas in canvases) {
canvas.renderMode = RenderMode.WorldSpace;
Transform originalParent = canvas.transform.parent;
canvas.transform.parent = Object.FindObjectOfType<Camera>().transform;
canvas.transform.localPosition = new Vector3(0, 0, 1);
canvas.transform.localRotation = new Quaternion(0, 0, 0, 1);
canvas.transform.localScale = Vector3.one * 0.0005f;
if (canvas == null) {
NomaiVR.Log("Couldn't find canvas with name: " + canvasInfo.name);
return;
}
}

void MovePauseMenuToWorldSpace() {
GameObject pauseMenu = GameObject.Find("PauseMenu");
NomaiVR.Helper.Console.WriteLine("pauseMenu: " + pauseMenu.name);
Canvas[] canvases = pauseMenu.GetComponentsInChildren<Canvas>();
Camera playerCamera = GameObject.Find("PlayerCamera").GetComponent<Camera>();
Canvas[] subCanvases = canvas.GetComponentsInChildren<Canvas>();
NomaiVR.Log("subcanvases: " + subCanvases.Length);

foreach (Canvas canvas in canvases) {
canvas.renderMode = RenderMode.WorldSpace;
foreach (Canvas subCanvas in subCanvases) {
subCanvas.renderMode = RenderMode.WorldSpace;
subCanvas.transform.localPosition = Vector3.zero;
subCanvas.transform.localRotation = Quaternion.identity;
subCanvas.transform.localScale = Vector3.one;
}

pauseMenu.transform.parent = playerCamera.transform;
pauseMenu.transform.localPosition = new Vector3(-0.6f, -0.5f, 1);
pauseMenu.transform.localEulerAngles = new Vector3(0, 0, 0);
pauseMenu.transform.localScale = Vector3.one * 0.001f;
canvas.transform.parent = _camera.transform;
canvas.transform.localPosition = canvasInfo.offset;
canvas.transform.localEulerAngles = new Vector3(0, 0, 0);
canvas.transform.localScale = Vector3.one * canvasInfo.scale;
}

void FixMainMenuCanvas() {
MoveCanvasToWorldSpace(new CanvasInfo("TitleMenu", 0.0005f));
}

void FixGameCanvases() {
foreach (CanvasInfo canvasInfo in _canvasInfos) {
MoveCanvasToWorldSpace(canvasInfo);
}
}
protected class CanvasInfo
{
public string name;
public Vector3 offset;
public float scale;
const float _defaultScale = 0.001f;

public CanvasInfo(string _name, Vector3 _offset, float _scale = _defaultScale) {
name = _name;
offset = _offset;
scale = _scale;
}

public CanvasInfo(string _name, float _scale = _defaultScale) {
name = _name;
offset = new Vector3(0, 0, 1);
scale = _scale;
}
}
}
}

0 comments on commit 55375a4

Please sign in to comment.