using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshTest : MonoBehaviour {
public MeshFilter myMeshFilter;//聲明一個網(wǎng)格適配器
public Mesh myMesh; //聲明一個網(wǎng)格
public Vector3[] myVertices = new Vector3[4];//聲明一組頂點(diǎn)
public int[] myTriangles = new int[6];//聲明一組頂點(diǎn)序列
// Use this for initialization
// 當(dāng)播放時第一幀開始執(zhí)行
void Start () {
myMesh = new Mesh ();//在內(nèi)存開辟一個Mesh類型的空間
myMesh.vertices = myVertices;//為myMesh賦予頂點(diǎn)數(shù)據(jù)
myMesh.triangles = myTriangles;//為myMesh賦予頂點(diǎn)序列數(shù)據(jù)
myMeshFilter = this.gameObject.AddComponent ();//添加并獲取網(wǎng)格適配器組件
myMeshFilter.mesh = myMesh;//為網(wǎng)格適合器組件的mesh屬性賦值
this.gameObject.AddComponent ();//添加網(wǎng)格渲染組件
}
// Update is called once per frame
void Update () {
}
}
using System.Collections.Generic;
using UnityEngine;
public class MeshTest : MonoBehaviour {
public MeshFilter myMeshFilter;//聲明一個網(wǎng)格適配器
public Mesh myMesh; //聲明一個網(wǎng)格
public Material myMaterial;
public Vector3[] myVertices = new Vector3[4];//聲明一組頂點(diǎn)
public int[] myTriangles = new int[6];//聲明一組頂點(diǎn)序列
/*
// Use this for initialization
// 當(dāng)播放時第一幀開始執(zhí)行
void Start () {
myMesh = new Mesh ();//在內(nèi)存開辟一個Mesh類型的空間
myMesh.vertices = myVertices;//為myMesh賦予頂點(diǎn)數(shù)據(jù)
myMesh.triangles = myTriangles;//為myMesh賦予頂點(diǎn)序列數(shù)據(jù)
myMeshFilter = this.gameObject.AddComponent ();//添加并獲取網(wǎng)格適配器組件
myMeshFilter.mesh = myMesh;//為網(wǎng)格適合器組件的mesh屬性賦值
this.gameObject.AddComponent ();//添加網(wǎng)格渲染組件
}
*/
void Start(){
myMesh = new Mesh ();//在內(nèi)存開辟一個Mesh類型的空間
myMesh.vertices = myVertices;//為myMesh賦予頂點(diǎn)數(shù)據(jù)
myMesh.triangles = myTriangles;//為myMesh賦予頂點(diǎn)序列數(shù)據(jù)
MeshFilter myMeshFilter = this.gameObject.GetComponent();//獲取腳本所在物體的網(wǎng)格適配器
myMeshFilter.mesh = myMesh;//為網(wǎng)格適配器賦值
MeshRenderer myMeshRenderer = this.gameObject.GetComponent ();//獲取腳本所在物體的網(wǎng)格渲染器
myMeshRenderer.material = myMaterial;//為網(wǎng)格渲染賦值
myMesh.RecalculateNormals();
// this.transform.position = Vector3.up * 5;
// this.gameObject.GetComponent ().position = Vector3.up * 5;
}
// Update is called once per frame
void Update () {
}
}