各位对项目感兴趣的看官请移步,这里没有各位需要的代码,本内容仅做阶段记录用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* #AuthorName#
* #UpdateTime#
*/
//要求组件
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
[ExecuteInEditMode]
public class RectMesh : MonoBehaviour
{
//定义平面的宽和长
public float m_Width;
public float m_Length;
//MeshFilter
private MeshFilter meshFilter;
//定义mesh网格的纹理坐标数组
private Vector2[] uvs;
//定义mesh网格的三角形顶点索引数组
private int[] triangles;
//定义mesh网格的顶点数据数组
private Vector3[] vertices;
//唤醒
private void Awake()
{
//创建对象其他相关属性
Init();
}
void Init(){
//需要对象的MeshFilter
meshFilter = GetComponent<MeshFilter>();
//需要对象空Mesh
meshFilter.sharedMesh = new Mesh();
}
//创建Mesh
void InitMesh()
{
uvs = new Vector2[4];
uvs[0].x = 0;
uvs[0].y = 0;
uvs[1].x = 1;
uvs[1].y = 0;
uvs[2].x = 0;
uvs[2].y = 1;
uvs[3].x = 1;
uvs[3].y = 1;
triangles = new int[] { 0, 2, 3, 0, 3, 1 };
vertices = new Vector3[4];
Vector3 localPos = new Vector3(0, 0, 0);
vertices[0].x = localPos.x - m_Width * 0.5f;
vertices[0].y = localPos.y;
vertices[0].z = localPos.z - m_Length * 0.5f;
vertices[1].x = localPos.x + m_Width * 0.5f;
vertices[1].y = localPos.y;
vertices[1].z = localPos.z - m_Length * 0.5f;
vertices[2].x = localPos.x - m_Width * 0.5f;
vertices[2].y = localPos.y;
vertices[2].z = localPos.z + m_Length * 0.5f;
vertices[3].x = localPos.x + m_Width * 0.5f;
vertices[3].y = localPos.y;
vertices[3].z = localPos.z + m_Length * 0.5f;
meshFilter.sharedMesh.vertices = vertices;
meshFilter.sharedMesh.triangles = triangles;
meshFilter.sharedMesh.uv = uvs;
}
//当面板内容改动时,就会自动调用这个方法
private void OnValidate()
{
InitMesh();
}
}
网友评论