Search Unity

How can I show/Hide a UI popup menu in Unity with a button press?

Discussion in 'VR' started by KenniKure, Sep 3, 2019.

  1. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    Hi forum
    (I am a noob at Unity and C# scripting)

    I have been working on a UI menu in Unity for a while now. I have tried writing some scripts but they have not been succesfull. Can anyone help me? I have made the menu look like an Ipad that is supposed to popup when a joystickbutton is pressed. It seems like the scripts doesnt recognice the gameobject?
    upload_2019-9-3_8-13-38.png
    The menu is a in a prefab of the Ipad model.

    Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class MenuAppear : MonoBehaviour
    {
    public Gameobject Canvas;
    private bool isShowing;

    // Update is called once per frame
    void Update() {
    if(Input.GetKeyDown(KeyCode.JoystickButton1))
    isShowing = !isShowing;
    Canvas.SetActive(isShowing);
    }
    }
     
  2. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    I got the canvas to open and close with buttonpress on X o the keyboard!
    Does anybody know how I can make the menu show in front of me (and scale it down when it opens in front of me?)
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Move your Canvas object inside your Player object (in the Hierarchy tab). Then, however the player moves, your canvas will move too.
     
  4. Corysia

    Corysia

    Joined:
    Mar 14, 2018
    Posts:
    108
    FrontEnd Fanatics on YouTube has a good video on Debugging for the Oculus Quest. In it, he uses a free asset called Graphy, which he hide/shows to the Oculus Start button (left hand menu button). It's in the latter part of the video, but he goes over Screen/World space, placing it near but not on top of you...which camera to use, etc.
     
    JoeStrout likes this.
  5. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    I found out how to do it now :) Thanks! Now the menu is a reasonable size in front of the player. I made the buttons interactable, now I just need to figure out how to get the left oculus controller to cast a ray and click on them :)
     
  6. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    39
    Thanks :) I got the menu to show up in front of the player now.