unity编辑器拓展七——脚本创建模型

来源:互联网 发布:java程序调用rest api 编辑:程序博客网 时间:2024/06/05 20:51

描述:

    其实这个教程到处都是,我真正想做的是把模型分成N个等分,实现动态加载。但是在网上搜索了很久,也没有搜索

到合适的方法,要么就是不写清楚,舍不得把代码全部放出来。要么就是自带地形的切割,最想要的是,t4m 刷完后直

接将模型分成N分,所以只能苦逼的从最mesh最基础的开始学吧。

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System;public class CreatMesh : Editor {    [MenuItem("tool/creatMesh")]    static void CreaMesh()    {         creatVertAadTriangels();    }    //创建顶点 三角面    static void creatVertAadTriangels()    {        /*mesh属性         * 长宽         * 段数         */        Vector2 size = new Vector3(10, 10); //长宽(因为是平面,所以就xy了 不要高度了)        Vector2 segment =new Vector2 (10,10);//xy段数                /*mesh组成部分         * 顶点         * uv         * 三角形索引         * 法线         */        Vector3[] verters;//顶点数组        Vector2[] uv;//uv数组        int[] triangles; //三角形索引        //1.顶点数组赋值        int countVert = Mathf.FloorToInt ((segment.x + 1) * (segment.y + 1)); //根据长宽计算出总的顶点数 转换成int类型        float w = size.x / segment.x; //计算每一段的宽度        float h = size.y / segment.y; //计算每一段的长度        int index01 = 0;        verters = new Vector3[countVert];        uv = new Vector2[countVert];        for (int i = 0; i < segment.y + 1; i++)        {            for (int j = 0; j < segment.x + 1; j++)            {                verters[index01] = new Vector3(j * w, 0, i * h); //创建每个点 (按照xy的段数距离)并且给顶点数组赋值                                             float u = j / segment.x;   //每个点在u上面的位置                float v = i / segment.y;   //每个点在v上面的位置                uv[index01] = new Vector2(u, v); //将每个点的uv加到uv数组里面                               index01++;            }        }        Vector2[] uv02 = new Vector2[] {new Vector2(0,0), new Vector2(0.5f, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(0.5f, 1), new Vector2(1, 1), };        //3.三角形        int countVertTri = Mathf.FloorToInt ( segment.x * segment.y * 6); //三角形顶点总数:每1*1段有两个三角形组成  两个三角形有6个点(包含公用的两个点),所有总点数 是段数*6        int index02 = 0;        triangles = new int[countVertTri];        for (int i = 0; i < segment.y; i++)        {            for (int j = 0; j < segment.x; j++)            {                //顺时针                int role = Mathf.FloorToInt (segment.x) + 1; //x方向顶点数                int self = j + (i * role);       // i=0 j=0的时候 第0行第0个点  i=0 j=1的时候 第0行第1个点                int next = j + ((i + 1) * role); // i=0 j=0的时候 第1行第0个点  i=0 j=1的时候 第1行第1个点                //第一个正方形                //顺时针                //第一个三角形                triangles[index02] = self;         //i=0 j=0的时候  第0行0个点                triangles[index02 + 1] = next;     //i=0 j=0的时候  第1行0个点                triangles[index02 + 2] = next + 1; //i=0 j=0的时候  第1行1个点                //第二个三角形                triangles[index02 + 3] = self;     //i=0 j=0的时候  第0行0个点                triangles[index02 + 4] = next + 1; //i=0 j=0的时候  第1行1个点                triangles[index02 + 5] = self + 1; //i=0 j=0的时候  第0行1个点         //一次可以实现6个点的排序,两个三角形 拼成一个正方形                //随着j index的增加 绘制第一行的所有正方形 随着i的增加 一行一行的向上绘制                index02 += 6;            }        }           //渲染    //新建mesh,并对它的属性赋值    Mesh me = new Mesh();        me.vertices = verters; //mesh顶点赋值        me.triangles = triangles; //mesh三角形索引赋值        me.uv = uv;        me.RecalculateNormals(); //自动添加法线信息        //新建材质球        Material ma = new Material(Shader.Find("Diffuse"));        //新建模型,将mesh跟material给他赋值        GameObject  m_gameObject = new GameObject("m_newOBJ");//新建一个gameobject        m_gameObject.AddComponent<MeshFilter>().sharedMesh = me; //增加meshFilter组件        m_gameObject.AddComponent<MeshRenderer>().sharedMaterial = ma;//增加MeshRenderer组件    }}


原创粉丝点击