unity, 用脚本创建mesh
创建一个空gameObject,添加Mesh Filter和Mesh Renderer两个component,再添加一个脚本createMeshScript:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class createMeshScript : MonoBehaviour {
void Awake() {
gameObject.GetComponent<MeshFilter> ().mesh = CreateMesh (1,1);
}
Mesh CreateMesh(float width, float height)
{
Mesh m = new Mesh();
m.name = "ScriptedMesh";
//note: unity is left-hand system
m.vertices = new Vector3[] {
new Vector3(-width/2, -height/2, 0),
new Vector3(-width/2, height/2, 0),
new Vector3(width/2, height/2, 0),
new Vector3(width/2, -height/2, 0)
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2 (1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
m.RecalculateBounds();
return m;
}
}
此时可以看到Inspector中Mesh Filter->Mesh显示为ScriptedMesh(因为我们在代码中为它命名是scriptedMesh)。
此时创建的mesh没有材质,显示为紫色,新建一个Material拖放到Mesh Renderer->Materials->Element 0上,完成mesh的创建。
运行程序,可以看到脚本创建的mesh如图:

另外需要注意的是:
1,unity中是坐标系统是左手系,如果不注意这一点把顶点顺序(法线)搞反了,可能画出来的东西因背面剔除而看不到。
2,mesh.vertices要用局部坐标。
参考:
http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
http://answers.unity3d.com/questions/139808/creating-a-plane-mesh-directly-from-code.html
----补充:(2015-5-23)
下面尝试创建一个复杂一点儿的面片:
将前面的脚本改成:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class createMeshScript : MonoBehaviour {
void Awake() {
gameObject.GetComponent<MeshFilter> ().mesh = CreateRandomPlane (1,1,3,3,true);//false
}
Mesh CreateCustomPlane(float width, float height,int nRow,int nCol,bool isAddRandom){
Mesh m = new Mesh();
m.name = "customPlane";
//note: unity is left-hand system
Vector3 LU = new Vector3 (-width/2,height/2,0);
float dx = width / nCol;
float dy = height / nRow;
int vertCount = (nRow + 1) * (nCol + 1);
int triangleCount = nRow * nCol * 2;
Vector3[] vertices=new Vector3[vertCount];
Vector2[] uv=new Vector2[vertCount];
int[] IDs=new int[triangleCount*3];
for (int i=0; i<nRow+1; i++) {
for(int j=0;j<nCol+1;j++){
Vector3 v=LU+new Vector3(dx*j,-dy*i,0);
Vector2 texCoord=new Vector2(dx*j/width,1-dy*i/height);
if(isAddRandom){//add random
v.z=(Random.value*2-1)*0.1f;
}
vertices[i*(nCol+1)+j]=v;
uv[i*(nCol+1)+j]=texCoord;
}
}
for (int i=0; i<nRow; i++) {
for(int j=0;j<nCol;j++){
//quad[i][j]
int LUID=i*(nCol+1)+j;
int LDID=LUID+(nCol+1);
int RUID=LUID+1;
int RDID=LDID+1;
//quad[i][j] is triangle[2*(i*4+j)] and triangle[2*(i*4+j)+1]
//triangle[2*(i*4+j)] is {IDs[2*(i*4+j)*3],IDs[2*(i*4+j)*3+1],IDs[2*(i*4+j)*3+2]}
IDs[2*(i*nCol+j)*3]=LUID;
IDs[2*(i*nCol+j)*3+1]=RUID;
IDs[2*(i*nCol+j)*3+2]=LDID;
//triangle[2*(i*4+j)+1] is {IDs[(2*(i*4+j)+1)*3],IDs[(2*(i*4+j)*3+1)*3+1],IDs[(2*(i*4+j)*3+2)*3+2]}
IDs[(2*(i*nCol+j)+1)*3]=LDID;
IDs[(2*(i*nCol+j)+1)*3+1]=RUID;
IDs[(2*(i*nCol+j)+1)*3+2]=RDID;
}
}
m.vertices = vertices;
m.uv = uv;
m.triangles = IDs;
m.RecalculateNormals();
m.RecalculateBounds();
return m;
}
}
运行结果:
左边是isAddRandom取false,右边是isAddRandom取true。

注:脚本中的m.RecalculateBounds()可能是多余的,因为Mesh.RecalculateBounds的官方文档中写道:After modifying vertices you should call this function to ensure the bounding volume is correct. Assigning triangles will automatically Recalculate the bounding volume.
unity, 用脚本创建mesh的更多相关文章
- Unity通过脚本创建Mesh(网格)
##1.创建一个带Mesh的物体 Unity中的网格作为组件不能脱离物体单独存在 新建脚本CreateMesh public class CreateMesh: MonoBehaviour { voi ...
- Unity中动态创建Mesh
什么是Mesh? Mesh是指的模型的网格,3D模型是由多边形拼接而成,而多边形实际上又是由多个三角形拼接而成的.即一个3D模型的表面其实是由多个彼此相连的三角面构成.三维空间中,构成这些三角形的点和 ...
- Unity Mono脚本 加密
加密环境 引擎版本:Unity3D 5.3.4 及更高版本 (使用Mono而并非IL2CPP) 操作系统:CentOS 6.2(Final) 加密环境:Android.IOS(暂定) 加密对象:C#源 ...
- 游戏编程之Unity常用脚本类的继承关系
前言学习Unity开发引擎的初学者会接触大量的脚本类,而这些类之间的关系往往容易被忽略.本文对Unity引擎开发中的一些常用类及其关系进行了简单的归纳总结. 博文首发地址:http://tieba.b ...
- Unity基础-脚本生命周期
理解Unity脚本的生命周期对游戏开发很重要,这篇文章对生命周期做一个记录和总结.Unity的脚本生命周期(消息),也就是在脚本运行时,自动并且按顺序执行的一系列函数.在unity官网中有对生命周期详 ...
- Unity3D学习笔记4——创建Mesh高级接口
目录 1. 概述 2. 详论 3. 其他 4. 参考 1. 概述 在文章Unity3D学习笔记2--绘制一个带纹理的面中使用代码的方式创建了一个Mesh,不过这套接口在Unity中被称为简单接口.与其 ...
- Unity获取脚本的CustomEditor(自定义编辑)数据
在此之前,粗略的介绍下 CustomEditor(自定义编辑). Unity对于我们创建的Mono脚本提供了属性面板的展示和修改.默认情况下,Inspector面板中会显示当前脚本类的公开字段(pub ...
- loadrunner工具使用之脚本创建
loadrunner工具使用之脚本创建 一.创建脚本 1.打开loadrunner,选择第一个控件VuGen(创建/编辑脚本),点击
- BlazeMeter发布chrome扩展插件,支持JMeter脚本创建
BlazeMeter发布chrome扩展插件,支持JMeter脚本创建http://www.automationqa.com/forum.php?mod=viewthread&tid=3898 ...
随机推荐
- 狗日的rem
rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...
- 找不到原始安装光盘的佳能EOS Utility的下载和安装
佳能EOS Utility的下载和安装 佳能很有意思,在官方网站上提供的数码相机驱动程序,只是“升级版”,而不是原始安装版.如果我有安装光盘,还去网上下载驱动干吗?解决方案:1,从佳能官方网站上下 ...
- glob函数的使用
glob库函数用于Linux文件系统中路径名称的模式匹配,即查找文件系统中指定模式的路径.注意,这不是正则表达式匹配,虽然有些相似,但还是有点差别. glob函数原型 #include & ...
- cocos2d-x 环境搭建
刚搬到博客园,第一次在这写博.有点小激动啊~~ 闲话不多说,这次想做一个专题,针对最近比较流行的手游开发引擎cocos2d-x,希望大家不吝赐教~ 本节主要针对环境搭建方面进行 ...
- Spark Client和Cluster两种运行模式的工作流程
1.client mode: In client mode, the driver is launched in the same process as the client that submits ...
- MRIcro tutorial -- mricro 教程
MRIcro tutorial 参考网址:http://www.mccauslandcenter.sc.edu/mricro/mricron/ http://www.cabiatl.com/mri ...
- 第十一章 PhpMyAdmin连接远程mysql服务器---连接openwrt 703N服务器
//千万不要在你原来的那个phpmyadmin文件夹上操作~~~要复制一个新的进行操作,这样我们就可以同时使用本地和远程 一.下载phpmyadmin到本地 我使用的是windows下的集成WAM ...
- FFMPEG类库打开流媒体的方法(传参数)
使用ffmpeg类库进行开发的时候,打开流媒体(或本地文件)的函数是avformat_open_input(). 其中打开网络流的话,前面要加上函数avformat_network_init(). 一 ...
- C#启动外部程序以及等待外部程序关闭的几种方法
1. 启动外部程序,不等待其退出. 2. 启动外部程序,等待其退出. 3. 启动外部程序,无限等待其退出. 4. 启动外部程序,通过事件监视其退出. // using System.Diagnosti ...
- 策略模式Strategy——回家乘什么车?
1.问题与模式 时间:2014年6月 学校:廊坊师范 家:石家庄 人物:学生 又快到期末考试了,回家的节奏也奔上日程.无聊之余就想想这次回家的事儿.对我来说回家主 ...