当前位置:   article > 正文

Unity绘制GUI连连看(尚未完善效果和重置)

unity连连看游戏制作

 

OneImage.cs

public class OneImage : MonoBehaviour
{
    public int row, col;
    public Rect rect;
    public Texture texture;
    public bool walkable = true;
    public bool isinit = false;
    public bool selected = false;

    public OneImage()
    { }

    public OneImage(Rect rect,int row,int col)
    {
        this.row = row;
        this.col = col;
        this.rect = rect;
        this.walkable = true;
    }
}

 

LinkLinkSee.cs

public class LinkLinkSee : MonoBehaviour
{
    public int rows, cols;
    public Texture black, wayTexture;
    public Texture[] textures;
    private OneImage[][] images;

    bool game = true;
    int upStep = 0;

    void Start()
    {
        StartCoroutine("GameInit");
    }

    IEnumerator GameInit()
    {
        game = false;
        rows += upStep;
        cols += upStep;

        int realRows = rows + 2;
        int realCols = cols + 2;

        float w = Screen.height / realRows;
        float x = Screen.width / 2 - ((w * realCols + realCols) / 2);
        float y = Screen.height / 2 - (w * realRows / 2);

        images = new OneImage[realRows][];
        for (int row = 0; row < realRows; row++)
        {
            images[row] = new OneImage[realCols];
            for (int col = 0; col < realCols; col++)
            {
                images[row][col] = new OneImage(new Rect(x, y, w, w), row, col);
                images[row][col].texture = black;
                x += w + 1;
            }
            x = Screen.width / 2 - ((w * realCols + realCols) / 2);
            y += w + 1;
        }

        for (int row = 1; row < realRows - 1; row++)
        {
            for (int col = 1; col < realCols - 1; col++)
            {
                if (images[row][col].isinit)
                {
                    continue;
                }
                else
                {
                    int textureIndex = Random.Range(0, textures.Length);
                    images[row][col].texture = textures[textureIndex];
                    images[row][col].isinit = true;
                    images[row][col].walkable = false;

                    int newRow = 1, newCol = 1;
                    int max = rows * cols;
                    while (images[newRow][newCol].isinit)
                    {
                        newRow = Random.Range(row, realRows - 1);
                        newCol = Random.Range(1, realCols - 1);
                        max--;
                        if (max <= 0)
                            break;
                    }
                    images[newRow][newCol].texture = textures[textureIndex];
                    images[newRow][newCol].isinit = true;
                    images[newRow][newCol].walkable = false;
                    yield return null;
                }
            }
        }
        game = true;
    }

    void OnGUI()
    {
        if (game)
        {
            bool isOver = true;
            for (int row = 0; row < rows + 2; row++)
            {
                for (int col = 0; col < cols + 2; col++)
                {
                    OneImage img = images[row][col];

                    if (!img.walkable)
                    {
                        isOver = false;
                    }

                    GUI.DrawTexture(img.rect, img.texture);
                    //GUI.Label(img.rect, img.walkable.ToString());
                }
            }
            if (isOver)
            {
                game = false;
                upStep++;
                StartCoroutine("GameInit");
            }
        }
        else
        {
            for (int row = 0; row < rows + 2; row++)
            {
                for (int col = 0; col < cols + 2; col++)
                {
                    OneImage img = images[row][col];
                    GUI.DrawTexture(img.rect, img.texture);
                }
            }
        }
    }

    private OneImage ChoseFirst = new OneImage(), ChoseSecond = new OneImage();
    List<OneImage> FWay;
    List<OneImage> SWay;

    void Update()
    {
        if (Input.GetMouseButtonDown(0)&&game)
        {
            try
            {
                if (!ChoseFirst.selected)
                {
                    ChoseFirst = GetChose(Input.mousePosition);
                    ChoseFirst.selected = true;
                }
                else
                {
                    ChoseSecond = GetChose(Input.mousePosition);
                    if (CompareImg(ChoseFirst, ChoseSecond))
                    {
                        ChoseFirst.selected = false;
                    }
                    else
                    {
                        if (ChoseSecond.texture == ChoseFirst.texture)
                        {
                            FindWay(ChoseFirst, ChoseSecond);
                            ChoseFirst.selected = false;
                        }
                        else
                        {
                            ChoseFirst = ChoseSecond;
                            ChoseFirst.selected = true;
                        }
                    }
                }
            }
            catch
            {
                ChoseFirst = new OneImage();
                ChoseSecond = new OneImage();
            }
        }
    }

    private OneImage GetChose(Vector2 mousePos)
    {
        mousePos.y = Screen.height - mousePos.y;
        for (int i = 0; i < rows + 2; i++)
        {
            for (int j = 0; j < cols + 2; j++)
            {
                if (images[i][j].rect.Contains(mousePos))
                {
                    return images[i][j];
                }
            }
        }
        return null;
    }

    private void FindWay(OneImage F, OneImage S)
    {
        if (F.row == S.row && Mathf.Abs(F.col - S.col) == 1 || F.col == S.col && Mathf.Abs(F.row - S.row) == 1)
        {
            F.texture = wayTexture;
            F.walkable = true;
            S.texture = wayTexture;
            S.walkable = true;
        }
        else
        {
            FWay = GetSingleWay(F);
            SWay = GetSingleWay(S);

            List<List<OneImage>> WayWay = new List<List<OneImage>>();
            foreach (OneImage img in FWay)
            {
                List<OneImage> way = GetSingleWay(img);
                foreach (OneImage one in way)
                {
                    foreach (OneImage sone in SWay)
                    {
                        if (CompareImg(sone, one))
                        {
                            WayWay.Add(way);
                            break;
                        }
                    }
                }
            }

            if (WayWay.Count > 0)//找到的路线数量>0
            {
                F.texture = S.texture = this.wayTexture;
                F.walkable = S.walkable = true;
            }
        }
    }

    private List<OneImage> GetSingleWay(OneImage img)
    {
        List<OneImage> singleWay = new List<OneImage>();
        for (int i = img.row; i >= 0; i--)
        {
            if (CompareImg(images[i][img.col], img) || images[i][img.col].walkable)
            {
                singleWay.Add(images[i][img.col]);
            }
            else
                break;
        }

        for (int i = img.row; i < rows + 2; i++)
        {
            if (CompareImg(images[i][img.col], img) || images[i][img.col].walkable)
            {
                singleWay.Add(images[i][img.col]);
            }
            else
                break;
        }

        for (int i = img.col; i >= 0; i--)
        {
            if (CompareImg(images[img.row][i], img) || images[img.row][i].walkable)
            {
                singleWay.Add(images[img.row][i]);
            }
            else
                break;
        }

        for (int i = img.col; i < cols + 2; i++)
        {
            if (CompareImg(images[img.row][i], img) || images[img.row][i].walkable)
            {
                singleWay.Add(images[img.row][i]);
            }
            else
                break;
        }

        return singleWay;
    }

    private bool CompareImg(OneImage F, OneImage S)
    {
        if (F.row == S.row && F.col == S.col)
            return true;
        else
            return false;
    }
}

 

 

转载于:https://www.cnblogs.com/yoatl/p/4744105.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/146406
推荐阅读
相关标签
  

闽ICP备14008679号