CreatePlane

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (set the centre of the panel as the centre of the object (instead of bottom left))
(fixed a large mistake with it not saving the asset)
Line 11: Line 11:
 
==Usage==
 
==Usage==
 
Place this script as "CreatePlane.cs" in ''YourProject/Assets/Editor'' and a menu item will automatically appear in the "GameObject/Create Other" menu after it is compiled.
 
Place this script as "CreatePlane.cs" in ''YourProject/Assets/Editor'' and a menu item will automatically appear in the "GameObject/Create Other" menu after it is compiled.
 +
It will create a mesh asset named either "PlaneHxV" or "OptionalNameHxV" (where H and W are the horizontal and vertical quads) in the "YourProject/Assets/Editor" folder
  
 
==C# - CreatePlane.cs==
 
==C# - CreatePlane.cs==
Line 37: Line 38:
 
return;
 
return;
 
}
 
}
 +
 
GameObject plane = new GameObject();
 
GameObject plane = new GameObject();
 
Vector3 pos = plane.transform.position;
 
Vector3 pos = plane.transform.position;
Line 43: Line 45:
 
plane.transform.position = pos;
 
plane.transform.position = pos;
 
 
if (optionalName != null && optionalName != "")
+
if (!string.IsNullOrEmpty(optionalName))
 
plane.name = optionalName;
 
plane.name = optionalName;
 
else
 
else
plane.name = "New Plane";
+
plane.name = "Plane";
 +
 +
string meshPrefabPath = "Assets/Editor/" + plane.name + horizontalCount + "x" + verticalCount;
 +
string meshPrefabName = meshPrefabPath + ".asset";
  
 
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
 
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
 
plane.AddComponent(typeof(MeshRenderer));
 
plane.AddComponent(typeof(MeshRenderer));
  
Mesh m = new Mesh();
+
Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabName, typeof(Mesh));
m.name = plane.name;
+
 +
if (m == null)
 +
{
 +
m = new Mesh();
 +
m.name = plane.name;
 
 
int numTriangles = horizontalCount * verticalCount * 2;
+
int numTriangles = horizontalCount * verticalCount * 2;
int numVertices = numTriangles * 3;
+
int numVertices = numTriangles * 3;
 
 
Vector3[] vertices = new Vector3[numVertices];
+
Vector3[] vertices = new Vector3[numVertices];
Vector3[] normals = new Vector3[numVertices];
+
Vector3[] normals = new Vector3[numVertices];
Vector2[] uvs = new Vector2[numVertices];
+
Vector2[] uvs = new Vector2[numVertices];
int[] triangles = new int[numVertices];
+
int[] triangles = new int[numVertices];
 
 
for (int i = 0; i < numVertices; ++i)
+
for (int i = 0; i < numVertices; ++i)
{
+
{
triangles[i] = i;
+
triangles[i] = i;
normals[i] = Vector3.one;
+
normals[i] = Vector3.one;
}
+
}
 
 
for (int i = 0; i < horizontalCount; ++i)
+
for (int i = 0; i < horizontalCount; ++i)
{
+
for (int j = 0; j < verticalCount; ++j)
+
 
{
 
{
int current = (j + i*verticalCount)*6;
+
for (int j = 0; j < verticalCount; ++j)
 +
{
 +
int current = (j + i*verticalCount)*6;
 
 
Vector3 bottomLeft = new Vector3((float)i/horizontalCount - 0.5f, 0, (float)j/verticalCount - 0.5f);
+
Vector3 bottomLeft = new Vector3((float)i/horizontalCount, 0, (float)j/verticalCount);
  Vector3 bottomRight = new Vector3((float)(i+1)/horizontalCount - 0.5f, 0, (float)j/verticalCount - 0.5f);
+
Vector3 bottomRight = new Vector3((float)(i+1)/horizontalCount, 0, (float)j/verticalCount);
  Vector3 topLeft = new Vector3((float)i/horizontalCount - 0.5f, 0, (float)(j+1)/verticalCount - 0.5f);
+
Vector3 topLeft = new Vector3((float)i/horizontalCount, 0, (float)(j+1)/verticalCount);
  Vector3 topRight = new Vector3((float)(i+1)/horizontalCount - 0.5f, 0, (float)(j+1)/verticalCount - 0.5f);
+
Vector3 topRight = new Vector3((float)(i+1)/horizontalCount, 0, (float)(j+1)/verticalCount);
 
 
Vector2 bottomLeftV2 = new Vector2((float)i/horizontalCount, (float)j/verticalCount);
+
Vector2 bottomLeftV2 = new Vector2((float)i/horizontalCount, (float)j/verticalCount);
Vector2 bottomRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)j/verticalCount);
+
Vector2 bottomRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)j/verticalCount);
Vector2 topLeftV2 = new Vector2((float)i/horizontalCount, (float)(j+1)/verticalCount);
+
Vector2 topLeftV2 = new Vector2((float)i/horizontalCount, (float)(j+1)/verticalCount);
Vector2 topRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)(j+1)/verticalCount);
+
Vector2 topRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)(j+1)/verticalCount);
 
 
vertices[current] = bottomLeft; //triangle 1
+
Debug.Log(bottomLeftV2 + "  " + topLeftV2);
vertices[current+1] = topLeft;
+
vertices[current] = bottomLeft; //triangle 1
vertices[current+2] = topRight;
+
vertices[current+1] = topLeft;
vertices[current+3] = topRight; //triangle 2
+
vertices[current+2] = topRight;
vertices[current+4] = bottomRight;
+
vertices[current+3] = topRight; //triangle 2
vertices[current+5] = bottomLeft;
+
vertices[current+4] = bottomRight;
 +
