Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检测颜色是否匹配_C#_Unity3d_Unity2d - Fatal编程技术网

C# 检测颜色是否匹配

C# 检测颜色是否匹配,c#,unity3d,unity2d,C#,Unity3d,Unity2d,我试图检测Unity2D中的颜色是否匹配(2x2),但它不能完全工作。我有16块4种不同颜色的瓷砖。 我正在检测颜色是否与脚本匹配,脚本附加到每个碰撞器上,碰撞器周围有4个瓷砖。在每个colider前面都有一个按钮,该按钮将围绕该按钮(碰撞器)向右旋转4个平铺。碰撞器的脚本有一个名为childTiles的列表。该列表包含碰撞器周围的4个平铺,用于检测一个列表中是否有4个颜色相同的平铺。看起来像这样。(GIF) 此脚本附加到每个碰撞器。总共有9个对撞机(每个都在一个按钮后面) 公共游戏对象游戏管理

我试图检测Unity2D中的颜色是否匹配(2x2),但它不能完全工作。我有16块4种不同颜色的瓷砖。 我正在检测颜色是否与脚本匹配,脚本附加到每个碰撞器上,碰撞器周围有4个瓷砖。在每个colider前面都有一个按钮,该按钮将围绕该按钮(碰撞器)向右旋转4个平铺。碰撞器的脚本有一个名为childTiles的列表。该列表包含碰撞器周围的4个平铺,用于检测一个列表中是否有4个颜色相同的平铺。看起来像这样。(GIF)

此脚本附加到每个碰撞器。总共有9个对撞机(每个都在一个按钮后面)

公共游戏对象游戏管理器;
public List ChildTiles=新列表();
公共游戏对象[]位置;
//公共游戏对象瓷砖;
public bool canTurn=true;
私人TileController家长职位;
私有浮动旋转度;
私人游戏经理;
公共色绿色;
公共色红色;
公共色蓝色;
公共色黄色;
公共bool greenComplete=false;
公共bool redComplete=false;
公共bool blueComplete=false;
公共bool yellowComplete=假;
公共游戏对象左上角//碰撞器(9个碰撞器中只有这些碰撞器是必需的)
公共游戏对象权限;
公共游戏对象左下角;
公共游戏对象权;
公共游戏对象tileGreen1;
公共游戏对象tileRed1;
公共游戏对象tileBlue1;
公共游戏对象1;
无效唤醒()
{
if(gameObject.name==“左上”| | gameObject.name==“右上”| | gameObject.name==“左下”| | gameObject.name==“右下”)
{
绿色=tileGreen1.GetComponent().color;
红色=tileRed1.GetComponent().color;
Blue=tileBlue1.GetComponent().color;
黄色=tileYellow1.GetComponent().color;
}
GameManager=GameObject.Find(“GameManager”);
//Tiles=GameObject.Find(“Tiles”);
ParentPositions=GameObject.FindGameObjectsWithTag(“位置”);
}
无效更新()
{
greenComplete=parentPositions.greenComplete;
redComplete=parentPositions.redComplete;
blueComplete=parentPositions.blueComplete;
yellowComplete=parentPositions.yellowComplete;
}
void Start()
{
gameManager=gameManager.GetComponent();
_旋转度=0f;
foreach(游戏对象进入父位置)
{
parentPositions=go.GetComponent();
}
}
公共空间()
{
if(parentPositions.canTurn==true&&gameManager.turns>0f)
{
gameManager.turns-=1;
gameManager.turnsText.text=“Turns:+gameManager.Turns;
foreach(游戏对象进入父位置)
{
parentPositions=go.GetComponent();
parentPositions.canTurn=false;
}
foreach(子图块中的游戏对象图块)
{
tile.transform.parent=gameObject.transform;
}
start例程(Rotate());//这是旋转父对象的地方。
如果(gameManager.turns==0f)
{
gameManager.turnsText.text=“不左转”;
}
}
}
IEnumerator旋转()
{
LeanTween.rotateez(游戏对象,旋转度-90f,0.5f);
_旋转度-=90f;
如果(_旋转度==-360f)
{
_旋转度=0f;
}
收益率返回新的WaitForSeconds(0.5f);
parentPositions.canTurn=true;
收益率返回新的WaitForSeconds(0.5f);
if(parentPositions.greenComplete==false)
{
parentPositions.greenComplete=parentPositions.ChildTiles.All(t=>t.GetComponent().color==Green);
}
if(parentPositions.redComplete==false)
{
parentPositions.redComplete=parentPositions.ChildTiles.All(t=>t.GetComponent().color==Red);
}
if(parentPositions.blueComplete==false)
{
parentPositions.blueComplete=parentPositions.ChildTiles.All(t=>t.GetComponent().color==Blue);
}
if(parentPositions.yellowComplete==false)
{
parentPositions.yellowComplete=parentPositions.ChildTiles.All(t=>t.GetComponent().color==Yellow);
}
Debug.Log(“_greenComplete”+parentPositions.greenComplete);
Debug.Log(“\u redComplete”+parentPositions.redComplete);
Debug.Log(“_blueComplete”+parentPositions.blueComplete);
Debug.Log(“\u yellowComplete”+parentPositions.yellowComplete);
if(parentPositions.greenComplete&&parentPositions.redComplete&&parentPositions.blueComplete&&parentPositions.yellowComplete)
{
调试日志(“级别完成”);
}
}
无效OnTriggerEnter2D(碰撞的R2D列)
{
如果(col.gameObject.tag==“平铺”)
{
添加(col.gameObject);
}
}
无效OnTriggerExit2D(碰撞的R2D列)
{
如果(col.gameObject.tag==“平铺”)
{
移除(游戏对象列);
}
}

