Unity 多个切换按钮反转

Unity Multiple Toggle Buttons Reversing

我正在做一个统一项目。情况是我有两个切换按钮(实际上不止两个)toggle1 和 toggle2 以及两个游戏对象 cube1 和 cube2。开始时,两个切换都未选中,游戏对象为 SetActive(false)。我想要的是如果两个按钮 toggle1.isOn && toggle2.isOn 然后 cube1 SetActive 并且如果 toggle2.isOn && toggle1.isOn 然后 cube2 SetActive 根据切换顺序即首先选择哪个切换。 现在的问题是,当我检查 toggle1 然后 toggle2 以显示两个立方体时,当我检查 toggle2 然后 toggle1 然后再次出现两个立方体....

using System.Collections;
using UnityEngine;
using UnityEngine.UI;


public class ToggleToggle : MonoBehaviour {

    public Toggle toggle1;
    public Toggle toggle2;
    public GameObject cube;
    public GameObject cube2;

    // Use this for initialization
    void Start () {
        cube.SetActive (false);
        cube2.SetActive (false);
    }

    // Update is called once per frame
    void Update () {


        // Toggle1 Selected First then Toggle2
        if (toggle1.isOn && toggle2.isOn) {
            cube.SetActive (true);
        }

        // Toggle2 Selected First then Toggle1
        if (toggle2.isOn && toggle1.isOn) {
            cube2.SetActive (true);
        }
    }
}

您的程序中的错误是您忘记通知按下的第一个按钮:

public int first;
void Start () {
    cube.SetActive (false);
    cube2.SetActive (false);
    first = 0;
}

    // Update is called once per frame
void Update () {
    if (!toggle1.isOn && !toggle2.isOn) {
        first = 0;
    }

    if (first == 0 && toggle1.isOn) {
        first = 1;
    }        

    if (first == 0 && toggle2.isOn) {
        first = 2;
    } 

    if (first == 1 && toggle2.isOn) {
        cube.SetActive (true);
    }

    if (first == 2 && toggle1.isOn) {
        cube2.SetActive (true);
    }
}

如果我理解得很好,您只想启用基于多个 Toggle 一个 GameObject。 第一个打勾决定 GameObject.

所以你必须存储第一个打勾的Toggle

这是一个例子:

public class ToggleToggle : MonoBehaviour {
/*
    public Toggle toggle1;
    public Toggle toggle2;
    public GameObject cube;
    public GameObject cube2;
*/

    public Toggle[] toggles;
    public GameObject[] cubes;

    public static Toggle firstTickedToggle;

    void Start ()
    {
        for (int i = 0; i < cubes.Length; i++)
        {
            cubes[i].SetActive(false);
        }
    }

    public void OnToggleClick(Toggle toggle)
    {
        if (toggle.enabled)
        {
            if (firstTickedToggle == null)
                firstTickedToggle = toggle;

            int toggleIndex = 0; // Index of the first clicked toggle

            // Check if all toggles are ticked
            for (int i = 0; i < toggles.Length; i++)
            {
                // If one is not ticked
                if (!toggles[i].enabled)
                    return;
                if (toggles[i] == firstTickedToggle)
                    toggleIndex = i;
            }

            // Here all toggles are ticked
            cubes[toggleIndex].SetActive(true);
        }


        else
        {
            for (int i = 0; i < toggles.Length; i++)
            {
                // If one toggle is still ticked, don't do anything
                if (toggles[i].enabled)
                    return;
            }

            // If all toggles are unticked, remove the reference to the first ticked
            firstTickedToggle = null;
        }
    }
}

All toggles need a OnValueChanged