C# 在相机之间切换时如何更改显示

C# 在相机之间切换时如何更改显示,c#,unity3d,C#,Unity3d,我试图通过激活不同的摄像机,根据输入轴的值来创建一个切换摄像机的代码 这是代码的样子: public class CameraManager : MonoBehaviour { public Camera mainCamera; public Camera rightCamera; public Camera leftCamera; public float axisValue; // Start is called before the first

我试图通过激活不同的摄像机,根据输入轴的值来创建一个切换摄像机的代码

这是代码的样子:

public class CameraManager : MonoBehaviour
{

    public Camera mainCamera;
    public Camera rightCamera;
    public Camera leftCamera;

    public float axisValue;

    // Start is called before the first frame update
    void Start()
    {
        SetMainCamera();
    }

    // Update is called once per frame
    void Update()
    {
        // gets value of "Camera Position" value
        axisValue = Input.GetAxis("Camera Position");
        ToggleCamera();
    }

    // manages cameras based on axis values
    public void ToggleCamera()
    {
        if (axisValue > 0)
        {
            SetRightCamera();
        }
        else
        if (axisValue < 0)
        {
            SetLeftCamera();
        }
        else 
        if (axisValue == 0)
        {
            SetMainCamera();
        }
    }

    // Sets camera to the mainCamera
    void SetMainCamera()
    {
        mainCamera.enabled = true;
        rightCamera.enabled = false;
        leftCamera.enabled = false;
    }

    // Sets camera to rightCamera
    void SetRightCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = true;
        leftCamera.enabled = false;
    }

    // Sets camera to leftCamera
    void SetLeftCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = false;
        leftCamera.enabled = true;
    }
}
public GameObject mainCamera;
public GameObject rightCamera;
public GameObject leftCamera;
public GameObject firstPerson;

public bool povToggle;

private float axisValue;

// Start is called before the first frame update
void Start()
{
    SetMainCamera();
}

// Update is called once per frame
void Update()
{
    // gets value of "Camera Position" value
    axisValue = Input.GetAxis("Camera Position");
    ToggleCamera();
}

// manages cameras based on axis values
public void ToggleCamera()
{
    if (axisValue > 0)
    {
        SetRightCamera();
    }
    else
    if (axisValue < 0)
    {
        SetLeftCamera();
    }
    else 
    if (axisValue == 0)
    {
        SetMainCamera();
    }
}

// Old code
/*
void SetMainCamera()
{
    mainCamera.enabled = true;
    rightCamera.enabled = false;
    leftCamera.enabled = false;
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = true;
    leftCamera.enabled = false;
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = false;
    leftCamera.enabled = true;
}
*/

// Sets camera to the mainCamera
void SetMainCamera()
{
    mainCamera.SetActive(true);
    rightCamera.SetActive(false);
    leftCamera.SetActive(false);
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(true);
    leftCamera.SetActive(false);
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(false);
    leftCamera.SetActive(true);
}
公共类CameraManager:MonoBehavior
{
公共摄像机;
公共摄像机;
公共摄像机;
公共价值;
//在第一帧更新之前调用Start
void Start()
{
SetMainCamera();
}
//每帧调用一次更新
无效更新()
{
//获取“摄影机位置”值的值
axisValue=Input.GetAxis(“摄影机位置”);
切换摄像头();
}
//基于轴值管理摄影机
公共无效切换摄影机()
{
如果(axisValue>0)
{
SetRightCamera();
}
其他的
if(值<0)
{
SetLeftCamera();
}
其他的
如果(axisValue==0)
{
SetMainCamera();
}
}
//将摄影机设置为主摄影机
void SetMainCamera()
{
mainCamera.enabled=true;
rightCamera.enabled=false;
leftCamera.enabled=false;
}
//将摄影机设置为右摄影机
void SetRightCamera()
{
mainCamera.enabled=false;
rightCamera.enabled=true;
leftCamera.enabled=false;
}
//将摄影机设置为左摄影机
void SetLeftCamera()
{
mainCamera.enabled=false;
rightCamera.enabled=false;
leftCamera.enabled=true;
}
}
这段代码实际上启用和禁用了每个摄像头,但它不显示已启用的摄像头(我必须手动更改游戏视图中的显示)


有没有办法编写脚本,使游戏可以根据输入轴更改显示?

您不能随意更改正在使用的显示。首先,您必须通过激活所述显示器()来配置多屏幕设置(告诉Unity要使用哪个显示器)。之后,Unity将始终使用/占据这些底片,即使您不渲染它们

要渲染到显示,必须将摄影机的目标显示设置为该显示(例如,通过检查器)()。之后,摄像机将对准正确的显示器


但是,您不能在编辑器中测试此功能,但必须构建应用程序(如果我正确回忆起上次使用此功能的项目)。

我现在意识到,我应该指定相机的用途。显示器用于同时在同一显示器上添加游戏的第二视图,如Thomas提供的中所示

我想要的是通过按下一个按钮来改变玩家的视角(想象一下玩一个斗狗游戏,按下一个按钮来看看你身后是什么)。解决这个问题的方法其实相当简单,或多或少:

1。)(假设主摄影机已存在)在层次中创建一个或多个新摄影机[右键单击层次>摄影机]

