Unity实现瓦片地图tile map

Unity自定义mesh绘制  基于上篇的mesh修改,实现tile map

第一步,修改mesh 顶点和三角片信息,生成方格

230643_ZJ2j_1391394.png

顶点信息就是一系列正方形的组合,三角片,在这里实现的方法是一个矩形由2个三角形组成,注意,三角形的顺序依赖于顶点的顺序,三角形的三个点按照观察方向顺时针,三角形数量=顶点数量*6,6=2*3,2是三角形个数,3是一个三角形 由3个顶点组成,输入的三角数组的顺序是,对应顶点在输入的顶点数据中的index,贴代码:

using UnityEngine;
using System.Collections;


public class ss5 : MonoBehaviour
{
    void Start()
    {
        dooo();

    }

    void dooo()
    {
        {

            MeshFilter filter = this.GetComponent<MeshFilter>();
            MeshRenderer render = this.GetComponent<MeshRenderer>();
            var mesh = filter.mesh;

            var ts = this.GetComponentsInChildren<Transform>();

            Vector3[] vers = new Vector3[ts.Length];

            for (int i = 0; i < ts.Length; i++)
            {
                vers[i] = ts[i].position;
            }

            const int MAX_X = 30;
            const int MAX_Y = 30;
            Vector3[] vertics = new Vector3[MAX_Y * MAX_X];

            for (int x = 0; x < MAX_X; x++)
            {
                for (int y = 0; y < MAX_Y; y++)
                {
                    vertics[x + MAX_X * y] = new Vector3(x, 0f, y);
                }
            }
            foreach (var p in vertics)
            {
                Ray ray = new Ray(p, new Vector3(-100f, -100f, -100f));
                Debug.DrawLine(ray.GetPoint(-0.1f), ray.GetPoint(0.1f), Color.red);
            }
            int[] triss = new int[(MAX_Y - 1) * (MAX_X - 1) * 6];

            for (int x = 0; x < MAX_X - 1; x++)
            {
                for (int y = 0; y < MAX_Y - 1; y++)
                {
                    var p = vertics[x + MAX_X * y];
                    int baseIdx = 0;
                    if (y >= 1)
                    {
                        baseIdx = (y * (MAX_X - 1) * 6) + x * 6;
                    }
                    else
                    {
                        baseIdx = x * 6;
                    }
                    Debug.LogError(x + "   " + y + "    " + triss.Length + "  baseIdx=" + baseIdx);
                    triss[baseIdx + 0] = x + y * MAX_X;
                    triss[baseIdx + 1] = x + (y + 1) * MAX_X;
                    triss[baseIdx + 2] = (x + 1) + y * MAX_X;
                    triss[baseIdx + 3] = x + (y + 1) * MAX_X;
                    triss[baseIdx + 4] = (x + 1) + (y + 1) * MAX_X;
                    triss[baseIdx + 5] = (x + 1) + y * MAX_X; ;

                    /*   triss[x + y * MAX_X + 0] = 0;
                    triss[x + y * MAX_X + 1] = 2;
                    triss[x + y * MAX_X + 2] = 1;
                    triss[x + y * MAX_X + 3] = 2;
                    triss[x + y * MAX_X + 4] = 3;
                    triss[x + y * MAX_X + 5] = 1;
    */

                    /* triss[(x * 2 + y * 9 * 2) + 0] = 5;
                     triss[(x * 2 + y * 9 * 2) + 1] = 5;
                     triss[(x * 2 + y * 9 * 2) + 2] = 5;
                     triss[(x * 2 + y * 9 * 2) + 3] = 5;
                     triss[(x * 2 + y * 9 * 2) + 4] = 5;
                     triss[(x * 2 + y * 9 * 2) + 5] = 5;
                     */
                }
            }


            mesh.vertices = vertics;
            mesh.triangles = triss;// this.GetTris(vers);
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            this.GetComponent<MeshCollider>().sharedMesh = mesh;
            filter.mesh = mesh;
            return;
        }
    }
    void Update()
    {

    }



    void OnDrawGizmos()
    {
        return;

        {

            MeshFilter filter = this.GetComponent<MeshFilter>();
            MeshRenderer render = this.GetComponent<MeshRenderer>();
            var mesh = filter.mesh;

            var ts = this.GetComponentsInChildren<Transform>();

            Vector3[] vers = new Vector3[ts.Length];

            for (int i = 0; i < ts.Length; i++)
            {
                vers[i] = ts[i].position;
            }

            const int MAX_X = 3;
            const int MAX_Y = 3;
            Vector3[] vertics = new Vector3[MAX_Y * MAX_X];

            for (int x = 0; x < MAX_X; x++)
            {
                for (int y = 0; y < MAX_Y; y++)
                {
                    vertics[x + MAX_X * y] = new Vector3(x, 0f, y);
                }
            }
            foreach (var p in vertics)
            {
                Ray ray = new Ray(p, new Vector3(-100f, -100f, -100f));
                Debug.DrawLine(ray.GetPoint(-0.1f), ray.GetPoint(0.1f), Color.red);
            }
            int[] triss = new int[(MAX_Y - 1) * (MAX_X - 1) * 6];

            for (int x = 0; x < MAX_X - 1; x++)
            {
                for (int y = 0; y < MAX_Y - 1; y++)
                {
                    var p = vertics[x + MAX_X * y];
                    int baseIdx = 0;
                    if (y >= 1)
                    {
                        baseIdx = (y * (MAX_X - 1) * 6) + x * 6;
                    }
                    else
                    {
                        baseIdx = x * 6;
                    }
                    Debug.LogError(x + "   " + y + "    " + triss.Length + "  baseIdx=" + baseIdx);
                    triss[baseIdx + 0] = x + y * MAX_X;
                    triss[baseIdx + 1] = x + (y + 1) * MAX_X;
                    triss[baseIdx + 2] = (x + 1) + y * MAX_X;
                    triss[baseIdx + 3] = x + (y + 1) * MAX_X;
                    triss[baseIdx + 4] = (x + 1) + (y + 1) * MAX_X;
                    triss[baseIdx + 5] = (x + 1) + y * MAX_X; ;

                    /*   triss[x + y * MAX_X + 0] = 0;
                    triss[x + y * MAX_X + 1] = 2;
                    triss[x + y * MAX_X + 2] = 1;
                    triss[x + y * MAX_X + 3] = 2;
                    triss[x + y * MAX_X + 4] = 3;
                    triss[x + y * MAX_X + 5] = 1;
    */
                    /* triss[(x * 2 + y * 9 * 2) + 0] = 5;
                     triss[(x * 2 + y * 9 * 2) + 1] = 5;
                     triss[(x * 2 + y * 9 * 2) + 2] = 5;
                     triss[(x * 2 + y * 9 * 2) + 3] = 5;
                     triss[(x * 2 + y * 9 * 2) + 4] = 5;
                     triss[(x * 2 + y * 9 * 2) + 5] = 5;
                     */
                }
            }


            mesh.vertices = vertics;
            mesh.triangles = triss;// this.GetTris(vers);
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            this.GetComponent<MeshCollider>().sharedMesh = mesh;
            filter.mesh = mesh;
            return;
        } 
    }


}

 

第二步:修改UV信息,U是x轴,V是y轴,矩形4个顶点 对应UV4个坐标点,在这里是逆时针,UV也是逆时针。因此只需要一张大贴图 附上对应的UV信息 就可以实现tile map了

 

 

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/1546557

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值