[英]how do I deactivate all cameras in a list except for the one that is currently in use?
I have a list of cameras that the player can switch through using the up and down keys to look around with.我有一个摄像机列表,玩家可以使用向上和向下键切换以环顾四周。
Instead of all cameras being on at the same time when the game starts, I would like it if they were all off until the player switches over to them, then it becomes active.我希望在游戏开始时所有摄像机都关闭,而不是在游戏开始时所有摄像机都关闭,直到玩家切换到它们,然后它才会激活。 then if I switch again the next camera becomes active and the previous becomes inactive and so on and so forth.
然后,如果我再次切换,下一个摄像头将变为活动状态,而前一个摄像头将变为非活动状态,依此类推。
How do I do this?我该怎么做呢?
public class CameraManager : MonoBehaviour
{
[SerializeField]
public List<Camera> cameras = new List<Camera>();
public int currentCamera;
public void Awake ()
{
cameras.Add(Camera.main);
cameras.AddRange(FindObjectsOfType<Camera>());
}
void incCamera()
{
cameras [currentCamera].enabled = false;
currentCamera++;
if(currentCamera >= cameras.Count){
currentCamera = 0;
}
cameras [currentCamera].enabled = true;
}
void decCamera()
{
cameras [currentCamera].enabled = false;
currentCamera--;
if(currentCamera < 0){
currentCamera = cameras.Count-1;
}
cameras [currentCamera].enabled = true;
}
public void Update()
{
if (Input.GetKeyUp(KeyCode.UpArrow))
{
incCamera();
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
decCamera();
}
I guess you can move on your list and if current camera is not Camera.main, you can deactivate it:我想您可以在列表中移动,如果当前相机不是 Camera.main,您可以停用它:
// based on comments, it's better to change your Awake method
private void Awake()
{
_cameras.AddRange(FindObjectsOfType<Camera>());
}
private void Start()
{
foreach (var camera1 in _cameras)
{
if (camera1 == Camera.main) continue;
camera1.gameObject.SetActive(false);
}
// using LINQ
// foreach (var camera1 in _cameras.Where(camera1 => camera1 != Camera.main))
// {
// camera1.gameObject.SetActive(false);
// }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.