Camera = GameObject.Find("Main Camera").GetComponent<Camera>();
시작시 Main Camera를 비추도록 하는 코드
"MainCamera"태그가 지정된 첫 번째 활성화 된 카메라 구성 요소 (읽기 전용).
"MainCamera"태그가있는 활성화 된 카메라 구성 요소가없는 경우이 속성은 null입니다.
내부적으로 Unity는 "MainCamera"태그를 사용하여 모든 게임 오브젝트를 캐시합니다.
이 속성에 액세스하면 Unity는 캐시에서 첫 번째 유효한 결과를 반환합니다.
이 속성에 액세스하면 GameObject.GetComponent를 호출하는 것과 비슷한 CPU 오버 헤드가 적습니다. 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;
}
}
}
}
댓글