아래 코드와 같이 Camera.main을 매번 호출할 필요는 없다.
Start()에서 미리 Camera.main을 Camera 변수에 담아놓고 다른 함수에서 불러쓰는 것이 좋겠다.
성능 상 큰 문제는 아니지만, 성능을 고려하자면 캐싱해 놓고 써야겠다.
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;
}
}
참고 문서
https://docs.unity3d.com/ScriptReference/Camera-main.html
Unity - Scripting API: Camera.main
If there is no enabled Camera component with the "MainCamera" tag, this property is null. Internally, Unity caches all GameObjects with the "MainCamera" tag. When you access this property, Unity returns the first valid result from its cache. Accessing this
docs.unity3d.com
'Engine > Unity' 카테고리의 다른 글
유니티 Billboard Renderer 직접 구현할 필요가 없다 (0) | 2023.02.11 |
---|---|
유니티 GetChild() vs GameObject.Find() 어느 것이 속도가 빠를까. (0) | 2023.02.11 |
유니티 [ExecuteInEditMode]로 플레이 하지 않은 상태에서 값 변경하기 (0) | 2023.02.11 |
유니티 인스펙터 창에 속성들 구분하는 [Header()] (0) | 2023.02.11 |
Unity Addressable Asset System 좋은 글 링크 (0) | 2023.01.18 |