简体   繁体   中英

Unity, change the display camera for the scene and the target display in the Canvas C#

I have 2 cameras and 1 Canvas, and I want to change the display for the scene to the second camera when pressing a button, and at the same time, change the target display for the Canvas to the second camera as well, and I'm using C#.

I know that I have to use GetComponent<Camera> and GetComponent<Canvas> but i don't know how to set the indexes for these two.

Any help will appreciated.

Rather than changing the index: just disable the one camera and enable the other. There's no point wasting GPU cycles rendering something that can't be seen (which is what a second active camera will do).

As for changing the Canvas target:

Change the canvas's worldCamera property, eg:

canvasGO.GetComponent<Canvas>().worldCamera = cam2.GetComponent<Camera>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM