简体   繁体   English

如何停用列表中除当前正在使用的相机之外的所有相机?

[英]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.

相关问题 我如何停用阵列中除 ONE 之外的所有游戏对象? - How do I deactivate all game objects in an array except ONE? 仅按下一个按钮后,如何停用 Vuforia 中的所有虚拟按钮? - How do I deactivate all the virtual buttons in Vuforia after pressing just one? 如何在 Unity 中创建摄像机列表? - How do you create a list of Cameras in Unity? innertext返回所有子文本和自身文本。 除了一个孩子的文字,我该怎么办 - innertext return all the child and self text. how do i except one child text 如何过滤除特定白名单之外的所有 HTML 标签? - How do I filter all HTML tags except a certain whitelist? 我如何将对象转换为具有所有相同属性的另一种对象类型,但新类型还有一个字段 - How do I cast an object to another object type with all the same properties except the new type has one more field 如何停用Unity中的所有孩子? - How to deactivate all children in Unity? 除了一个索引,你如何对列表中的每个元素“做某事”? - How do you 'do something' to every element in a list except that one index? 如何删除除一个以外的所有控件? - How to remove all controls except one? 如何使用c#检索所有当前打开的内存映射文件的列表? - How can I retrieve a list of all currently open memory mapped files using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM