UI按钮统一切换摄像头

时间:2015-05-18 08:01:34

标签: camera unityscript

我的sript有问题。我需要通过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);
    }

1 个答案:

答案 0 :(得分:0)

尝试将有源相机的标签设置为&#34; MainCamera&#34;和不活动的相机到&#34; Untagged&#34;或其他:

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调用的多个分支和标记重新分配,代码有点重复。如果是我的代码,我会消除这种冗余,但这个代码示例可以用于说明目的。