Для начала, скрипт вызывается из верхнего меню, создаёт ГО, помещает себя на него и генерирует для начала просто плейн. Как-то так:
Синтаксис:
Используется csharp
[MenuItem("My Editor Scripts/Carve Land")]
static void CreateBase()
{
GameObject landPiece = new GameObject();
landPiece.AddComponent<CarveLand>();
landPiece.name = "Land Piece";
Selection.activeGameObject = landPiece;
Undo.RegisterCreatedObjectUndo(landPiece, "Created Land Piece");
landPiece.GetComponent<CarveLand>().Reset();
}
static void CreateBase()
{
GameObject landPiece = new GameObject();
landPiece.AddComponent<CarveLand>();
landPiece.name = "Land Piece";
Selection.activeGameObject = landPiece;
Undo.RegisterCreatedObjectUndo(landPiece, "Created Land Piece");
landPiece.GetComponent<CarveLand>().Reset();
}
И вот с некоторых пор это действие начало выдавать гору ошибок. http://radikal.ru/big/29d48c798a0044dea74c94933fbc660c
При чем в инспекторе есть кнопка ресет, которая делает то же самое landPiece.GetComponent<CarveLand>().Reset();
И она то работает как надо.
Вот какой ресет
Синтаксис:
Используется csharp
public void Reset()
{
MyUtilityScript.ClearLog();
storedHits.Clear();
grid = vol.Multiply(res);
var verts = new List<Vector3>();
var uv = new List<Vector2>();
var step = new Vector2(1f / grid.x, 1f / grid.z);
step *= UVres;
colors = new Color[(grid.x + 1)*(grid.z + 1)];
for (int z = 0; z <= grid.z; z++)
{
for (int x = 0; x <= grid.x; x++)
{
verts.Add(new Vector3(x, 0, z) / res);
uv.Add(new Vector2(x * step.x, z * step.y));
colors[x + z * (1 + grid.x)] = Color.red;
}
}
uvs = uv.ToArray();
vertices = verts.ToArray();
updateCollider = true;
AddTris();
void AddTris()
{
List<int> tris = new List<int>();
for (int A = 0; tris.Count < grid.x * grid.z * 6; A++)
{
if (vertices[A].x == vol.x) continue;
int B = A + grid.x + 1;
int C = A + 1;
int D = C + grid.x + 1;
tris.AddRange(new int[]{ A, B, C, D, C, B});
}
triangles = tris.ToArray();
ApplyMesh();
}
void ApplyMesh()
{
Undo.RecordObjects(new Object[] { this, GetComponent<MeshFilter>() }, "Carve Land");
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.colors = colors;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
GetComponent<MeshFilter>().sharedMesh = mesh;
if (updateCollider)
{
updateCollider = false;
GetComponent<MeshCollider>().sharedMesh = mesh;
}
}
}
{
MyUtilityScript.ClearLog();
storedHits.Clear();
grid = vol.Multiply(res);
var verts = new List<Vector3>();
var uv = new List<Vector2>();
var step = new Vector2(1f / grid.x, 1f / grid.z);
step *= UVres;
colors = new Color[(grid.x + 1)*(grid.z + 1)];
for (int z = 0; z <= grid.z; z++)
{
for (int x = 0; x <= grid.x; x++)
{
verts.Add(new Vector3(x, 0, z) / res);
uv.Add(new Vector2(x * step.x, z * step.y));
colors[x + z * (1 + grid.x)] = Color.red;
}
}
uvs = uv.ToArray();
vertices = verts.ToArray();
updateCollider = true;
AddTris();
void AddTris()
{
List<int> tris = new List<int>();
for (int A = 0; tris.Count < grid.x * grid.z * 6; A++)
{
if (vertices[A].x == vol.x) continue;
int B = A + grid.x + 1;
int C = A + 1;
int D = C + grid.x + 1;
tris.AddRange(new int[]{ A, B, C, D, C, B});
}
triangles = tris.ToArray();
ApplyMesh();
}
void ApplyMesh()
{
Undo.RecordObjects(new Object[] { this, GetComponent<MeshFilter>() }, "Carve Land");
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.colors = colors;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
GetComponent<MeshFilter>().sharedMesh = mesh;
if (updateCollider)
{
updateCollider = false;
GetComponent<MeshCollider>().sharedMesh = mesh;
}
}
}
Ошибка указывает на mesh.vertices = vertices;