- Home /
Camera being disabled by any GUI button
Hello everyone,
I have a camera that should be enabled or disabled with a specific toggle button. The problem is that when it is on, if I press the mouse button on any other GUI button on the screen, the camera will be desabled. After I release the mouse button it will be turned back on. This happens with every GUI button/toggle on the screen.
Next is the code of C# class that handles the enabling/disabling of the camera. Don't consider the Start and LateUpdate functions since they are used for other purposes:
using UnityEngine;
using System.Collections;
public class Configuration : MonoBehaviour {
public Camera mainCamera; //Para configurar su ViewPort
public Camera gizmoCamera; //Para configurar su ViewPort
public Camera mapCamera; //Para configurar su ViewPort
public GUISkin skin; //Para estilizar los componentes de la GUI para el BOX de herramientas
private bool mapaButton;
private int anchoFinal; //Para alojar el ancho de la ventana final (según desktop)
private int altoFinal; //Para alojar el alto de la ventana final (según desktop)
private bool viewPortConfiged = false; //para ejecutar sólo una vez el cambio de ViewPort
void Awake(){
//Inicialmente la cámara del mapa está encendida, por lo que se busca apagarla
ToggleMapCamera();
}
void Start() {
WindowsAPI wh = new WindowsAPI();
//obtener los datos del desktop
Rect workingArea = wh.GetWorkingArea(); //
int ancho = (int) workingArea.width;
int alto = (int) workingArea.height;
int bordeVertical = wh.GetBorderHeight(); //
int bordeHorizontal = wh.GetBorderWidth(); //
int bordeCaption = wh.GetCaptionHeight(); //
anchoFinal = ancho + bordeHorizontal * 2;
altoFinal = alto + bordeVertical*2;
if(Screen.width == 299)
wh.MakePlayerWindow(ancho - bordeHorizontal * 2, alto - bordeCaption - bordeVertical*2, WindowState.Windowed);
}
void LateUpdate(){
//sólo configura si cambió la resolución y si no se ha ingresado antes
if((Screen.width != 299) && (!viewPortConfiged)) {
WindowsAPI wh = new WindowsAPI();
//setea el ViewPort de cada cámara de acuerdo a la nueva resolución
mainCamera.pixelRect = new Rect(0,0, Screen.width, Screen.height - 31);
mapCamera.pixelRect = new Rect(Screen.width - 159, Screen.height - 369 - 14, 159, 369);
gizmoCamera.pixelRect = new Rect(0, 0, 100, 100);
//Despliega la ventana utilizando la mayor parte del área del desktop
wh.CentrarVentana(anchoFinal,altoFinal);
viewPortConfiged = true;
}
}
void OnGUI() {
GUI.skin = skin;
GUI.Box (new Rect(0, 0, Screen.width, 31),"",GUI.skin.GetStyle("GUI2-Box Herramientas"));
//Crea el texto "Mapa" y el botón que se encuentra inmediatamente al lado
GUILayout.BeginArea (new Rect (Screen.width - 159, 0, 159, 30));
GUILayout.BeginHorizontal();
GUILayout.Label ("Mapa",GUI.skin.GetStyle("GUI2-HeaderMapa"));
//Si se presiona el botón, se invocará ToggleMapCamera()
mapaButton = GUILayout.Toggle(mapaButton,"Mapa");
GUILayout.EndHorizontal();
GUILayout.EndArea ();
if(mapaButton) ToggleMapCamera();
}
void ToggleMapCamera(){
mapCamera.enabled = !mapCamera.enabled;
}
}
Thanks for the help.
Regards,
Sebastián Toro
Can you show code of button that shouldn't toggle camera but does ?
Also add debug log into Toggle$$anonymous$$apCamera and check if logs appears in console when u are pressing another buttons
I added the Debug into Toggle$$anonymous$$apCamera() and it didn't enter. Although on the editor I could see how the camera switched between enabled and disabled when I clicked on any GUI button-type control.
That would imply that you have some code elsewhere that's toggling the camera.
Dave,
I'm sorry but I don't have any code on the other JS class posted and it still happens. Other ideas?
Answer by Dave-Carlile · Jan 25, 2013 at 02:30 PM
It seems to me that if the button is ever checked, then ToggleMapCamera is going to be called every frame since GUILayout.Toggle will always return true. In fact, it could be called multiple times per frame according to the OnGUI documentation.
The reason you're seeing the odd behavior is likely that in the normal case ToggleMapCamera is being called twice per frame, so it's toggling the camera on, then off. But when you click a different button it's being called an odd number of times.
Rather than toggling the camera, you should just turn it on or off based on the state of the toggle...
mapCamera.enabled = mapaButton;
Tried the "mapCamera.enabled = mapaButton;" solution, the problem is that the toggle always returns TRUE when pressed, therefore, "mapCamera.enabled" will always be set to TRU$$anonymous$$
Removed the "Toggle$$anonymous$$apCamera()" function and replaced the "if(mapaButton) Toggle$$anonymous$$apCamera();" with a "if(mapaButton) mapCamera.enabled =!mapCamera.enabled;" and the problem still occurs. It seems to me that the problem is beyond the "Toggle$$anonymous$$apCamera()" function.
If you're wanting to toggle the camera every time you press the button then you should be using a normal button, not a toggle (and use your enabled = !enabled code). A toggle button will always return true if it's toggled on, and false if it's not.
I agree with you Dave. I know I can't use the statement "mapCamera.enabled = mapaButton;" that's why I'm using the " = !enabled". I would like to know why the camera is being toggled when I press any other GUI control. This happens even if all the code is inside the function OnGUI. This is beyond ODD.
Also, I must add that if I invert the relation Toggle-$$anonymous$$apState (when Toggle is ON $$anonymous$$ap is OFF), the problem still occurs when the $$anonymous$$ap is OFF (Toggle On), meaning that when the $$anonymous$$ap is OFF and I press any other GUI control, the map appears and disappears. This means that the problem is occurring only on the but when the Toggle is in its ON state.
Any other ideas?
If you use your enabled = !enabled logic and a toggle button then this will not work, becuase OnGUI is called one or more times during a frame depending on state changes. This will cause your enabled = !enabled logic to run an undeter$$anonymous$$ed number of times, so the final state is undeter$$anonymous$$ed. If you want to use a toggle button, then the enabled state of mapCamera should be the same state as the toggle button...
void OnGUI()
{
toggled = GUILayout.Toggle(toggled, "Test Toggle");
GUILayout.Label("Toggled=" + toggled);
// mapCamera.enabled = toggled;
}
Answer by SebastianToro · Jan 25, 2013 at 02:00 PM
Here is another class (JS) that has another toggle button. Remember that this happens with every GUI button-type controller.
Just focus on the OnGUI function:
import System.Xml;
//XML con los layers
var listaCompletaLayers : TextAsset;
//Skin para la selección de layers
var guiSkin : GUISkin;
//Imágenes para el botón de encendido/apagado de los layers
var onTexture : Texture;
var offTexture : Texture;
private var layers : Array;
private var gameObjects : Array;
private var toolBoxVisible : boolean;
//almacena la posición del scrollbar
private var scrollPosition : Vector2 = Vector2.zero;
function Start () {
//Cargar el XML
var xmlDoc : XmlDocument = new XmlDocument();
xmlDoc.LoadXml(listaCompletaLayers.text);
//Obtener todos los nodos tipo Layer
var NodeList : XmlNodeList = xmlDoc.GetElementsByTagName("Layer");
//leer todos los nodos obtenidos y almacenarlos en el arreglo planos
layers = new Array();
for (var layer : XmlNode in NodeList)
{
var objetoLayer : LayerClass = new LayerClass(layer.Attributes["tipo"].Value);
layers.Add(objetoLayer);
}
//encontrar los componentes y almacenar su referencia
gameObjects = new Array();
for (var layer : LayerClass in layers)
{
var componentes : GameObject[] = GameObject.FindGameObjectsWithTag(layer.GetTipo());
gameObjects.Add(componentes);
}
}
function OnGUI () {
GUI.skin = guiSkin;
//Crea el texto "Capas" y el botón que se encuentra inmediatamente al lado
GUILayout.BeginArea (new Rect (0, 0, 159, 30));
GUILayout.BeginHorizontal();
GUILayout.Label ("Capas",GUI.skin.GetStyle("GUI2-HeaderCapas"));
toolBoxVisible = GUILayout.Toggle(toolBoxVisible,"Capas");
GUILayout.EndHorizontal();
GUILayout.EndArea ();
if (toolBoxVisible) {
/* Con ToolBox en la esquina superior-izquierda */
var ToolBoxScroll : boolean;
var altoNecesario : int = layers.length * 18 + 5;
var altoDisponible : int = Screen.height/2; //383 corresponde a la barra más el Mapa
var altoTollBox : int;
var ptoInicioToolBox : int = 30; //En el 30 termina el ToolBox
if (altoDisponible > altoNecesario){
altoTollBox = altoNecesario;
ToolBoxScroll = false;
}else{
altoTollBox = (Mathf.Round(altoDisponible/18) - 1)*18 + 5;
ToolBoxScroll = true;
}
GUILayout.BeginArea (Rect (0, 30, 200, altoTollBox),GUI.skin.GetStyle("GUI2-BoxCapas"));
GUILayout.BeginVertical();
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(197), GUILayout.Height(altoTollBox-5));
for (var layer : LayerClass in layers)
{
GUILayout.BeginHorizontal();
if(ToolBoxScroll)
GUILayout.Label (layer.GetTipo(),GUI.skin.GetStyle("LabelLayer_c_Scroll"));
else
GUILayout.Label (layer.GetTipo(),GUI.skin.GetStyle("LabelLayer_s_Scroll"));
var buttonSkin : Texture = (layer.estado) ? onTexture : offTexture;
if(GUILayout.Button (buttonSkin,GUI.skin.GetStyle("GUI2-ToggleCapas"))) ToggleLayer(layer.GetTipo());
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea ();
}
}
function ToggleLayer (nombreLayer : String) {
var indexLayer : int = 0;
for (var layer : LayerClass in layers)
{
if(layer.GetTipo() == nombreLayer){
layer.estado = !layer.estado; //hace un toggle al estado de la capa
var arregloObjetosCapa : GameObject[] = gameObjects[indexLayer] as GameObject[];
//
for (var componente : GameObject in arregloObjetosCapa) {
componente.active = !componente.active; //Hace un Toggle al estado del objeto
}
//apagar todos los componentes de dicho layer
break;
}
indexLayer++;
}
}
Your answer
Follow this Question
Related Questions
Attach image to button 1 Answer
Button moving away from Camera. 0 Answers
Making GUI Buttons on a GUI Texture 1 Answer
pan camera by touch / gui button 0 Answers
Clicking Trigger 1 Answer