- Home /
UI Buttons switch cameras
I have four UI Buttons, and there are four different cameras. Is there a way I can press the UI Button to switch the Camera? One button for one camera.
Answer by maccabbe · Feb 27, 2015 at 11:34 PM
using UnityEngine;
public class NewBehaviour : MonoBehaviour {
public GameObject camera1;
public GameObject camera2;
public GameObject camera3;
public GameObject camera4;
public Texture texture;
void Start() {
camera1.SetActive(true);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
}
void OnGUI() {
if(GUI.Button(new Rect(0, 0, 100, 100), texture)) {
camera1.SetActive(true);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
}
if(GUI.Button(new Rect(100, 0, 100, 100), texture)) {
camera1.SetActive(false);
camera2.SetActive(true);
camera3.SetActive(false);
camera4.SetActive(false);
}
if(GUI.Button(new Rect(200, 0, 100, 100), texture)) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(true);
camera4.SetActive(false);
}
if(GUI.Button(new Rect(300, 0, 100, 100), texture)) {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(true);
}
}
}
Nice thanks for the reply. Is this only for one button? Sorry trying to wrap my $$anonymous$$d around it, I had planed on using the new UI buttons
Sorry, tried to do what you described and I could do all 4 buttons in one script with the old UI.
GUI.Button(Rect rect, Texture texture) shows a button in the specified rectangle. It returns true when the button is pressed which causes one camera to be enabled and the rest to be disabled. So each if statement relates to one button which, when pressed down switches to a camera.
With the new UI you can use the following script
using UnityEngine;
using System.Collections;
public class CameraSwitcher : $$anonymous$$onoBehaviour {
public GameObject camera1;
public GameObject camera2;
public GameObject camera3;
public GameObject camera4;
public void EnableCamera1() {
camera1.SetActive(true);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(false);
}
public void EnableCamera2() {
camera1.SetActive(false);
camera2.SetActive(true);
camera3.SetActive(false);
camera4.SetActive(false);
}
public void EnableCamera3() {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(true);
camera4.SetActive(false);
}
public void EnableCamera4() {
camera1.SetActive(false);
camera2.SetActive(false);
camera3.SetActive(false);
camera4.SetActive(true);
}
}
Attach this script to a gameObject, fill in the cameras, and have each of the new buttons call one of the EnableCamera functions on click.
Hey man, thank you for that script, I just done exactly the same before reading, so awesome to know I done it right lol I will make your answer as correct, thanks for the time!
While this solution works, you should consider using @tadadosi 's solution for it is more elegant and easier to extend later.
Answer by Alan23253 · Feb 28, 2015 at 04:40 AM
It's very simple, First select your button, next add OnClick function in the button Script (repeat this for each camera you want to active/deactive) drag the camera on One slot, in no function select GameObject --> SetActive (bool) an check if you want the camera active or deactive, repeat it for the number of cameras you have
@Alan23253 This simple method worked great for me! Thank you!
One problem I have though is that my cameras have a Drag Camera 2D script attached to them that allows the user to pan around the scene. What do I have to do to the buttons to get them to reset the camera back to the starting position. Right now, if someone pans the camera to a different view and then clicks a different button to go to another camera, when they go back to the first camera the view is still at wherever they panned to.
Answer by tadadosi · May 21, 2020 at 02:49 PM
You could do a simple class called CameraManager to control it, something like:
using UnityEngine;
public class Quija_CameraManager : MonoBehaviour
{
public GameObject[] cameras;
public Transform[] startPoints;
public void SetCamera(int index)
{
DisableAndResetCameras();
if (index >= 0 && index <= cameras.Length - 1)
cameras[index].SetActive(true);
}
private void DisableAndResetCameras()
{
if (cameras.Length > 0)
{
for (int i = 0; i < cameras.Length; i++)
{
cameras[i].SetActive(false);
cameras[i].transform.position = startPoints[i].position;
}
}
}
}
CameraManager Inspector setup:
UI Button Event setup:
Thanks @tadadosi this has gotten me closer. However, for the inspector setup, How do I get the Cam_StartPoint0 (Transform)? I don't have that option to put in that field. I dragged my cameras in there but that doesn't reset back to the starting position.
Np! @Bolton$$anonymous$$enk, in my script what you should be adding to the startPoints array are not the actual camera transforms, they should be a different gameobject placed in the the starting position of each of your cameras.
Here is an alternative that works with just the cameras, no need to have extra gameobjects:
using UnityEngine;
public class Camera$$anonymous$$anagerV2 : $$anonymous$$onoBehaviour
{
public int initialCamera;
public GameObject[] cameras;
[SerializeField] private Vector2[] startPoints;
private void Awake()
{
StoreStartPoints();
SetCamera(initialCamera);
}
private void StoreStartPoints()
{
if (cameras.Length > 0)
{
startPoints = new Vector2[cameras.Length];
for (int i = 0; i < cameras.Length; i++)
{
startPoints[i] = cameras[i].transform.position;
}
}
}
public void SetCamera(int index)
{
DisableAndResetCameras();
if (cameras.Length > 0 && index >= 0 && index <= cameras.Length - 1)
cameras[index].SetActive(true);
}
private void DisableAndResetCameras()
{
if (cameras.Length > 0)
{
for (int i = 0; i < cameras.Length; i++)
{
cameras[i].SetActive(false);
cameras[i].transform.position = startPoints[i];
}
}
}
}
@tadadosi you are amazing! Your script solved a problem I have been trying to solve for at least two months. Thank you so much!
If I'm not pushing my luck too far, I have one more question for you. I had to modify your script slightly to assign a vector3 ins$$anonymous$$d of vector2 in order to get the buttons to get the cameras back to the exact starting position. However, this does not account for someone zoo$$anonymous$$g in a camera, which is also a feature of the Drag Camera 2D script. If they zoom in and then try to reset the camera with the click of the button, the camera resets to the X, Y & Z coordinates but remains zoomed in. Is there something I can add to your script that would reset the zoom as well?
Thanks again!
Your answer
Follow this Question
Related Questions
Button moving away from Camera. 0 Answers
character switching on collision 1 Answer
3rd Person to First Person Camera 0 Answers
Why does transform.rotate() do this? 1 Answer
How to switch between four characters when a button is pressed? 1 Answer