C# 如何创建在Unity中与网格生成的长方体R2D?
因此,我使用的代码如下(统一中的C)C# 如何创建在Unity中与网格生成的长方体R2D?,c#,unity3d,game-physics,C#,Unity3d,Game Physics,因此,我使用的代码如下(统一中的C) 使用System.Collections.Generic; 使用UnityEngine; [要求组件(类型(网状过滤器))] 公共类多工发生器:单行为 { public List new顶点=new List(); public List newTriangles=新列表(); public List newUV=新列表(); 私有网格; private float tUnitY=0.33333f;//这些只是我为纹理指定ID的对象 专用浮点数=0.1666
使用System.Collections.Generic;
使用UnityEngine;
[要求组件(类型(网状过滤器))]
公共类多工发生器:单行为
{
public List new顶点=new List();
public List newTriangles=新列表();
public List newUV=新列表();
私有网格;
private float tUnitY=0.33333f;//这些只是我为纹理指定ID的对象
专用浮点数=0.166667f;
私有向量2 tDirtBL=新向量2(0,0);
私有向量2 tDirtB=新向量2(1,0);
私有向量2 tDirtBR=新向量2(2,0);
私有向量2 tDirtL=新向量2(0,1);
私有向量2 tDirt=新向量2(1,1);
私有向量2 tDirtR=新向量2(2,1);
专用矢量2 tDirtUL=新矢量2(0,2);
专用矢量2 tDirtU=新矢量2(1,2);
专用矢量2 tDirtUR=新矢量2(2,2);
私有向量2 tStone=新向量2(4,1);
私有整数平方计数;
公共字节[,]块;
public List colVertices=new List();
public List colTriangles=新列表();
私有整数计数;
私人EdgeCollider2D col;
void Start()
{
mesh=GetComponent().mesh;
col=GetComponent();
float x=transform.position.x;
float y=transform.position.y;
float z=transform.position.z;
庆大霉素();
BuildMesh();
网格更新();
}
void GenSquare(int x,int y,Vector2纹理)//这将创建我可以应用纹理的块
{
添加(新向量3(x,y,0));
添加(新向量3(x+1,y,0));
添加(新向量3(x+1,y-1,0));
添加(新向量3(x,y-1,0));
新三角形相加(平方数*4);
加上((平方数*4)+1);
新三角形。加上((平方数*4)+3);
加上((平方数*4)+1);
加上((平方数*4)+2);
新三角形。加上((平方数*4)+3);
添加(新向量2(tUnitX*texture.x,tUnitY*texture.y+tUnitY));
newUV.Add(新向量2(tUnitX*texture.x+tUnitX,tUnitY*texture.y+tUnitY));
添加(新向量2(tUnitX*texture.x+tUnitX,tUnitY*texture.y));
newUV.Add(新向量2(tUnitX*texture.x,tUnitY*texture.y));
平方计数++;
}
void MeshUpdate()//这仅更新网格
{
mesh.Clear();
mesh.vertices=newVertices.ToArray();
mesh.triangles=newTriangles.ToArray();
mesh.uv=newUV.ToArray();
mesh.normals();
平方计数=0;
newVertices.Clear();
newTriangles.Clear();
newUV.Clear();
网格newMesh=新网格();
newMesh.vertices=colVertices.ToArray();
newMesh.triangles=colTriangles.ToArray();
col.sharedMaterial=新网格;
colVertices.Clear();
colTriangles.Clear();
colCount=0;
}
void GenTerrain()//根据我提供的参数生成地形,我可以添加这些参数
{//PerlinNoise创建可变地形而不是平坦地形
块=新字节[512,128];
for(int-px=0;px10)
{
块[px,py]=2;
}
//接下来的三行清除泥土和岩石,在某些地方形成洞穴
如果(噪声(px,py*2,16,14,1)>10)
{//洞穴
块[px,py]=0;
}
}
否则,如果(py<污垢)
{
块[px,py]=2;
}
}
}
}
void BuildMesh()//网格创建
{
for(intpx=0;pxusing System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class PolygonGenerator : MonoBehaviour
{
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;
private float tUnitY = 0.33333f; //These are just objects I've given IDs for textures
private float tUnitX = 0.166667f;
private Vector2 tDirtBL = new Vector2(0, 0);
private Vector2 tDirtB = new Vector2(1, 0);
private Vector2 tDirtBR = new Vector2(2, 0);
private Vector2 tDirtL = new Vector2(0, 1);
private Vector2 tDirt = new Vector2(1, 1);
private Vector2 tDirtR = new Vector2(2, 1);
private Vector2 tDirtUL = new Vector2(0, 2);
private Vector2 tDirtU = new Vector2(1, 2);
private Vector2 tDirtUR = new Vector2(2, 2);
private Vector2 tStone = new Vector2(4, 1);
private int squareCount;
public byte[,] blocks;
public List<Vector3> colVertices = new List<Vector3>();
public List<int> colTriangles = new List<int>();
private int colCount;
private EdgeCollider2D col;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
col = GetComponent<EdgeCollider2D>();
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
GenTerrain();
BuildMesh();
MeshUpdate();
}
void GenSquare(int x, int y, Vector2 texture) //This creates the blocks I can apply textures to
{
newVertices.Add(new Vector3(x, y, 0));
newVertices.Add(new Vector3(x + 1, y, 0));
newVertices.Add(new Vector3(x + 1, y - 1, 0));
newVertices.Add(new Vector3(x, y - 1, 0));
newTriangles.Add(squareCount * 4);
newTriangles.Add((squareCount * 4) + 1);
newTriangles.Add((squareCount * 4) + 3);
newTriangles.Add((squareCount * 4) + 1);
newTriangles.Add((squareCount * 4) + 2);
newTriangles.Add((squareCount * 4) + 3);
newUV.Add(new Vector2(tUnitX * texture.x, tUnitY * texture.y + tUnitY));
newUV.Add(new Vector2(tUnitX * texture.x + tUnitX, tUnitY * texture.y + tUnitY));
newUV.Add(new Vector2(tUnitX * texture.x + tUnitX, tUnitY * texture.y));
newUV.Add(new Vector2(tUnitX * texture.x, tUnitY * texture.y));
squareCount++;
}
void MeshUpdate() //This merely updates the mesh
{
mesh.Clear();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.RecalculateNormals();
squareCount = 0;
newVertices.Clear();
newTriangles.Clear();
newUV.Clear();
Mesh newMesh = new Mesh();
newMesh.vertices = colVertices.ToArray();
newMesh.triangles = colTriangles.ToArray();
col.sharedMaterial = newMesh;
colVertices.Clear();
colTriangles.Clear();
colCount = 0;
}
void GenTerrain() //Generates terrain based on parameters I supply and I can add in
{ //PerlinNoise to create variable terrain instead of flat
blocks = new byte[512, 128];
for (int px = 0; px < blocks.GetLength(0); px++) //Also tells which blocks are which
{
int stone = Noise(px, 0, 80, 15, 1);
stone += Noise(px, 0, 50, 30, 1);
stone += Noise(px, 0, 10, 10, 1);
stone += 75;
print(stone);
int dirt = Noise(px, 0, 25f, 35, 1);
dirt += Noise(px, 100, 50, 30, 1);
dirt += 75;
for (int py = 0; py < blocks.GetLength(1); py++)
{
if (py < stone)
{
blocks[px, py] = 1;
//The next three lines make dirt spots in random places
if (Noise(px, py, 12, 16, 1) > 10)
{
blocks[px, py] = 2;
}
//The next three lines remove dirt and rock to make caves in certain places
if (Noise(px, py * 2, 16, 14, 1) > 10)
{ //Caves
blocks[px, py] = 0;
}
}
else if (py < dirt)
{
blocks[px, py] = 2;
}
}
}
}
void BuildMesh() //Mesh Creation
{
for (int px = 0; px < blocks.GetLength(0); px++)
{
for (int py = 0; py < blocks.GetLength(1); py++)
{
if (blocks[px, py] != 0)
{
GenCollider(px, py);
}
if (blocks[px, py] == 1)
{
GenSquare(px, py, tStone);
}
else if (blocks[px, py] == 2)
{
GenSquare(px, py, tDirtU);
}
}
}
}
void GenCollider(int x, int y)
{
//Top
if (Block(x, y + 1) == 0)
{
colVertices.Add(new Vector3(x, y, 1));
colVertices.Add(new Vector3(x + 1, y, 1));
colVertices.Add(new Vector3(x + 1, y, 0));
colVertices.Add(new Vector3(x, y, 0));
ColliderTriangles();
colCount++;
}
//Bottom
if (Block(x, y - 1) == 0)
{
colVertices.Add(new Vector3(x, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y - 1, 1));
colVertices.Add(new Vector3(x, y - 1, 1));
ColliderTriangles();
colCount++;
}
//Left
if (Block(x - 1, y) == 0)
{
colVertices.Add(new Vector3(x, y - 1, 1));
colVertices.Add(new Vector3(x, y, 1));
colVertices.Add(new Vector3(x, y, 0));
colVertices.Add(new Vector3(x, y - 1, 0));
ColliderTriangles();
colCount++;
}
//Right
if (Block(x + 1, y) == 0)
{
colVertices.Add(new Vector3(x + 1, y, 1));
colVertices.Add(new Vector3(x + 1, y - 1, 1));
colVertices.Add(new Vector3(x + 1, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y, 0));
ColliderTriangles();
colCount++;
}
}
void ColliderTriangles()
{
colTriangles.Add(colCount * 4);
colTriangles.Add((colCount * 4) + 1);
colTriangles.Add((colCount * 4) + 3);
colTriangles.Add((colCount * 4) + 1);
colTriangles.Add((colCount * 4) + 2);
colTriangles.Add((colCount * 4) + 3);
}
byte Block(int x, int y)
{
if (x == -1 || x == blocks.GetLength(0) || y == -1 || y == blocks.GetLength(1))
{
return (byte)1;
}
return blocks[x, y];
}
int Noise(int x, int y, float scale, float mag, float exp)
{
return (int)(Mathf.Pow((Mathf.PerlinNoise(x / scale, y / scale) * mag), (exp)));
}
}