当前位置: 首页 > news >正文

UNITY 超快速 在UNITY画网格GRID

首先,下载一个shapes插件!

为shapes插件的立刻绘画模式创建一个脚本

using Shapes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;namespace XXX
{[ExecuteAlways]public class DrawController : ImmediateModeShapeDrawer{public Camera cam;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}// called by the ImmediateModeShapeDrawer base typepublic override void DrawShapes(Camera cam){if (cam != this.cam) // only draw in the player camerareturn;using (Draw.Command(cam)){Draw.ZTest = CompareFunction.Less ; // to make sure it draws on top of everything like a HUD                                                    //Draw.Matrix = crosshairTransform.localToWorldMatrix; // draw it in the space of crosshairTransformDraw.BlendMode = ShapesBlendMode.Transparent;Draw.LineGeometry = LineGeometry.Flat2D;Draw.Thickness = 0.01f;//在这里写draw函数}}}
}

public class GridPos{public int width;public int height;private float cellSize;public Vector3 originPosition;public GridPos(int width, int height, float cellSize, Vector3 originPosition){this.width = width;this.height = height;this.cellSize = cellSize;this.originPosition = originPosition;//bool showDebug = false;//if (showDebug) {//    //TextMesh[,] debugTextArray = new TextMesh[width, height];//    for (int x = 0; x < this.width; x++) {//        for (int y = 0; y < this.height; y++) {//            //debugTextArray[x, y] = UtilsClass.CreateWorldText("", null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter);//            Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);//            Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);//        }//    }//    Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);//    Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);//}}public int GetWidth(){return width;}public int GetHeight(){return height;}public float GetCellSize(){return cellSize;}public Vector3 GetWorldPosition(int x, int y){return new Vector3(x, y) * cellSize + originPosition;}public Vector3 GetWorldPosition2(int x, int y){return new Vector3(x, y) * cellSize + originPosition + new Vector3(cellSize / 2, cellSize / 2, 0);}//GetXY(worldPosition, out x, out y);public void GetXY(Vector3 worldPosition, out int x, out int y){x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);}public List<Vector3> GetAllJustPoint(){List<Vector3> m_linePoints = new List<Vector3>();for (int x = 0; x < this.width; x++){for (int y = 0; y < this.height; y++){m_linePoints.Add(GetWorldPosition(x, y));}}return m_linePoints;}public List<Vector3[]> GetAllPoint(){List<Vector3[]> m_linePoints = new List<Vector3[]>();for (int x = 0; x < this.width; x++){for (int y = 0; y < this.height; y++){Vector3[] rows = new Vector3[2];rows[0] = GetWorldPosition(x, y);rows[1] = GetWorldPosition(x, y + 1);m_linePoints.Add(rows);Vector3[] cols = new Vector3[2];//debugTextArray[x, y] = UtilsClass.CreateWorldText("", null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter);cols[0] = GetWorldPosition(x, y);cols[1] = GetWorldPosition(x + 1, y);m_linePoints.Add(cols);}}Vector3[] rows2 = new Vector3[2];rows2[0] = GetWorldPosition(0, height);rows2[1] = GetWorldPosition(width, height);m_linePoints.Add(rows2);Vector3[] cols2 = new Vector3[2];cols2[0] = GetWorldPosition(width, 0);cols2[1] = GetWorldPosition(width, height);m_linePoints.Add(cols2);return m_linePoints;}

在文件中创建这样一个脚本。

使用这句话创建一个网格

 GridPos gp1 =new GridPos((int)width, (int)height, 1, new Vector3(0.5f, 0.5f, 0) + offposition);
List<Vector3[]> m_linePoints_point = gp1.GetAllPoint();//这是要绘画出来的点

然后用下列语句添加在 DrawController 中,进行绘制:

                for (int i = 0; i < m_linePoints_point.Count; i++){//Debug.Log(m_linePoints[i][0]);Draw.Line(m_linePoints_point[i][0], m_linePoints_point[i][1], UnityEngine.Color.gray);}

相关文章:

  • selenium已知一个元素定位同级别的另一个元素
  • Haiku库和Jax库介绍
  • 【Java GUI 窗体开发实践】基于抽象模板设计模式下实现Windows SSH连接Linux服务器
  • scrapy的建模及管道的使用
  • docker: Error response from daemon: failed to create shim task: OCI runtime
  • 【嵌入式Linux程序开发综合实验】-1(附流程图) | ARM开发板 | 测试“Hello World” | Makefile文件 | 实现加法相加
  • Java的第二十一章:网络通信
  • 解决在Linux中进行redis的主从复制时出现的从机可以获取到主机的信息,主机获取不到从机的信息~
  • Linux C++ 贪吃蛇游戏 -- 方向键控制蛇移动
  • 三、Zookeeper数据模型
  • 苍穹外卖项目笔记(7)— 微信登录、商品浏览
  • python中序列类型运算符
  • 大数据Doris(三十一):Doris简单查询
  • LeetCode103. Binary Tree Zigzag Level Order Traversal
  • 微服务--03--OpenFeign 实现远程调用 (负载均衡组件SpringCloudLoadBalancer)
  • 【译】JS基础算法脚本:字符串结尾
  • [笔记] php常见简单功能及函数
  • 【EOS】Cleos基础
  • 【前端学习】-粗谈选择器
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • js如何打印object对象
  • Linux后台研发超实用命令总结
  • Python打包系统简单入门
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • Twitter赢在开放,三年创造奇迹
  • vue 配置sass、scss全局变量
  • 阿里云Kubernetes容器服务上体验Knative
  • 从0到1:PostCSS 插件开发最佳实践
  • 从零搭建Koa2 Server
  • 从伪并行的 Python 多线程说起
  • 你不可错过的前端面试题(一)
  • 如何胜任知名企业的商业数据分析师?
  • 实战|智能家居行业移动应用性能分析
  • 使用 5W1H 写出高可读的 Git Commit Message
  • ​学习一下,什么是预包装食品?​
  • (9)STL算法之逆转旋转
  • (Forward) Music Player: From UI Proposal to Code
  • (ros//EnvironmentVariables)ros环境变量
  • (附源码)springboot教学评价 毕业设计 641310
  • (九)One-Wire总线-DS18B20
  • (离散数学)逻辑连接词
  • (转)Linux下编译安装log4cxx
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .md即markdown文件的基本常用编写语法
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .net 设置默认首页
  • .Net 应用中使用dot trace进行性能诊断
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .net中调用windows performance记录性能信息
  • @ 代码随想录算法训练营第8周(C语言)|Day57(动态规划)
  • @31省区市高考时间表来了,祝考试成功
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解
  • [ vulhub漏洞复现篇 ] Grafana任意文件读取漏洞CVE-2021-43798
  • [20160902]rm -rf的惨案.txt