标签:http fun protected virt collect raw tco har string
本文在上一篇平面圆形基础上生成平面环形。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class CreateMeshBase : MonoBehaviour
{
MeshFilter meshFilter;
protected Mesh mesh;
protected virtual Vector3[] Vertices { get; }
protected virtual int[] Triangles { get; }
protected virtual Vector3[] Normals { get; }
protected virtual Vector2[] Uvs { get; }
protected virtual string MeshName { get; }
protected virtual void Start()
{
GetMeshFilter();
}
protected virtual void Reset()
{
GetMeshFilter();
}
protected virtual void OnValidate()
{
GetMeshFilter();
}
void GetMeshFilter()
{
if (meshFilter == null)
{
meshFilter = GetComponent<MeshFilter>();
mesh = new Mesh();
}
mesh.triangles = null;
mesh.uv = null;
mesh.vertices = null;
mesh.name = MeshName;
mesh.vertices = Vertices;
mesh.triangles = Triangles;
mesh.uv = Uvs;
meshFilter.mesh = mesh;
}
private void OnDrawGizmos()
{
if (Vertices == null) return;
Gizmos.color = Color.red;
Gizmos.DrawSphere(Vector3.zero, 0.5f);
Gizmos.color = Color.blue;
for (int i = 0; i < Vertices.Length; i++)
{
Gizmos.DrawSphere(Vertices[i], 0.3f);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateRing : CreateMeshBase
{
[Range(3,100)]
public int count = 4;
public float outerRadius = 8;
public float innerRadius = 4;
public bool showHalf = false;
protected override string MeshName
{
get
{
return "Ring Mesh";
}
}
protected override Vector3[] Vertices
{
get
{
Vector3[] vertices = new Vector3[count * 2];
float delta = 2 * Mathf.PI / count;
for (int i = 0; i < count; i++)
{
int a = i * 2;
float sinValue = Mathf.Sin(i * delta);
float cosValue = Mathf.Cos(i * delta);
vertices[a] = new Vector3(innerRadius * cosValue, innerRadius * sinValue, 0);
vertices[a + 1] = new Vector3(outerRadius * cosValue, outerRadius * sinValue, 0);
//Debug.LogFormat("Uv value of {0} and {1} is {2} and {3}", a, a + 1, vertices[a], vertices[a + 1]);
}
return vertices;
}
}
protected override int[] Triangles
{
get
{
int[] triangles = new int[count * 2 * 3];
for (int i = 0; i < count; i++)
{
if (showHalf)
{
if (i % 2 == 0) continue;
}
int baseIndex = i * 6;
int vertexIndex = 2 * i;
triangles[baseIndex] = vertexIndex;
triangles[baseIndex + 2] = vertexIndex + 1;
if (i >= count - 1)
{
triangles[baseIndex + 1] = 0;
triangles[baseIndex + 3] = 0;
triangles[baseIndex + 5] = vertexIndex + 1;
triangles[baseIndex + 4] = 1