Esempio n. 1
0
    //创建格子
    public void Generate()
    {
        Clear();

        //计算左下角的位置,为了得到偏移量,使得每次创建的方格盘都能显示在屏幕中间
        float minX    = -(size.x * cellSize + (size.x - 1) * interval) / 2;
        float minY    = -(size.y * cellSize + (size.y - 1) * interval) / 2;
        float offsetX = minX + cellSize / 2;
        float offsetY = minY + cellSize / 2;

        for (int i = 0; i < size.x; i++)
        {
            for (int j = 0; j < size.y; j++)
            {
                RawImage cell = new GameObject(i + "," + j).AddComponent <RawImage>();
                cell.transform.SetParent(gridAnchor);
                cell.transform.localPosition = new Vector3(i * (cellSize + interval) + offsetX, j * (cellSize + interval) + offsetY);
                cell.rectTransform.sizeDelta = new Vector2(cellSize, cellSize);

                Vector2Int   pos      = new Vector2Int(i, j);
                GridCellCtrl cellCtrl = new GridCellCtrl(cell, pos);
                //添加记录
                _dicPos2Ctrl.Add(pos, cellCtrl);
                _dicGO2Ctrl.Add(cell.gameObject, cellCtrl);
            }
        }
    }
Esempio n. 2
0
    public void OnPointerClick(PointerEventData eventData)
    {
        GameObject pressGO = eventData.pointerEnter;

        if (_dicGO2Ctrl.ContainsKey(pressGO))
        {
            //DrawLine(_dicGO2Ctrl[pressGO].pos);
            GridCellCtrl ctrl = _dicGO2Ctrl[pressGO];
            _start = ctrl.pos;
            ClearLine();
            ctrl.SetColor(Color.red);
        }
    }
Esempio n. 3
0
    void DrawLine(Vector2Int end)
    {
        ClearLine();

        _dicPos2Ctrl[_start].SetColor(Color.red);

        GridCellCtrl ctrl = _dicPos2Ctrl[end];

        ctrl.SetColor(Color.green);

        List <Vector2Int> touchedGrids = GridHelper.GetTouchedPosBetweenTwoPoints(_start, ctrl.pos);

        for (int i = 0; i < touchedGrids.Count; i++)
        {
            _dicPos2Ctrl[touchedGrids[i]].SetColor(Color.black);
        }
    }