Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cearas · Apr 01, 2015 at 04:19 PM · canvasscreenbuttonskeyboardbutton backround

How do i pop up a canvas, when a click some key from keyboard?

Hi people, so my doubt is this. How do i pop up a canvas, when a click some key from keyboard?

for example, i'm controlling a 3rd person controller through a house, and when i'm trying to open a door, i want to open some canvas or UI or show me some buttons, that give me some actions to do with this same door: - knock knock! - ring the bell - pull the key - just open the door

first off all, i'm sorry about my poor english, but i hope you understood my doubt.

best regards!

unity FTW!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KdRWaylander · Apr 02, 2015 at 08:06 AM

Hi,

You want to use the 'SetActive' function of your canvas (and to be listening to your keyboard) !

Let's say you wanna use 'E' to interact with the door, you will want to write a script in which as soon as you use the E key, the canvas will be active (and thus visible) and when you press E again, the canvas will be hidden.

C#:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 // put this script on any gameobject, i personally use an empty 'root' gameobject on which i put all my scripts alike
 public class ToggleDoorCanvas : MonoBehaviour {
     public Canvas doorCanvas;    // now you have to drag and drop your canvas in the editor in the script component
     private bool doorActive = false; // do we have to display the canvas (true) or not (false)
     
     void Update () {
         if(Input.GetKeyDown(Keycode.E)){ // if you press the E key
             doorActive = !doorActive; // change the state of your bool
             doorCanvas.transform.SetActive(doorActive); // display or not the canvas (following the state of the bool)
         }
     }
 }

Now that's not perfect because it will display the canvas wherever you are, far from the door or not, whatever you're doing, pressing E will display the canvas. What you can do to improve the solution:

  • Use a 'world canvas' instead of a classic canvas that is stuck on you camera, that will display the canvas 'in the world' like a gameobject: https://www.youtube.com/watch?v=Mzt1rEEdeOI

  • Use raycast to fire a limited-range ray in front of you to detect if there's a door or not and if it's the case display the canvas.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DeadManJoe33 · Apr 19, 2016 at 03:46 PM 0
Share

$$anonymous$$dRWaylander, I am using your script to make a canvas popup on pressing a keycode but after putting it in I had to change some parts of the code to get it to work and even after doing that it still won't popup the canvas, what am I missing?alt text

screen-shot-2016-04-19-at-111010-am.png (102.0 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Canvas buttons and fullscreen aspect ratio 1 Answer

Screen Space - Camera Bug (Buttons does not work) on Unity 2017 0 Answers

Menu not getting Keyboard Input 0 Answers

Lock the screen 1 Answer

UNFIT SCREEN SIZE FOR MOBILE 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges