使用mesh自己创建一个cube

参考链接:https://www.cnblogs.com/JLZT1223/p/6089996.html

直接上代码:

void Update()
    {

        if (Input.GetKeyUp(KeyCode.J))
        {
            GameObject NewObj=CreateCube(1, 1, 1,Vector3.zero);
            NewObj.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Standard"));
        }

       
    }

 /// <summary>
    /// 绘制立方体
    /// </summary>
    /// <param name="length">长</param>
    /// <param name="width">宽</param>
    /// <param name="heigth">高</param>
    /// <returns>GameObject类型物体</returns>
    GameObject CreateCube(float length, float width, float heigth,Vector3 Point)
     {

        GameObject NewObj = new GameObject("新建");
        //NewObj.name = ;
        NewObj.AddComponent<MeshFilter>();
        NewObj.AddComponent<MeshRenderer>();
        MeshFilter meshFilter = NewObj.GetComponent<MeshFilter>();

        //vertices(顶点、必须):
        int vertices_count = 4 * 6;                                 //顶点数(每个面4个点,六个面)
         Vector3[] vertices = new Vector3[vertices_count];
         vertices[0] = new Vector3(Point .x- length/2, Point .y- heigth/2, Point .z- width/2);                     //前面的左下角的点
         vertices[1] = new Vector3(Point .x- length / 2, Point.y+heigth / 2, Point.z - width / 2);                //前面的左上角的点
         vertices[2] = new Vector3(Point.x+length / 2, Point.y - heigth / 2, Point.z - width / 2);                 //前面的右下角的点
        vertices[3] = new Vector3(Point.x+length / 2, Point.y+heigth / 2, Point.z - width / 2);           //前面的右上角的点

        vertices[4] = new Vector3(Point.x+length / 2, Point.y - heigth / 2, Point.z+width / 2);           //后面的右下角的点
         vertices[5] = new Vector3(Point.x+length / 2, Point.y+heigth / 2, Point.z+width / 2);      //后面的右上角的点
         vertices[6] = new Vector3(Point .x- length / 2, Point.y - heigth / 2, Point.z+ width /2);                //后面的左下角的点
         vertices[7] = new Vector3(Point .x- length / 2, Point.y+heigth / 2, Point.z+width / 2);           //后面的左上角的点
 
         vertices[8] = vertices[6];                              //左
         vertices[9] = vertices[7];
         vertices[10] = vertices[0];
         vertices[11] = vertices[1];
 
         vertices[12] = vertices[2];                              //右
         vertices[13] = vertices[3];
         vertices[14] = vertices[4];
         vertices[15] = vertices[5];
 
         vertices[16] = vertices[1];                              //上
         vertices[17] = vertices[7];
         vertices[18] = vertices[3];
         vertices[19] = vertices[5];
 
         vertices[20] = vertices[2];                              //下
         vertices[21] = vertices[4];
         vertices[22] = vertices[0];
         vertices[23] = vertices[6];
 
 
         //triangles(索引三角形、必须):
         int SplitTriangle = 6 * 2;//分割三角形数量
         int triangles_cout = SplitTriangle * 3;                  //索引三角形的索引点个数
         int[] triangles = new int[triangles_cout];            //索引三角形数组
         for(int i=0,vi=0;i<triangles_cout;i+=6,vi+=4)
         {
             triangles[i] = vi;
             triangles[i + 1] = vi+1;
             triangles[i + 2] = vi+2;
 
             triangles[i + 3] = vi+3;
             triangles[i + 4] = vi+2;
             triangles[i + 5] = vi+1;
 
         }
 
         //uv:
         //.........
 
         //负载属性与mesh
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshFilter.mesh= mesh;
        return NewObj;
     }

主要流程就是获取顶点位置和三角索引点然后进行绘制立方体,具体详细的可以看这边文章https://blog.csdn.net/yffgamestart/article/details/90268680

解释的非常到位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值