Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Show canvas by pressing esc

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jeremie-de-Vos, Sep 3, 2015.

  1. Jeremie-de-Vos

    Jeremie-de-Vos

    Joined:
    Jun 5, 2015
    Posts:
    120
    Hey can someone please tell me how I can show a canvas by pressing esc? (Im a begginer)
    And wen I press a button that opend a canvas It will close the all other canvas.

    (Im still a begginer)
    And sorry for my Englishe
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Code (CSharp):
    1. //Attach your canvas to this variable in inspector
    2. public GameObject canvasObj;
    3.  
    4. //This will check if your game is paused (we'll set it)
    5. bool gamePaused;
    6.  
    7.  
    8. void Update()
    9. {
    10. //Reading input for ESCAPE key, and by saying gamePaused = !gamePaused, we switch the bool on and off each time the Keycode is registered!
    11. if(Input.GetKeyDown(KeyCode.Escape))
    12.     gamePaused = !gamePaused;
    13.  
    14. //Now we enable and disable the game object!
    15. if(gamePaused)
    16.     canvasObj.SetActive(true);
    17. else
    18.     canvasObj.SetActive(false);
    19.  
    20. }
    21.  
    A useful link for you:
    http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html


    Let me know/reply here if you need any further help! :)
     
    Last edited: Oct 7, 2015
    nattapong9 and jaymacibe like this.
  3. Jeremie-de-Vos

    Jeremie-de-Vos

    Joined:
    Jun 5, 2015
    Posts:
    120
    Hey, Thx for help men it works Fantastic:)
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    You are more than welcome! :)
    Let me know in case you need any further assistance!
     
  5. Jeremie-de-Vos

    Jeremie-de-Vos

    Joined:
    Jun 5, 2015
    Posts:
    120
    Hey, I got another question how can you lock the cursor with escape.
    what I actually want is a the scrip here above but wen you press escape it wil set the game to pause and it will show the cursor. but I really don't know how you can do that. Do you know how you can do that?
     
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I found this link for you, let me know if it's good enough:
    http://docs.unity3d.com/ScriptReference/Screen-showCursor.html
     
  7. nattapong9

    nattapong9

    Joined:
    Sep 13, 2015
    Posts:
    1
    Hi Sykoo,
    Your explanation is very short but very helpful and easy to understand.
    I am a beginner too, seeking this info just found your help here.
    Perfect.
    I tried reading UnityDoc but sometime it's not easy to understand.
    But reading how to use SetActive from your code above, I just suddenly realized cause you comments are so straight and simple.

    Thanks so much.
     
  8. Vinicius_Araujo

    Vinicius_Araujo

    Joined:
    Feb 23, 2020
    Posts:
    3
    Code (CSharp):
    1. //Pause System - To enable mouse when you press Escape.
    2. void Update() {
    3.         if (!gamePaused)
    4.         {
    5.             Cursor.lockState = CursorLockMode.Locked;
    6.             Cursor.visible = false;
    7.             mouseLook(); //All mouse / moviment or other scripts
    8.         }
    9.         else
    10.         {
    11.             Cursor.lockState = CursorLockMode.None;
    12.             Cursor.visible = true;
    13.         }
    14. }
     
    Last edited: Feb 23, 2020