CreatePlane

From Unify Community Wiki
Revision as of 03:54, 26 July 2009 by Mike (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author: Michael Garforth

Description

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

Usage

Place this script in YourProject/Assets/Editor and a menu item will automatically appear in the "GameObject/Create Other" menu after it is compiled.

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;

   [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 = "New Plane";

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

Mesh 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;

meshFilter.mesh = m;

       Selection.activeObject = plane;
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox