【问题标题】:unity switch cameras by UI Buttons通过 UI 按钮统一切换相机
【发布时间】:2015-07-29 15:29:09
【问题描述】:

我的脚本有问题。我需要通过 2 个按钮切换一组摄像头。但我的柜台有问题。当我尝试前后更换相机时,它无法正常工作。

public void Pressed () { 

    if (Next == true) {
        currCamIndex ++;
        Debug.Log ("index = " + currCamIndex);
        if (currCamIndex < cameras.Length) {
            cameras [currCamIndex - 1].gameObject.SetActive (false);
            cameras [currCamIndex].gameObject.SetActive (true);
        } else {
            cameras [currCamIndex - 1].gameObject.SetActive (false);
            currCamIndex = 0;
            cameras [currCamIndex].gameObject.SetActive (true);
        }
    }
    if (Prev == true) {
        currCamIndex --;
        if (currCamIndex >= 0) {
            cameras [currCamIndex + 1].gameObject.SetActive (false);
            cameras [currCamIndex].gameObject.SetActive (true);
        } else {
            cameras [currCamIndex + 1].gameObject.SetActive (false);
            currCamIndex = cameras.Length-1;
            cameras [currCamIndex].gameObject.SetActive (true);
        }
    }

    if (Back == true) {
        Application.LoadLevel(0);
    }

【问题讨论】:

    标签: camera unityscript


    【解决方案1】:

    尝试将活动相机的标签设置为“MainCamera”,将非活动相机的标签设置为“Untagged”或其他:

    public void Pressed () { 
    
    if (Next == true) {
        currCamIndex ++;
        Debug.Log ("index = " + currCamIndex);
        if (currCamIndex < cameras.Length) {
            cameras [currCamIndex - 1].gameObject.SetActive (false);
            cameras [currCamIndex].gameObject.SetActive (true);
            cameras [currCamIndex - 1].gameObject.tag = "Untagged";
            cameras [currCamIndex].gameObject.tag = "MainCamera";
        } else {
            cameras [currCamIndex - 1].gameObject.SetActive (false);
            cameras [currCamIndex - 1].gameObject.tag = "Untagged";
            currCamIndex = 0;
            cameras [currCamIndex].gameObject.SetActive (true);
            cameras [currCamIndex].gameObject.tag = "MainCamera";
        }
    }
    if (Prev == true) {
        currCamIndex --;
        if (currCamIndex >= 0) {
            cameras [currCamIndex + 1].gameObject.SetActive (false);
            cameras [currCamIndex].gameObject.SetActive (true);
            cameras [currCamIndex + 1].gameObject.tag = "Untagged";
            cameras [currCamIndex].gameObject.tag = "MainCamera";
        } else {
            cameras [currCamIndex + 1].gameObject.SetActive (false);
            cameras [currCamIndex + 1].gameObject.tag = "Untagged";
            currCamIndex = cameras.Length-1;
            cameras [currCamIndex].gameObject.SetActive (true);
            cameras [currCamIndex].gameObject.tag = "MainCamera";
        }
    }
    
    if (Back == true) {
        Application.LoadLevel(0);
    }
    

    由于 SetActive 调用的多个分支和标签重新分配,代码有点重复。如果是我的代码,我会消除这种冗余,但是这个代码示例可以用于说明目的。

    【讨论】:

    • 哦..我忘了这个问题。我用统计变量 currCamindex 决定了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2021-10-14
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多