vertices[current+5] = bottomLeft;
 
 
uvs[current] = bottomLeftV2; //triangle 1
+
uvs[current] = bottomLeftV2; //triangle 1
uvs[current+1] = topLeftV2;
+
uvs[current+1] = topLeftV2;
uvs[current+2] = topRightV2;
+
uvs[current+2] = topRightV2;
uvs[current+3] = topRightV2; //triangle 2
+
uvs[current+3] = topRightV2; //triangle 2
uvs[current+4] = bottomRightV2;
+
uvs[current+4] = bottomRightV2;
uvs[current+5] = bottomLeftV2;
+
uvs[current+5] = bottomLeftV2;
 +
}
 
}
 
}
}
 
 
 
m.vertices = vertices;
+
m.vertices = vertices;
m.normals = normals;
+
m.normals = normals;
m.uv = uvs;
+
m.uv = uvs;
m.triangles = triangles;
+
m.triangles = triangles;
 +
 +
AssetDatabase.CreateAsset(m, meshPrefabName);
 +
AssetDatabase.SaveAssets();
 +
}
 
 
meshFilter.mesh = m;
+
meshFilter.sharedMesh = m;
 
 
 
if (addCollider)
 
if (addCollider)

Revision as of 06:40, 4 August 2009

Author: Michael Garforth

Description

This editor script creates a plane at the origin with parameters radius and thickness.

Usage

Place this script as "CreatePlane.cs" in YourProject/Assets/Editor and a menu item will automatically appear in the "GameObject/Create Other" menu after it is compiled. It will create a mesh asset named either "PlaneHxV" or "OptionalNameHxV" (where H and W are the horizontal and vertical quads) in the "YourProject/Assets/Editor" folder

C# - CreatePlane.cs

<csharp>using UnityEngine; using UnityEditor; using System.Collections;

public class CreatePlane : ScriptableWizard {

   public int horizontalCount = 1;

public int verticalCount = 1; public string optionalName; public bool addCollider = false;

   [MenuItem ("GameObject/Create Other/Custom Plane")]
   static void CreateWizard()
   {
       ScriptableWizard.DisplayWizard("Create Plane", typeof(CreatePlane));
   }
   
   void OnWizardCreate()
   {

if (horizontalCount < 1 || verticalCount < 1) { Debug.LogError("Count must be greater than 0 for horizontal and vertical"); return; }

GameObject plane = new GameObject(); Vector3 pos = plane.transform.position; pos.x -= 0.5f; pos.z -= 0.5f; plane.transform.position = pos;

if (!string.IsNullOrEmpty(optionalName)) plane.name = optionalName; else plane.name = "Plane";

string meshPrefabPath = "Assets/Editor/" + plane.name + horizontalCount + "x" + verticalCount; string meshPrefabName = meshPrefabPath + ".asset";

MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter)); plane.AddComponent(typeof(MeshRenderer));

Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabName, typeof(Mesh));

if (m == null) { m = new Mesh(); m.name = plane.name;

int numTriangles = horizontalCount * verticalCount * 2; int numVertices = numTriangles * 3;

Vector3[] vertices = new Vector3[numVertices]; Vector3[] normals = new Vector3[numVertices]; Vector2[] uvs = new Vector2[numVertices]; int[] triangles = new int[numVertices];

for (int i = 0; i < numVertices; ++i) { triangles[i] = i; normals[i] = Vector3.one; }

for (int i = 0; i < horizontalCount; ++i) { for (int j = 0; j < verticalCount; ++j) { int current = (j + i*verticalCount)*6;

Vector3 bottomLeft = new Vector3((float)i/horizontalCount, 0, (float)j/verticalCount); Vector3 bottomRight = new Vector3((float)(i+1)/horizontalCount, 0, (float)j/verticalCount); Vector3 topLeft = new Vector3((float)i/horizontalCount, 0, (float)(j+1)/verticalCount); Vector3 topRight = new Vector3((float)(i+1)/horizontalCount, 0, (float)(j+1)/verticalCount);

Vector2 bottomLeftV2 = new Vector2((float)i/horizontalCount, (float)j/verticalCount); Vector2 bottomRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)j/verticalCount); Vector2 topLeftV2 = new Vector2((float)i/horizontalCount, (float)(j+1)/verticalCount); Vector2 topRightV2 = new Vector2((float)(i+1)/horizontalCount, (float)(j+1)/verticalCount);

Debug.Log(bottomLeftV2 + " " + topLeftV2); vertices[current] = bottomLeft; //triangle 1 vertices[current+1] = topLeft; vertices[current+2] = topRight; vertices[current+3] = topRight; //triangle 2 vertices[current+4] = bottomRight; vertices[current+5] = bottomLeft;

uvs[current] = bottomLeftV2; //triangle 1 uvs[current+1] = topLeftV2; uvs[current+2] = topRightV2; uvs[current+3] = topRightV2; //triangle 2 uvs[current+4] = bottomRightV2; uvs[current+5] = bottomLeftV2; } }

m.vertices = vertices; m.normals = normals; m.uv = uvs; m.triangles = triangles;

AssetDatabase.CreateAsset(m, meshPrefabName); AssetDatabase.SaveAssets(); }

meshFilter.sharedMesh = m;

if (addCollider) plane.AddComponent(typeof(BoxCollider));

       Selection.activeObject = plane;
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox