- Home /
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);
}
}
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 !!!
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.
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:
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?
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 ):
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 ();
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
can you show me the code where you are handling escape button
@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);
}
}
I Check Your Code in Editor working Fine For me
Your answer
Follow this Question
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