版本: Unity 6 (6000.0)
语言英语
  • C#

Camera.main

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们确实会阅读用户提出的每个修改建议,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的修改建议无法提交。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提高 Unity 文档的质量。

关闭

取消

切换到手册
public static Camera main;

描述

第一个被启用的并标记为“MainCamera”的 Camera 组件(只读)。

如果没有被启用的并标记为“MainCamera”的 Camera 组件,此属性将为 null。

在内部,Unity 会缓存所有标记为“MainCamera”的 GameObject。当您访问此属性时,Unity 会从其缓存中返回第一个有效结果。访问此属性会产生少量的 CPU 开销,类似于调用 GameObject.GetComponent。在 CPU 性能至关重要的情况下,请考虑缓存此属性。

其他资源: 标签

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