通过2D网格坐标选取游戏地形(UI网格)
来源:互联网 发布:灵知的太阳信仰 编辑:程序博客网 时间:2024/06/06 23:10
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;
}
- 通过2D网格坐标选取游戏地形(UI网格)
- Unity3D动态创建地形网格(一)
- Unity3D动态创建地形网格(二)
- Unity3D动态地形网格一
- raphael - 创建网格坐标
- 三角网格(2)
- 网格
- 网格
- 网格
- 网格
- 高仿易信UI之网格视图(GridView)
- Android UI之GridLayout(网格布局)
- ui-grid 网格布局
- unity 2D Sprite网格Slice工具
- 3D导航网格
- 朴素的网格游戏
- 静态LOD地形网格索引计算
- vision引擎中 地形几何网格导出
- 深入mysql "ON DUPLICATE KEY UPDATE" 语法的分析
- 三年工作经验——感悟
- Linux自启动脚本
- MY SQL的字段带约束条件插入
- 非线性回归(上)
- 通过2D网格坐标选取游戏地形(UI网格)
- PyCharm、Python3和PyQt5环境搭建和简单使用
- DH 参数
- logstash 多input与
- 嵌入式项目1--制板流程
- C# 调用C++DLL传递的bool型,返回混乱(c++为false,c#确为ture)
- Spring Bean的生命周期
- xserver源码分析
- 用深度学习解决问答(QA)方法