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);
}
}
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 !!!
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.
@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 ();