[英]How to change displays when switching between cameras
I'm trying to make a code that switches cameras based in the value of an Input Axis by activating the different cameras.我正在尝试制作一个代码,通过激活不同的相机来根据输入轴的值切换相机。
This is what the code looks like:这是代码的样子:
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;
}
}
This code does in fact enable and disable each camera but it does not display the enabled cameras (I would have to manually change the Display in the game view).这段代码实际上确实启用和禁用了每个摄像头,但它不显示启用的摄像头(我必须在游戏视图中手动更改显示)。
Is there a way to script so that the game would change the display based on the input axis?有没有办法编写脚本,让游戏根据输入轴改变显示?
I'm realizing now that I should have specified my purpose with the cameras.我现在意识到我应该明确我对相机的目的。 Displays are used for adding a second view of your game on the same monitor at the same time, as shown in the documentation provided by Thomas.显示器用于在同一显示器上同时添加游戏的第二个视图,如 Thomas 提供的文档中所示。
What I wanted was a change of view of the player by pressing a button (think of playing a dogfighting game and pressing a button to see what's behind you.) The solution to this is actually quite simple, more or less:我想要的是通过按下按钮来改变玩家的视角(想想玩狗斗游戏并按下按钮看看你身后是什么。)解决这个问题的方法实际上非常简单,或多或少:
1.) (Assuming the Main Camera already exists) Create a new camera or cameras in the Hierarchy [right-click on Hierarchy > Camera] 1.)(假设主相机已经存在)在层次结构中创建一个或多个新相机[右键单击层次结构>相机]
2.) Set the camera(s)' Target Display to Display 1 (changing view will not work if the cameras' Target Displays are different.) 2.) 将相机的目标显示器设置为显示器 1(如果相机的目标显示器不同,更改视图将不起作用。)
3.) Make a new GameObject called 'CameraManager' and make a new script as well. 3.) 创建一个名为“CameraManager”的新游戏对象并创建一个新脚本。
4.) Rather than using Display.Activate();
4.) 而不是使用Display.Activate();
or gameObject.enable = true;
或gameObject.enable = true;
, instead use gameObject.SetActivate(true);
,而是使用gameObject.SetActivate(true);
, this basically disables and enables the cameras depending on what button you are using to switch between the cameras. ,这基本上是根据您用来在相机之间切换的按钮来禁用和启用相机。
This is what the final code looks like:这是最终代码的样子:
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);
}
You cannot change the display you are using freely.您不能随意更改正在使用的显示器。 First you have to configure a multi screen setup (telling Unity which displays to use) by activating said displays ( see Display.Activate() in documentation ).首先,您必须通过激活所述显示器来配置多屏幕设置(告诉 Unity 使用哪个显示器)(请参阅文档中的 Display.Activate() )。 Afterwards Unity will always use/occupy those diplays, even if you do not render onto them.之后 Unity 将始终使用/占用这些显示,即使您没有渲染它们。
To render to a display you have to set the target display of a camera to that display (eg via the inspector) ( again documentation ).要渲染到显示器,您必须将相机的目标显示器设置为该显示器(例如通过检查器)(同样是文档)。 Afterwards the cameras will target the correct display.之后,摄像机将瞄准正确的显示器。
However you can not test this functionality in the editor, but have to build your application (if I recall correctly from my last project using this feature).但是,您无法在编辑器中测试此功能,但必须构建您的应用程序(如果我没记错的话,我在上一个项目中使用过此功能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.