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

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++;   						   	
	}
}

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;