Unity学习笔记——使用线渲染器LineRenderer绘制网格

带有建筑元素的游戏往往都允许玩家在一定限制里自由地摆放建筑,建筑的摆放受到地形、其他建筑的阻挡限制或者以格子为单位,必须整齐摆放。

我想实现一个网格化的地图,建筑摆放限制必须在网格点初放下,在此使用了LineRenderer的方法画线,但有前辈说这样画线占用太多资源,又没有说实际中如何做。

于是我考虑仍使用线渲染器,但在后期把显示全地图的网格改为只在建筑附近显示,目前的思路是得到每个点的坐标,再以格子为单位的得到四个点坐标储存进数组,这样就得到了格子数组,画格子需要5个点,第5个点是起始点,不需要在数组里储存,画线时positionCount为5再加个起始点就行了。

目前进度:画线、储存格子

public class CutUpMap : MonoBehaviour {
    public float sizeX;//地图尺寸x
    public float sizeZ;//地图尺寸z
    public GameObject terrain;//获取地图
    public int rowNum;//线行数
    public int columnNum;//线列数
    public GameObject[,] lineArray;//线对象数组
    public LineRenderer lineRenderer;//线渲染器
    private Vector3[,] pointPos;//线上点坐标数组
    private Vector3[,] rectPos;//线画为格子,以格子为单位储存格子上四点坐标
    private int retPosIndex=0;//格子索引
    void Start() {
        MapGrid();//计算尺寸和线行数、线列数
        this.lineArray = new GameObject[rowNum - 1, columnNum - 1];//初始化线对象数组
        Debug.Log("lineArray得到的row为:"+ (rowNum - 1 )+ ", column为:"+(columnNum-1));
        pointPos = new Vector3[rowNum,columnNum];//初始化线上点坐标数组
        for(int i = 0; i < rowNum; i++)
        {
            for(int j = 0; j < columnNum; j++)
            {
                pointPos[i, j] = new Vector3(j * 10, 0.2f, i * 10);//给每个点赋值坐标
            }
        }
        rectPos = new Vector3[(rowNum-1)*(columnNum-1),4];//初始化格子坐标数组
        for(int i = 0; i < rowNum-1; i++)
        {
            for(int j = 0; j < columnNum-1; j++)
            {
                //给格子上点坐标赋值
                rectPos[retPosIndex,0] = pointPos[i, j];
                rectPos[retPosIndex,1] = pointPos[i+1, j];
                rectPos[retPosIndex,2] = pointPos[i + 1, j + 1];
                rectPos[retPosIndex,3] = pointPos[i, j + 1];
                retPosIndex++;//索引加1
                CreatLine(i,j,rectPos);//画线
            }
        }
    }
    void MapGrid()
    {
        //获取地图尺寸,并赋值线行数、线列数
        sizeX = terrain.GetComponent<TerrainCollider>().bounds.size.x;
        sizeZ = terrain.GetComponent<TerrainCollider>().bounds.size.z;
        Debug.Log("sizeX=" + sizeX + ",sizeZ=" + sizeZ);
        rowNum = (int)sizeX / 10 + 1;
        columnNum = (int)sizeZ / 10 + 1;
    }
    void CreatLine(int row, int column, Vector3[,] pos)
    {
        if (this.lineArray[row, column] != null)//线对象数组不为空则清空
            GameObject.Destroy(this.lineArray[row, column]);
        this.lineArray[row, column] = new GameObject();//初始化线数组
        LineRenderer lineRendererUpdate = this.lineArray[row, column].AddComponent<LineRenderer>();//给每个线对象添加线渲染器
        lineRendererUpdate.positionCount = 5;//每个格子由5个点组成
        lineRendererUpdate.material = new Material(Shader.Find("Particles/Additive"));//设置着色器
        lineRendererUpdate.material.color=Color.gray;//设置线颜色
        //设置开始、结束的线宽
        lineRendererUpdate.startWidth = 0.2f;
        lineRendererUpdate.endWidth = 0.2f;
        //设置组成格子的五个点的坐标,因为索引在start里已经加1,在此需要减去1
        lineRendererUpdate.SetPosition(0, pos[retPosIndex-1,0]);
        lineRendererUpdate.SetPosition(1, pos[retPosIndex-1,1]);
        lineRendererUpdate.SetPosition(2, pos[retPosIndex-1,2]);
        lineRendererUpdate.SetPosition(3, pos[retPosIndex-1,3]);
        lineRendererUpdate.SetPosition(4, pos[retPosIndex-1,0]);
    }
}

猜你喜欢

转载自blog.csdn.net/u012187817/article/details/80459735
今日推荐