正如您在GIF中看到的,存在一个问题。问题是当有4种颜色匹配时,它不会返回True。仅当您单击周围有4个相同颜色的平铺的按钮时,才会返回True。仅当单击按钮时,才会返回True。所以你匹配4种颜色,它不会返回真值,你点击按钮,周围有这4种颜色,它会返回真值。如果4种颜色匹配,如何使其立即返回真值?如你所见,我只使用左上、右上、左下和右下碰撞器,因为这些是唯一可能同时具有4个相同颜色的平铺的碰撞器。

在您将ChildTiles中的平铺添加到触发器事件中的列表后,检查它们是否都具有相同的颜色如何?在触发器事件中。没有想到这一点。我明天会试试的,谢谢。试过了。我把代码放在
void OnTriggerEnter2D(collizer2d col){if(col.gameObject.tag==“Tile”){
中,但它总是返回True。你知道吗?有人回答吗?
public GameObject GameManager;

public List<GameObject> ChildTiles = new List<GameObject>();
public GameObject[] ParentPositions;
//public GameObject Tiles;
public bool canTurn = true;
private TileController parentPositions;
private float _rotationDegrees;
private GameManager gameManager;

public Color Green;
public Color Red;
public Color Blue;
public Color Yellow;
public bool greenComplete = false;
public bool redComplete = false;
public bool blueComplete = false;
public bool yellowComplete = false;

public GameObject Topleft; //colliders (only these colliders are necessary out of the 9)
public GameObject Topright;
public GameObject Bottomleft;
public GameObject Bottomright;

public GameObject tileGreen1;
public GameObject tileRed1;
public GameObject tileBlue1;
public GameObject tileYellow1;

void Awake()
{
    if (gameObject.name == "Top-left" || gameObject.name == "Top-right" || gameObject.name == "Bottom-left" || gameObject.name == "Bottom-right")
    {
        Green = tileGreen1.GetComponent<SpriteRenderer> ().color;
        Red = tileRed1.GetComponent<SpriteRenderer> ().color;
        Blue = tileBlue1.GetComponent<SpriteRenderer> ().color;
        Yellow = tileYellow1.GetComponent<SpriteRenderer> ().color;
    }
    GameManager = GameObject.Find ("GameManager");
    //Tiles = GameObject.Find ("Tiles");
    ParentPositions = GameObject.FindGameObjectsWithTag ("Position");
}

void Update()
{
    greenComplete = parentPositions.greenComplete;
    redComplete = parentPositions.redComplete;
    blueComplete = parentPositions.blueComplete;
    yellowComplete = parentPositions.yellowComplete;
}

void Start()
{

    gameManager = GameManager.GetComponent<GameManager> ();
    _rotationDegrees = 0f;
    foreach (GameObject go in ParentPositions) 
    {
        parentPositions = go.GetComponent<TileController> ();
    }
}


public void SquareClicked()
{
    if (parentPositions.canTurn == true && gameManager.turns > 0f)
        {
        gameManager.turns -= 1;
        gameManager.turnsText.text = "Turns: " + gameManager.turns;
        foreach(GameObject go in ParentPositions)
        {
            parentPositions = go.GetComponent<TileController> ();
            parentPositions.canTurn = false;
        }

        foreach(GameObject tile in ChildTiles)
        {
            tile.transform.parent = gameObject.transform;
        }
        StartCoroutine(Rotate()); //this is where you do the rotation of the parent object.

        if (gameManager.turns == 0f) 
        {
            gameManager.turnsText.text = "No turns left";
        }
    }
}

IEnumerator Rotate()
{
    LeanTween.rotateZ (gameObject, _rotationDegrees - 90f, 0.5f);
    _rotationDegrees -= 90f;
    if (_rotationDegrees == -360f) 
    {
        _rotationDegrees = 0f;
    }
    yield return new WaitForSeconds (0.5f);
    parentPositions.canTurn = true;

    yield return new WaitForSeconds (0.5f);
    if (parentPositions.greenComplete == false) 
    {
        parentPositions.greenComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Green);
    }
    if (parentPositions.redComplete == false) 
    {
        parentPositions.redComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Red);
    }
    if (parentPositions.blueComplete == false) 
    {
        parentPositions.blueComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Blue);
    }
    if (parentPositions.yellowComplete == false) 
    {
        parentPositions.yellowComplete = parentPositions.ChildTiles.All (t => t.GetComponent<SpriteRenderer> ().color == Yellow);
    }

    Debug.Log ("_greenComplete " + parentPositions.greenComplete);
    Debug.Log ("_redComplete " + parentPositions.redComplete);
    Debug.Log ("_blueComplete " + parentPositions.blueComplete);
    Debug.Log ("_yellowComplete " + parentPositions.yellowComplete);

    if (parentPositions.greenComplete && parentPositions.redComplete && parentPositions.blueComplete && parentPositions.yellowComplete) 
    {
        Debug.Log ("Level Complete");
    }

}

void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.tag == "Tile") 
    {
        ChildTiles.Add (col.gameObject);
    }
}

void OnTriggerExit2D(Collider2D col)
{
    if (col.gameObject.tag == "Tile") 
    {
        ChildTiles.Remove (col.gameObject);
    }
}