• mastodon
  • Twitter
  • twitchBlackIcon
  • LinkedIn
  • TikTok
29381019-8C11-4D01-9327-9D8BD5731FF7_1_105_c.jpeg

Regular Polygons Fresh From The Editor

Originally Posted October 8, 2013 - idle-chatter.com

*Note - This totally worked in 2013, but Unity has changed a lot



I’ve been trying to get back in to some more independent development lately, and the first thing I re-built was a quick and dirty editor script that lets you easily pump out arbitrarily scaled regular polygons. Great for making some quick geometric shapes or just replacing Unity’s default plane with a simple quad.


using UnityEngine;
using UnityEditor;
using System.Collections;

public class CreatePolygon : ScriptableWizard
{

    public enum Orientation
    {
        Horizontal,
        Vertical
    }

    public float scale = 1.0f;
	public int sides = 4;
	public float angle = 90.0f;
	public bool addCollider = false;
	public string optionalName = "Polygon";

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

    void OnWizardUpdate()
    {
		sides = Mathf.Clamp(sides, 3, 100);
    }

    void OnWizardCreate()
    {
        GameObject polygon = new GameObject();

        if (!string.IsNullOrEmpty(optionalName))
            polygon.name = optionalName;
        else
            polygon.name = "Polygon";

		polygon.transform.position = Vector3.zero;

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

        string polygonAssetName = polygon.name + "_Sides"+sides.ToString()+"_Scale"+scale.ToString();
		Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath("Assets/Editor/" + polygonAssetName,typeof(Mesh));

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

            int numTriangles = (sides-2);
            int numVertices = sides;

            Vector3[] vertices = new Vector3[numVertices];
            Vector2[] uvs = new Vector2[numVertices];
            int[] triangles = new int[numTriangles*3];

            for(int i=0; i<sides;i++) {
				float ang = i * (Mathf.PI*2.0f/sides) + Mathf.Deg2Rad*angle;
				vertices[i] = new Vector3(Mathf.Sin(ang), Mathf.Cos(ang), 0);
				uvs[i] = new Vector2((vertices[i].x+1)*0.5f, (vertices[i].y+1)*0.5f);
				vertices[i] *= scale;
			}

			for(int i=0; i<numTriangles;i++) {
				triangles[i*3]=0;
				triangles[i*3+1]=i+1;
				triangles[i*3+2]=i+2;
			}

            m.vertices = vertices;
            m.uv = uvs;
            m.triangles = triangles;
            m.RecalculateNormals();

            AssetDatabase.CreateAsset(m, "Assets/Editor/" + polygonAssetName);
            AssetDatabase.SaveAssets();
        }

        meshFilter.sharedMesh = m;
        m.RecalculateBounds();

        if (addCollider)
            polygon.AddComponent(typeof(MeshCollider));

        Selection.activeObject = polygon;
    }
}

This work by Ted DiNola is licensed under a Creative Commons Attribution 3.0 United States License.