CreatePlane

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Mike)
Line 1: Line 1:
 +
[[Category: Wizard]]
 +
[[Category: ScriptableWizard]]
 +
[[Category: C Sharp]]
 +
[[Category: Selection]]
 +
[[Category: Primitive]]
 +
[[Category: Plane]]
 +
Author: [[User:Mike|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>

Revision as of 19:17, 19 October 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