UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Camera.main
# [Camera](https://docs.unity3d.com/cn/2019.4/ScriptReference/Camera.html).main public static [Camera](https://docs.unity3d.com/cn/2019.4/ScriptReference/Camera.html) **main** ; ## 描述 第一个启用的摄像机标记为“MainCamera”。(只读) 场景中的主摄像机。如果场景中没有此类摄像机,则返回 /null/。 该属性在内部使用 FindGameObjectsWithTag 而不会将此结果缓存。如果每帧多次使用 [Camera.main](https://docs.unity3d.com/cn/2019.4/ScriptReference/Camera-main.html) 的返回值,建议将该值缓存。 ``` //Place this script on a GameObject to switch between the main Camera and your own second Camera on the press of the "L" key //Place a second Camera in your Scene and assign it as the "Camera Two" in the Inspector. using UnityEngine; public class Example : MonoBehaviour { //This is Main Camera in the Scene Camera m_MainCamera; //This is the second Camera and is assigned in inspector public Camera m_CameraTwo; void Start() { //This gets the Main Camera from the Scene m_MainCamera = Camera.main; //This enables Main Camera m_MainCamera.enabled = true; //Use this to disable secondary Camera m_CameraTwo.enabled = false; } void Update() { //Press the L Button to switch cameras if (Input.GetKeyDown(KeyCode.L)) { //Check that the Main Camera is enabled in the Scene, then switch to the other Camera on a key press if (m_MainCamera.enabled) { //Enable the second Camera m_CameraTwo.enabled = true; //The Main first Camera is disabled m_MainCamera.enabled = false; } //Otherwise, if the Main Camera is not enabled, switch back to the Main Camera on a key press else if (!m_MainCamera.enabled) { //Disable the second camera m_CameraTwo.enabled = false; //Enable the Main Camera m_MainCamera.enabled = true; } } } } ```
da
2022年5月24日 10:14
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码