본문 바로가기
카테고리 없음

[Unity][C#] Camera

by 라이프리 2021. 3. 9.

 

   Camera = GameObject.Find("Main Camera").GetComponent<Camera>();

시작시 Main Camera를 비추도록 하는 코드

 

"MainCamera"태그가 지정된 첫 번째 활성화 된 카메라 구성 요소 (읽기 전용).

"MainCamera"태그가있는 활성화 된 카메라 구성 요소가없는 경우이 속성은 null입니다.

내부적으로 Unity는 "MainCamera"태그를 사용하여 모든 게임 오브젝트를 캐시합니다.

이 속성에 액세스하면 Unity는 캐시에서 첫 번째 유효한 결과를 반환합니다.

이 속성에 액세스하면 GameObject.GetComponent를 호출하는 것과 비슷한 CPU 오버 헤드가 적습니다. CPU 성능이 중요한 경우이 속성을 캐싱하는 것이 좋습니다.

 

캐싱이란? 잠시 저장해둔다, 속도가 빠르다

net-gate.tistory.com/11

 

캐싱(caching)이란

캐시(Cache) 개념과 원리 캐시는 종종 듣게 된다. 캐시 된 거 아니야? 라는 말을 하기도 하고, 캐시 되어 있어서 빠른거야 라는 말을 하기도 한다. 캐시는 잠시 저장해둔다는 의미이고 기능이다. 캐

net-gate.tistory.com


 

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

 

댓글