Hello to all. I need a little help to solve out a problem with a camera script and a GUI.button menu.
Here is my scene hierarchy:
There is a camera (named “animaCam”) that can see 5 layers. On each layer there is a texture animation script attached to a gameobject (Cube) that plays an animation if a suitable gui.button is clicked, on a gui.box (Menu).
Before this camera, there is another camera (named “logoCam”), that serves as a logo screen, that sees one layer only.
With this configuration, I want to achieve the following functionality. When the user clicks a gui.button, a gui.box pops up, showing initially the logoCam and five gui.buttons to choose from.
Each of these buttons, does the following actions (with some misuses)
a. Should hide the logoCam
b. Should enable the animaCam
c. Should choose the suitable layer with the suitable Cube
d. Should start the animation script on the Cube
With this configuration, I get the app running on Unity player and on an android device, but I have to solve the following mulfunctions:
a. Once the user clicks the gui.button for the “showMenuOptions”, on the android device the menu stays on. Even when the button is clicked again to close it stays on screen. On Unity player, the gui.button for menu behaves as it should, open/closes on click (see image 1 image 2)
b. On user option to play e.g. the first animation, the function executes and the animation plays (see image 3).
When the user clicks the other gui.button that enables/disables the “showMenuOption”, the menu closes, but the animation keeps playing. It should be closed with the menu shut down button (see image 4), meaning that animaCam, logoCam and runAnima1 should be off in Inspector.
I believe, the error must be in the Update function. I have tried everything I could think of, without much success.
Please, take a look at the following code. Post in some thoughts or a even better a code snippet if I have mistaken something.
I really need to make the camera and gui.button menu, behave as it should, both on device an on Unity player.
Thank you in advance for your answers.
Here is the code snippet:
//ShowAnimation Menu with camera option
#pragma strict
var native_width : float = 480;
var native_height : float = 320;
var btnTexture : Texture;
var bgrImage : Texture;
public var animaCam : Camera;
public var logoCam : Camera;
public var animaCamOn : boolean = false;
public var logoCamOn : boolean = false;
var showMenuOptions:boolean = false; //true = show the other GUI elements
public var runAnima1 = false;
public var runAnima2 = false;
public var runAnima3 = false;
public var runAnima4 = false;
public var runAnima5 = false;
var animator1Mask : LayerMask; // select desired camera layer in inspector
var animator2Mask : LayerMask; // select desired camera layer in inspector
var animator3Mask : LayerMask; // select desired camera layer in inspector
var animator4Mask : LayerMask; // select desired camera layer in inspector
var animator5Mask : LayerMask; // select desired camera layer in inspector
function Awake ()
{
DontDestroyOnLoad (GameObject.FindGameObjectWithTag("animator"));
}
function Start()
{
animaCam.enabled = false;
}
function Update()
{
if (!animaCamOn)
{
animaCam.enabled = false;
}
else
{
animaCam.enabled = true;
}
if (!logoCamOn)
{
logoCam.enabled = false;
}
else
{
logoCam.enabled = true;
}
if(!showMenuOptions)
{
showMenuOptions = false;
}
else
{
showMenuOptions = true;
}
}
function OnGUI ()
{
//set up scaling
var rx : float = Screen.width / native_width;
var ry : float = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
//now create your GUI normally, as if you were in your native resolution
//The GUI.matrix will scale everything automatically.
if (!btnTexture) // This is the Route button that triggers the "showMenuOption"
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
// Make sure we only call GUI.Window if showMenuOption is true.
if(GUI.Button(Rect(100, 200, 40, 25), btnTexture))
showMenuOptions = !showMenuOptions;
if(showMenuOptions)
{
// Make a background GUI box for showMenuOption
GUI.Box (Rect (12,2,350,100),"Choose to play an animation:");
GUI.DrawTexture(Rect(120,30,300,85), bgrImage);
logoCamOn = !logoCamOn; // In this configuration showMenuOptions is on/off with logoCam activated as a preview.
// Make the first button. If it is pressed, Animation (1) will be executed
if (GUI.Button (Rect (10,14,100,20), "Anima1"))
{
animaCamOn = !animaCamOn; // animaCamOn disabled. In this configuration showMenuOptions is on/off with logoCam activated. On "Anima1" btn clicked, logoCamera stays on.Error!!!
logoCamOn = !logoCamOn;
animaCam.cullingMask = Animator1Mask;
runAnima1 = !runAnima1;
}
// Make the second button. If it is pressed, Animation (2) will be executed
if (GUI.Button (Rect (10,36,100,20), "Anima2"))
{
animaCamOn = !animaCamOn;
logoCamOn = !logoCamOn;
animaCam.cullingMask = Animator2Mask;
runAnima2 = !runAnima2;
}
// Make the third button. If it is pressed, Animation (3) will be executed
if (GUI.Button (Rect (10,56,100,20), "Anima3"))
{
animaCamOn = !animaCamOn;
logoCamOn = !logoCamOn;
animaCam.cullingMask = Animator3Mask;
runAnima3 = !runAnima3; }
// Make the forth button. If it is pressed, Animation (4) will be executed
if (GUI.Button (Rect (10,88,100,20), "Anima4"))
{
animaCamOn = !animaCamOn;
logoCamOn = !logoCamOn;
animaCam.cullingMask = Animator4Mask;
runAnima4 = !runAnima4;
}
// Make the fifth button. If it is pressed, Animation (5) will be executed
if (GUI.Button (Rect (10,110,100,20), "Anima5"))
{
animaCamOn = !animaCamOn;
logoCamOn = !logoCamOn;
animaCam.cullingMask = Animator5Mask;
runAnima5 = !runAnima5;
}
}
}