- Home /
Question by
CallToAdventure · Jul 26, 2011 at 11:25 AM ·
cameratoggle button
Simple toggle camera button
ok, so I have three cameras in my scene. My problem is I can't have one button for each - it's just not nice. So how do I go around and actually use one button to toggle (clockwise, endlessly) between my cameras? Currently I use this rough approach:
if (Cam1) {
Cam1.enabled = true;
Cam2.enabled = false;
Cam3.enabled = false;
}
and on GUI function, the button :
if (GUI.RepeatButton(Rect(200,10,50,50),"Cam1"))
Cam1=true;
else
{
Cam1=false;
}
and so on, for each camera. Where should I look? The toggle button from docs doesn't inspire help me much, I get all sorts of fancy results when I try to build upon that :)
Comment
Best Answer
Answer by Mortennobel · Jul 26, 2011 at 07:56 PM
Use a index to keep track of the current camera. Something like this:
var cameras : Camera[];
var selectedCamera : int = 0;
function Awake(){
UpdateSelectedCamera();
}
function OnGUI () {
var cameraString = "Camera "+(selectedCamera+1); // show "Camera X", where X is 1-indexed
if (GUI.Button(new Rect(0,0,100,50),cameraString)){
selectedCamera++;
if (selectedCamera==cameras.Length){
selectedCamera=0;
}
UpdateSelectedCamera();
}
}
function UpdateSelectedCamera(){
for (var i=0;i<cameras.Length;i++){
cameras[i].enabled = i==selectedCamera;
}
}
Your answer