使用颜色、按钮和 if 语句的简单 2d 程序

问题描述

有 3 个按钮。单击按钮将更改其颜色。这里的任务是将按钮的每种颜色与绿色相匹配。现在,我将按钮编程为循环显示 4 种颜色,即相同的调色板。当然可以。

我现在想要实现的是,一旦所有按钮都匹配颜色,就会显示文本。我正在开发 Unity,而 c# 脚本在 Visual Studio 上。

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

 public class ChangeColor : MonoBehavIoUr
    {
        [Serializefield] int count = 0;
        [Serializefield] Color[] colorArray;
       public void button ()
        {
            if(count < colorArray.Length)
            {
                gameObject.GetComponent<Image>().color = new Color(colorArray[count].r,colorArray[count].g,colorArray[count].b);
                if(count == colorArray.Length - 1)
                {
                    count = -1;
                }
                count += 1;
            }
            
        }
    }

解决方法

首先,您的 button 方法可以简化很多。

然后我会使用一个回调来在每次按下一个按钮时检查你的颜色:

public class ChangeColor : MonoBehaviour
{
    [SerializeField] int count = 0;
    [SerializeField] Color[] colorArray;

    [SerializeField] private Image _image;

    public Color CurrentColor => _image.color;

    public event Action OnChangedColor;

    private void Awake ()
    {
        if(!_image) _image = GetComponent<Image>();
    }

    public void button ()
    {
        var count = (count + 1) % colorArray.Length;
        var nextColor = colorArray[Count];

        _image.color = nextColor;

        OnChangedColor?.Invoke();
    }
}

现在在中央控制器脚本中,您可以引用所有按钮 ChangeColor 组件及其相应的目标 Color,例如

[Serializable]
public class ButtonColorPair
{
    public ChangeColor changeColor;
    public Color targetColor;

    // Returns true if a ChangeColor is referenced and the colors are matching
    public bool IsMatching => changeColor && changeColor.CurrentColor == tatgetColor;
}

public class ColorChecker : MonoBehaviour
{
    public ButtonColorPair[] buttonColorPairs;

    private void Awake ()
    {
        // Register a callback which is called every time one of the buttons
        // has changed the color
        foreach(var pair in buttonColorPairs)
        {
            // It is save to remove a callback even if it wasn't added before
            // but it makes sure there is only one single callback 
            pair.changeColor.OnChangedColor -= HandleChangedColor;
            pair.changeColor.OnChangedColor += HandleChangedColor;

            // Here you would also set the targetColor if you want to do it via code e.g. like
            pair.targetColor = Color.green;
            // or to be save e.g. pick a random value from the color array etc

            // Maybe this class would even be responsible for telling the buttons which colors are 
            // available at all
        }
    }

    private void OnDestroy()
    {
        // As a good practice always remove callbacks as soon as you don't need them anymore
        foreach(var pair in buttonColorPairs)
        {
            pair.changeColor.OnChangedColor -= HandleChangedColor;
        }
    }

    // Finally check if the color matches for all buttons everytime one of them was changed
    private void HandleChangedColor ()
    {
        // This is Linq and require "using System.Linq;" on top of your file
        if(buttonColorPairs.All(p => p.IsMatching)
        {
            Debug.Log("All buttons are matching their target color");
        }
        else
        {
            Debug.Log("Nope not all matching yet");
        }
        // it basically equals doing something like
        //bool allMatch = true;
        //foreach(var p in buttonColorPairs)
        //{
        //    if(!p.IsMatching) 
        //    {
        //        allMatch = false;
        //        break;
        //    }
        //}
        //if(allMatch)
        //{
        //    Debug.Log("All buttons are matching their target color");
        //}
        //else
        //{
        //    Debug.Log("Nope not all matching yet");
        //}
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...