Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lucastosta · Oct 04, 2016 at 10:37 PM · mobilemobile devicesmobile keyboardmobilephone

Android Back (or Escape) button isn't being recognized.

Hello!

I've already searched a lot around the internet, but still with the problem. So, what i want to do is: I'm on a video gallery screen (it's a app not a game) and I want that when I press the Android "Back" button, it returns these functions CloseScreen(); and OpenScreen(); so that will close the video gallery screen and open the main menu screen. I already have a close button to do this, and it is working, but i want the Android "Back" button working too.

Here it is my code:

     void Update(){
         if (Input.GetKey(KeyCode.Escape)){
             CloseScreen ();
             OpenScreen ();
             Debug.Log ("Pressed Back button");
         }
     }
 
     public void OpenScreen(){
         screenToBeOpened.SetActive (true);
     }
     public void CloseScreen(){
         screenToBeClosed.SetActive (false);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by lucastosta · Oct 10, 2016 at 09:19 PM

I finally solved the problem.

1st thing: the android back button is totally buged using unity 5 remote with Galaxy tab 3 (I don't know with other devices), it just worked when I built the project.

2nd thing: It just worked with a brand new project, and I used other code to do the open/close functions.

3rd thing: I'll show the scripts.

 using UnityEngine;
 using System.Collections;
 
 public class openCloseAB : MonoBehaviour {
 
     public Transform canvas;
     public GameObject openThis;
 
     void Update () {
         if (Input.GetKeyDown(KeyCode.Escape)){
             if (canvas.gameObject.activeInHierarchy == false) { //If the "canvas" is deactivated on the hierarchy.
                 canvas.gameObject.SetActive (true); // It activates the "canvas" when I press Escape button.
             } 
             else{
                 canvas.gameObject.SetActive (false); //It deactivates the "canvas" when I press Escape button.
                 openit (); //When I deactivate the "canvas" it opens the previous gameObject (other canvas).
             }
         }
     }
 
     private void openit(){
         openThis.gameObject.SetActive (true);
     }
 }

This script is used for the android back button.

The other one:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class openClose : MonoBehaviour {
 
     public GameObject screenToBeOpened, screenToBeClosed;
 
     public void OpenScreen(){    //This function opens a canvas.
         screenToBeOpened.SetActive (true);
     }
     public void CloseScreen(){    //This function closes the previous canvas to optimize the app.
         screenToBeClosed.SetActive (false);
     }
 }

And this one is used to activate and deactivate the canvases, with the buttons functions too.

I'm sorry if it's a little bit confusing by the way... Well, thanks for the help of everyone @sohail_786 @Bonfire-Boy @SaurabhStudio !!!

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
0

Answer by SaurabhStudio · Oct 05, 2016 at 07:38 PM

     void Update()
     {
         if (Input.GetKey(KeyCode.Escape))
         {
            // code
         }
     }

Code is perfect , Do you get debug?

Have you drag that scripts to any gameobject?

If yes , then that gameobject is active or not? Please check that.

Comment
Add comment · Show 5 · 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 lucastosta · Oct 05, 2016 at 08:37 PM 0
Share

Hello @SaurabhStudio, and thank you for answering me!

Well, I forgot to say that I'm not receiving the Debug.Log message on the console. And yes, it is enabled and the script is parented. To be especific it's parented to a canvas, wich is the video gallery canvas. $$anonymous$$y idea is that when I touch the Android Back button, it deactivates the gallery canvas and reactivates the $$anonymous$$ain $$anonymous$$enu Canvas.

Here it is the Inspector:alt text

screenshot-1.jpg (60.5 kB)
avatar image Bonfire-Boy lucastosta · Oct 05, 2016 at 10:08 PM 0
Share

I've moved your reply to its proper place under the answer that it is a reply to.

The screenshot shows us that the script is enabled in the GameObject. but it does not answer SaurabhStudio's question: is the GameObject active? And note that if you're talking about an android build, an inspector screenshot isn't going to confirm this (unless, perhaps, you're using Unity Remote and the screenshot is made at the time you try pressing the button).

Here's another, possibly crucial, question... you appear to have attached the script to a Canvas... is it one of the Canvases that your code activates/deactivates?

avatar image lucastosta Bonfire-Boy · Oct 06, 2016 at 12:32 PM 0
Share

Thanks for moving it!

So, I already said that it's enabled, and the script is parented with both canvases. Both canvases activates/deactivates.

I'll tell you how it's working. I have 2 canvases:

1st canvas = $$anonymous$$ain $$anonymous$$enu with a button.

2nd canvas = A video gallery wich is opened by the button on the $$anonymous$$ain $$anonymous$$enu canvas.

The $$anonymous$$ain $$anonymous$$enu canvas starts activated and the video gallery canvas starts deactivated. When I press the button on the $$anonymous$$ain $$anonymous$$enu, it activates the video gallery canvas and deactivates the $$anonymous$$ain $$anonymous$$enu canvas. The two canvases has the script attached.

So what I want is: The video gallery canvas is activated and the $$anonymous$$ain $$anonymous$$enu canvas deactivated. So i'm on the video gallery canvas, and then when I touch the Android Back button it deactivates the gallery canvas wich is the actual canvas activated, and reactivate the $$anonymous$$ain $$anonymous$$enu canvas. But it isn't working for me, neither debugging ):

Show more comments
avatar image
0

Answer by SohailBukhari · Oct 06, 2016 at 12:54 PM

@username use Input.GetKeyDown (KeyCode.Escape) instead if (Input.GetKey(KeyCode.Escape)) and call it in update or make a couroutine with yield return new WaitForEndOfFrame ();

Comment
Add comment · Show 7 · 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 lucastosta · Oct 06, 2016 at 02:56 PM 0
Share

Well, I tryed this, but now I wasn't touching the back button and it gaves me lots of messages on debug.

Here it is the screenshot with the normal script: ![alt text][1] [1]: /storage/temp/79622-screenshot-1.jpg

And here it is the screenshot with Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Escape); ![alt text][2] [2]: /storage/temp/79623-screenshot-2.jpg

screenshot-1.jpg (245.2 kB)
screenshot-2.jpg (197.2 kB)
avatar image SohailBukhari · Oct 06, 2016 at 06:35 PM 0
Share

can you show me the code where you are handling escape button

avatar image lucastosta SohailBukhari · Oct 06, 2016 at 07:09 PM 0
Share

@sohail_786 This is the code:

      void Update(){
          if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Escape)){
              CloseScreen ();
              OpenScreen ();
              Debug.Log ("Pressed Back button");
          }
      }
  
      public void OpenScreen(){
          screenToBeOpened.SetActive (true);
      }
      public void CloseScreen(){
          screenToBeClosed.SetActive (false);
      }
  }
avatar image SohailBukhari lucastosta · Oct 08, 2016 at 04:56 PM 1
Share

I Check Your Code in Editor working Fine For me

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.GetKey for mobile game 0 Answers

How do you test a mobile game? 0 Answers

Keep input field caret active when focused on mobile devices in addition to native input field caret 0 Answers

Is it possible to make a Unity mobile app appear under the Lock Screen, like the phone app? 0 Answers

Android : How to manage game display with on-screen front cameras ? 1 Answer


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