2.)将摄像头的目标显示设置为显示1(如果摄像头的目标显示不同,则更改视图将不起作用。)

3.)制作一个名为“CameraManager”的新游戏对象,并制作一个新脚本

4.)而不是使用
Display.Activate()
gameObject.enable=true,而使用
gameObject.SetActivate(true),这基本上会禁用和启用摄像头,具体取决于您使用哪个按钮在摄像头之间切换

这是最终代码的样子:

public class CameraManager : MonoBehaviour
{

    public Camera mainCamera;
    public Camera rightCamera;
    public Camera leftCamera;

    public float axisValue;

    // Start is called before the first frame update
    void Start()
    {
        SetMainCamera();
    }

    // Update is called once per frame
    void Update()
    {
        // gets value of "Camera Position" value
        axisValue = Input.GetAxis("Camera Position");
        ToggleCamera();
    }

    // manages cameras based on axis values
    public void ToggleCamera()
    {
        if (axisValue > 0)
        {
            SetRightCamera();
        }
        else
        if (axisValue < 0)
        {
            SetLeftCamera();
        }
        else 
        if (axisValue == 0)
        {
            SetMainCamera();
        }
    }

    // Sets camera to the mainCamera
    void SetMainCamera()
    {
        mainCamera.enabled = true;
        rightCamera.enabled = false;
        leftCamera.enabled = false;
    }

    // Sets camera to rightCamera
    void SetRightCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = true;
        leftCamera.enabled = false;
    }

    // Sets camera to leftCamera
    void SetLeftCamera()
    {
        mainCamera.enabled = false;
        rightCamera.enabled = false;
        leftCamera.enabled = true;
    }
}
public GameObject mainCamera;
public GameObject rightCamera;
public GameObject leftCamera;
public GameObject firstPerson;

public bool povToggle;

private float axisValue;

// Start is called before the first frame update
void Start()
{
    SetMainCamera();
}

// Update is called once per frame
void Update()
{
    // gets value of "Camera Position" value
    axisValue = Input.GetAxis("Camera Position");
    ToggleCamera();
}

// manages cameras based on axis values
public void ToggleCamera()
{
    if (axisValue > 0)
    {
        SetRightCamera();
    }
    else
    if (axisValue < 0)
    {
        SetLeftCamera();
    }
    else 
    if (axisValue == 0)
    {
        SetMainCamera();
    }
}

// Old code
/*
void SetMainCamera()
{
    mainCamera.enabled = true;
    rightCamera.enabled = false;
    leftCamera.enabled = false;
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = true;
    leftCamera.enabled = false;
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.enabled = false;
    rightCamera.enabled = false;
    leftCamera.enabled = true;
}
*/

// Sets camera to the mainCamera
void SetMainCamera()
{
    mainCamera.SetActive(true);
    rightCamera.SetActive(false);
    leftCamera.SetActive(false);
}

// Sets camera to rightCamera
void SetRightCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(true);
    leftCamera.SetActive(false);
}

// Sets camera to leftCamera
void SetLeftCamera()
{
    mainCamera.SetActive(false);
    rightCamera.SetActive(false);
    leftCamera.SetActive(true);
}
公共游戏对象主摄像头;
公共游戏机;
公共游戏机;
公开游戏对象第一人称;
公共场所;
私人价值;
//在第一帧更新之前调用Start
void Start()
{
SetMainCamera();
}
//每帧调用一次更新
无效更新()
{
//获取“摄影机位置”值的值
axisValue=Input.GetAxis(“摄影机位置”);
切换摄像头();
}
//基于轴值管理摄影机
公共无效切换摄影机()
{
如果(axisValue>0)
{
SetRightCamera();
}
其他的
if(值<0)
{
SetLeftCamera();
}
其他的
如果(axisValue==0)
{
SetMainCamera();
}
}
//旧代码
/*
void SetMainCamera()
{
mainCamera.enabled=true;
rightCamera.enabled=false;
leftCamera.enabled=false;
}
//将摄影机设置为右摄影机
void SetRightCamera()
{
mainCamera.enabled=false;
rightCamera.enabled=true;
leftCamera.enabled=false;
}
//将摄影机设置为左摄影机
void SetLeftCamera()
{
mainCamera.enabled=false;
rightCamera.enabled=false;
leftCamera.enabled=true;
}
*/
//将摄影机设置为主摄影机
void SetMainCamera()
{
main camera.SetActive(真);
rightCamera.SetActive(错误);
leftCamera.SetActive(假);
}
//将摄影机设置为右摄影机
void SetRightCamera()
{
main camera.SetActive(假);
rightCamera.SetActive(真);
leftCamera.SetActive(假);
}
//将摄影机设置为左摄影机
void SetLeftCamera()
{
main camera.SetActive(假);
rightCamera.SetActive(错误);
leftCamera.SetActive(真);
}

change the display(更改显示)是什么意思?在游戏视图的左上角有一个选项,允许您选择不同的显示,因为有多个摄像头,显示1是默认设置。我的问题是,虽然脚本确实启用和禁用了摄影机,但游戏视图不显示已启用的摄影机。它只是停留在显示屏1上。