ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Unity网格系统(1)网格生成

2021-12-25 18:02:42  阅读:180  来源: 互联网

标签:pos int Vector3 网格 生成 Unity GetWorldPos public


通过学习YouTube的教程,学了一个简单的网格系统,所以在这做了个总结。
效果如图
自动生成网格
点击网格可以修改里面的数字
点击效果
其实在游戏开发中,可以使用这个去划分地图区域,例如植物大战僵尸,就可以用这个去划分地方区域,通过点击地图网格,生成植物,不过在这我们修改数字。
代码如下

using UnityEngine;

public class Grid
{
    public int width; //长度
    public int helight; //高度
    public int cellSize; //格子尺寸
    public Vector3 startPos;  //起点坐标
    public int[,] arrayCell;  //网格数组,存储网格内容
    public TextMesh[,] arrayTextMesh;      //存储网格textmesh,具体是用来表现

    public Grid(int width, int helight, int cellSize, Vector3 startPos)
    {
        this.width = width;
        this.helight = helight;
        this.cellSize = cellSize;
        this.startPos = startPos;
        arrayCell = new int[width, helight];
        arrayTextMesh = new TextMesh[width, helight];

        for (int i = 0; i < arrayCell.GetLength(0); i++)
        {
            for (int j = 0; j < arrayCell.GetLength(1); j++)
            {
                arrayTextMesh[i ,j] = SetText(arrayCell[i, j].ToString(), GetWorldPos(i, j) + new Vector3(cellSize, cellSize)*0.5f, Color.white);
                Debug.DrawLine(GetWorldPos(i, j), GetWorldPos(i, j + 1), Color.black, 100f);
                Debug.DrawLine(GetWorldPos(i, j), GetWorldPos(i+1, j), Color.black, 100f);
            }
        }
        Debug.DrawLine(GetWorldPos(0, helight), GetWorldPos(width, helight), Color.black, 100f);
        Debug.DrawLine(GetWorldPos(width, 0), GetWorldPos(width, helight), Color.black, 100f);

    }
    //偏移坐标
    public Vector3 GetWorldPos(int x, int y)
    {
        return new Vector3(x, y) * cellSize + startPos;
    }
    //设置网格中的值
    public void SetValue(int x, int y, int value)
    {
        if (x >= 0 & y >= 0 & x < width & y < helight)
        {
            arrayCell[x, y] = value;
            arrayTextMesh[x, y].text = value.ToString();

        }
    }
    //将点击的坐标传入
    public void SetValue(Vector3 pos, int value)
    {
        int x, y;
        GetXY(pos, out x, out y);
        SetValue(x, y, value);
    }

    //将点击的坐标转化为网格中位置
    public void GetXY(Vector3 pos, out int x, out int y)
    {
        x = Mathf.FloorToInt(pos.x / cellSize);
        y = Mathf.FloorToInt(pos.y / cellSize);
    }

    //点击的表现,这里是修改网格内容
    public TextMesh SetText(string text, Vector3 pos, Color color)
    {
        GameObject objText = new GameObject("WorldText", typeof(TextMesh));
        TextMesh textMesh = objText.GetComponent<TextMesh>();
        textMesh.text = text;
        textMesh.color = color;
        textMesh.alignment = TextAlignment.Center;
        textMesh.anchor = TextAnchor.MiddleLeft;
        textMesh.characterSize = 0.28f;
        objText.transform.position = pos;
        return textMesh;
    }
}

调用的脚本:

using UnityEngine;

public class GridTest : MonoBehaviour
{
    public Grid grid;
    void Start()
    {
        grid = new Grid(10, 6, 2, Vector3.zero);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            grid.SetValue(GetPos(), 1);
        }
    }

    public Vector3 GetPos()
    {
        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;
        return pos;
    }
}

随便一提
运行中,这个要点击,否则是看不到网格的
在这里插入图片描述

标签:pos,int,Vector3,网格,生成,Unity,GetWorldPos,public
来源: https://blog.csdn.net/qq_39121279/article/details/122146298

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有