- Home /
How do you make mesh in Unity?
I know how to insert a mesh block or cyliner or whatever the different shapes are. I also know how to resize it. My question is though, how to youcan edit mesh and make it a custom shape?
P.S. Sorry if this seems like a dumb question to any of you. I just started using Unity a few days ago, and I'm still trying to familiarize myself with it.
Answer by yoyo · Dec 03, 2010 at 07:14 PM
You can create Meshes procedurally, which is useful if your custom shapes can be generated from code.
See http://unity3d.com/support/documentation/ScriptReference/Mesh.html
You can even add custom menu handlers to the editor so that your shapes can be added just like the standard ones.
For "artistic" shapes, +1 to the Blender suggestion, I wouldn't try and program anything more complicated than a teapot if I were you! :-)
Answer by Propagant · Nov 07, 2014 at 10:56 PM
Everything is about vertexes, triangles, edges, normals, UVs etc... Example:
using UnityEngine;
using System.Collections;
public class Plane_Mesh_Script : MonoBehaviour {
public Vector3[] Vertex = new Vector3[]
{
new Vector3(0,0,0),new Vector3(1,0,0),new Vector3(0,1,0),new Vector3(1,1,0)
};
public Vector2[] UV_MaterialDisplay = new Vector2[]
{
new Vector2(0,0),new Vector2(1,0),new Vector2(0,1),new Vector2(1,1) // 4 UV with all directions! (Plane has 4 uvMaps)
};
public int[] Triangles = new int[6]; // 2 Triangle combinations (2*3=6 vertices/vertexes)
public Material material;
void Start () {
Triangles [0] = 0;
Triangles [1] = 3;
Triangles [2] = 1;
Triangles [3] = 0;
Triangles [4] = 2;
Triangles [5] = 3;
Mesh mesh = new Mesh ();
transform.GetComponent<MeshFilter> ();
if(!transform.GetComponent<MeshFilter> () || !transform.GetComponent<MeshRenderer> () ) //If you havent got any meshrenderer or filter
{
transform.gameObject.AddComponent<MeshFilter>();
transform.gameObject.AddComponent<MeshRenderer>();
}
transform.GetComponent<MeshFilter> ().mesh = mesh;
mesh.name = "MyOwnObject";
mesh.vertices = Vertex;
mesh.triangles = Triangles;
mesh.uv = UV_MaterialDisplay;
mesh.RecalculateNormals ();
mesh.Optimize ();
transform.gameObject.renderer.material = material; //If you want a material.. you have it :)
}
}
Your answer
Follow this Question
Related Questions
'Pre-create object VS Create when needed' Performance difference 1 Answer
Problem creating a circle mesh 1 Answer
Creating a dynamic mesh with audio = crash after 30secs ? 0 Answers
Adding and removing specific triangles ? 1 Answer
How do I create a specific object in Unity? I need a planet with huge mountains and oceans. 1 Answer