Skip to content

Instantly share code, notes, and snippets.

@ochoajulian
Last active January 11, 2021 16:29
Show Gist options
  • Save ochoajulian/53e7f981eca48d18420a6b16006cc43a to your computer and use it in GitHub Desktop.
Save ochoajulian/53e7f981eca48d18420a6b16006cc43a to your computer and use it in GitHub Desktop.
This script takes one UI Canvas array and one Cinemachine vCamera array. On player input, it cycles up or down through the array. Since they must always cycle together, a single private int (_currentCanvasAndVCam) is set for both arrays.
using UnityEngine;
using UnityEngine.InputSystem;
public class StartMenu : MonoBehaviour
{
public GameObject[] canvases = new GameObject[4];
public GameObject[] vCams = new GameObject[4];
private Vector2 _moveInput;
private int _currentCanvasAndVCam;
private void Start()
{
for (int i = 0; i < 4; i++)
{
canvases[i].SetActive(false);
vCams[i].SetActive(false);
}
_currentCanvasAndVCam = 0;
canvases[0].SetActive(true);
vCams[0].SetActive(true);
}
public void Move(InputAction.CallbackContext context)
{
_moveInput = context.ReadValue<Vector2>();
//context.performed checks only for OnButtonDown
if (context.performed)
{
NavigateMenus();
}
}
private void NavigateMenus()
{
//if player pushes up
if (_moveInput.y > 0.1)
{
_currentCanvasAndVCam++;
if (_currentCanvasAndVCam > 3)
{
_currentCanvasAndVCam = 0;
}
for (int i = 0; i < 4; i++)
{
canvases[i].SetActive(false);
vCams[i].SetActive(false);
}
canvases[_currentCanvasAndVCam].SetActive(true);
vCams[_currentCanvasAndVCam].SetActive(true);
}
//if player pushes down
else if(_moveInput.y < -0.1)
{
_currentCanvasAndVCam--;
if (_currentCanvasAndVCam < 0)
{
_currentCanvasAndVCam = 3;
}
for (int i = 0; i < 4; i++)
{
canvases[i].SetActive(false);
vCams[i].SetActive(false);
}
canvases[_currentCanvasAndVCam].SetActive(true);
vCams[_currentCanvasAndVCam].SetActive(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment