Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ouija · Feb 27, 2015 at 11:19 PM · camerabuttonswitch

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ouija · Feb 27, 2015 at 11:56 PM 0
Share

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

avatar image maccabbe · Feb 28, 2015 at 12:50 AM 1
Share

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.

avatar image Ouija · Feb 28, 2015 at 01:04 AM 0
Share

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!

avatar image KoenigX3 · May 24, 2020 at 05:35 PM 0
Share

While this solution works, you should consider using @tadadosi 's solution for it is more elegant and easier to extend later.

avatar image
4

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 alt text


captura-de-pantalla-67.png (269.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BoltonMenk · May 21, 2020 at 12:46 PM 0
Share

@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.

avatar image
2

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: alt text


UI Button Event setup: alt text


simple-camera-manager-inspector-setup.png (41.0 kB)
ui-button-setup-to-control-camera-with-simple-came.png (22.5 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BoltonMenk · May 24, 2020 at 05:14 PM 0
Share

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. alt text

screen-shot-2020-05-24-at-121024-pm.png (160.4 kB)
avatar image tadadosi BoltonMenk · May 25, 2020 at 12:21 PM 0
Share

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


avatar image BoltonMenk tadadosi · May 26, 2020 at 02:18 AM 0
Share

@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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges