Unity 摄像机切换镜头

直接上码:

public class test04 : MonoBehaviour {
    //对象
    private GameObject mainCamera;
    private GameObject camera1;
    private GameObject camera2;
    private GameObject camera3;

    // Use this for initialization
    void Start () {
        mainCamera = GameObject.Find ("Main Camera");
        camera1 = GameObject.Find ("Camera1");
        camera2 = GameObject.Find ("Camera2");
        camera3 = GameObject.Find ("Camera3");

        camera1.active = false;
        camera2.active = false;
        camera3.active = false;
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    void OnGUI() {

        if (GUILayout.Button ("主 摄像机", GUILayout.Height (50))) {
            mainCamera.active = true;
            camera1.active = false;
            camera2.active = false;
            camera3.active = false;
        }
        if (GUILayout.Button ("左 摄像机", GUILayout.Height (50))) {
            mainCamera.active = false;
            camera1.active = true;
            camera2.active = false;
            camera3.active = false;
        }
        if (GUILayout.Button ("右 摄像机", GUILayout.Height (50))) {
            mainCamera.active = false;
            camera1.active = false;
            camera2.active = true;
            camera3.active = false;
        }
        if (GUILayout.Button ("顶部 摄像机", GUILayout.Height (50))) {
            mainCamera.active = false;
            camera1.active = false;
            camera2.active = false;
            camera3.active = true;
        }
    }
}

把脚本放在 GameObject 上面比较好,因为摄像头会打开和关闭:


Unity 摄像机切换镜头_第1张图片
39AF4144-5567-42F7-BCA0-C3C1E18B64E4.png

主 摄像机:


Unity 摄像机切换镜头_第2张图片
501A52EB-1F79-4292-821E-BDFEA5A03F47.png

左 摄像机:


Unity 摄像机切换镜头_第3张图片
84DBBB6B-D572-48D2-A010-D80963CD2C5A.png

右 摄像机:


Unity 摄像机切换镜头_第4张图片
82494256-DF18-40D4-85E4-C5610B945D09.png

顶部 摄像机:


Unity 摄像机切换镜头_第5张图片
01DC4C02-F875-40FA-932A-CF21CFE5ED98.png

你可能感兴趣的:(Unity 摄像机切换镜头)