【问题标题】:Unity Multiple Toggle Buttons ReversingUnity 多个切换按钮反转
【发布时间】:2020-01-16 05:04:51
【问题描述】:

我正在制作一个统一项目。情况是我有两个切换按钮(实际上不止两个)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);
        }
    }
}

【问题讨论】:

    标签: c# unity3d button 3d toggle


    【解决方案1】:

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

    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);
        }
    }
    

    【讨论】:

    • 非常感谢法国人。你的代码就像一个魅力。
    【解决方案2】:

    如果我理解得很好,您只想基于多个Toggles 启用一个 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

    【讨论】:

    • 感谢您的宝贵时间 Malphegal。但是切换根本不会激活立方体。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2012-04-28
    • 2012-05-29
    相关资源
    最近更新 更多