通过2D网格坐标选取游戏地形(UI网格)

来源:互联网 发布:灵知的太阳信仰 编辑:程序博客网 时间:2024/06/06 23:10

通过2D网格坐标选取游戏地形。

步骤:
1.通过行列坐标对当前屏幕进行网格切分
2.确定最小的网格单元
3.获取点击位置信息,与最小单元相除,向下取整,从而获取点击位置信息的所属网格


网格系统创建方式:
1.以行列数为准,通过长宽的限定,确定单元网格大小
2.以单元网格大小为准,通过长宽的限定,确定行列数。


using System.Collections;

using System.Collections.Generic;
using UnityEngine;


public class SelectPosition : MonoBehaviour
{
    public int rows = 40;
    public int cloumns = 40;
    public bool isShowGrids = false;

    public GameObject tipPrefab;

    GameObject tip;
    UIGridsHelper helper;

    void Awake()
    {
        helper = new UIGridsHelper(rows, cloumns);
    }

    void Start()
    {
        helper.CreatTestInfos();

        tip = GameObject.Instantiate(tipPrefab);
        tip.transform.SetParent(this.transform);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            helper.GetGridsInfoID(Input.mousePosition);
            ShowTips();
        }


        if (isShowGrids)
            helper.ShowGrids();
    }


    /// <summary>
    /// 鼠标点击处 显示提示
    /// </summary>
    void ShowTips()
    {
        Vector3 followPosition;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(tip.GetComponent<RectTransform>(), Input.mousePosition, null, out followPosition))
            tip.GetComponent<RectTransform>().position = followPosition;
    }

}


using System.Collections.Generic;
using UnityEngine;

public class UIGridsHelper : MonoBehaviour
{
    private int totalRows = 2;
    private int totalCloumns = 2;

    public UIGridsHelper(int totalRows, int totalCloumns )
    {
        this.totalRows = totalRows;
        this.totalCloumns = totalCloumns;
    }

    public Vector2 CaculateGrid(Vector3 pos)
    {
        Vector2 cell = GetCell();

        Vector2 grid = new Vector2();
        grid.x = Mathf.FloorToInt(pos.x / cell.x);
        grid.y = Mathf.FloorToInt(pos.y / cell.y);

        return grid;
    }

    public int GetGridsInfoID(Vector3 pos)
    {
        int id = 0;
        Vector2 grid = CaculateGrid(pos);

        List<GridInfo> infos = CreatTestInfos();
        foreach (var item in infos)
        {
            if (item.grid.x == grid.x && item.grid.y == grid.y)
            {
                id = item.id;
                break;
            }
        }

        Debug.LogError(id);
        return id;
    }

    public Vector2 GetCell()
    {
        Vector2 cellSize = new Vector2();
        cellSize.x = Mathf.FloorToInt(Screen.width / totalCloumns);
        cellSize.y = Mathf.FloorToInt(Screen.height / totalRows);
        return cellSize;
    }

    public void ShowGrids()
    {
        Vector2 cell = GetCell();

        int rowNumber = totalRows;
        int cloumnNumber = totalCloumns;

        for (int i = 0; i < rowNumber; i++)
            Debug.DrawLine(new Vector3(0, i * cell.y, 0), new Vector3(Screen.width, i * cell.y, 0));
        for (int i = 0; i < cloumnNumber; i++)
            Debug.DrawLine(new Vector3(i * cell.x, 0, 0), new Vector3(i * cell.x, Screen.height, 0));
    }


    /// <summary>
    /// 测试
    /// </summary>
    /// <returns></returns>
    public List<GridInfo> CreatTestInfos()
    {
        List<GridInfo> target = new List<GridInfo>();
        int id = -1;


        for (int i = 0; i < totalCloumns; i++)
        {
            id++;
            for (int j = 0; j < totalRows; j++)
            {
                id++;
                GridInfo info = new GridInfo();
                info.id = id;
                info.grid = new Vector2(i, j);


                target.Add(info);
            }
        }


        return target;
    }


}


public class GridInfo
{
    public int id;
    public Vector2 grid;
}



原创粉丝点击