unity在同屏幕显示多Camera并在脚本中修改Viewport Rece

参考:https://www.it610.com/article/1305219586412548096.htm
参考:https://www.zhihu.com/question/41879088?sort=created

修改Camera的Viewport Rect即可
unity在同屏幕显示多Camera并在脚本中修改Viewport Rece
通过脚本修改,在绑定的CameraController中改

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    private Camera camera_Main;
    private GameObject camera_1;
    private GameObject camera_2;
    private GameObject camera_3;
    void Start()
    {
        camera_Main = transform.Find("Main Camera").GetComponent<Camera>();
        camera_1 = GameObject.Find("Camera1");
        camera_1.SetActive(true);
        camera_2 = GameObject.Find("Camera2");
        camera_2.SetActive(true);
        camera_3 = GameObject.Find("Camera3");
        camera_3.SetActive(true);


        camera_Main.rect = new Rect(camera_Main.rect.x, camera_Main.rect.y, 0.8f, 0.5f);
        //Camera mainCamera = Camera.main;
        //mainCamera.rect = new Rect(mainCamera.rect.x, mainCamera.rect.y, mainCamera.pixelWidth, 0.5f);
    }

    // Update is called once per frame 
    void Update()
    {
        if (Input.GetKey(KeyCode.Alpha1))
        {
            //camera_Main.SetActive(true);
            camera_1.SetActive(false);
            camera_2.SetActive(false);
            camera_3.SetActive(false);
        }
        if (Input.GetKey(KeyCode.Alpha2))
        {
            //camera_Main.SetActive(false);
            camera_1.SetActive(true);
            camera_2.SetActive(false);
            camera_3.SetActive(false);
        }
        if (Input.GetKey(KeyCode.Alpha3))
        {
            //camera_Main.SetActive(false);
            camera_1.SetActive(false);
            camera_2.SetActive(true);
            camera_3.SetActive(false);
        }
        if (Input.GetKey(KeyCode.Alpha4))
        {
            //camera_Main.SetActive(false);
            camera_1.SetActive(false);
            camera_2.SetActive(false);
            camera_3.SetActive(true);
        }
    } 
}

效果如下:
unity在同屏幕显示多Camera并在脚本中修改Viewport Rece

上一篇:一篇文章带你了解Android 最新Camera框架


下一篇:javascript调用摄像头