Skip to content

Instantly share code, notes, and snippets.

@TAK-EMI
Last active August 29, 2015 14:02
Show Gist options
  • Save TAK-EMI/708c25a628c674b2785f to your computer and use it in GitHub Desktop.
Save TAK-EMI/708c25a628c674b2785f to your computer and use it in GitHub Desktop.
とりあえず、直角三角柱を作るエディタ拡張。これから形状を増やしていく予定。
using UnityEngine;
using UnityEditor;
using System.Collections;
namespace TAKL
{
public class CreateOtherPrimitive : EditorWindow
{
[MenuItem("GameObject/Create Other/Other Primitives/Half Cube")]
public static GameObject CreateHalfCube()
{
GameObject hCube = new GameObject("HalfCube");
MeshFilter mf = hCube.AddComponent<MeshFilter>();
Vector3[] vert = new Vector3[8 * 3];
vert[0] = new Vector3(-0.5f, 0.5f, -0.5f);
vert[1] = new Vector3(-0.5f, 0.5f, 0.5f);
vert[2] = new Vector3(0.5f, 0.5f, -0.5f);
vert[3] = new Vector3(-0.5f, 0.5f, 0.5f);
vert[4] = new Vector3(-0.5f, 0.5f, -0.5f);
vert[5] = new Vector3(-0.5f, -0.5f, -0.5f);
vert[6] = new Vector3(-0.5f, 0.5f, 0.5f);
vert[7] = new Vector3(-0.5f, -0.5f, -0.5f);
vert[8] = new Vector3(-0.5f, -0.5f, 0.5f);
vert[9] = new Vector3(-0.5f, 0.5f, -0.5f);
vert[10] = new Vector3(0.5f, 0.5f, -0.5f);
vert[11] = new Vector3(-0.5f, -0.5f, -0.5f);
vert[12] = new Vector3(0.5f, 0.5f, -0.5f);
vert[13] = new Vector3(0.5f, -0.5f, -0.5f);
vert[14] = new Vector3(-0.5f, -0.5f, -0.5f);
vert[15] = new Vector3(0.5f, 0.5f, -0.5f);
vert[16] = new Vector3(-0.5f, 0.5f, 0.5f);
vert[17] = new Vector3(0.5f, -0.5f, -0.5f);
vert[18] = new Vector3(-0.5f, 0.5f, 0.5f);
vert[19] = new Vector3(-0.5f, -0.5f, 0.5f);
vert[20] = new Vector3(0.5f, -0.5f, -0.5f);
vert[21] = new Vector3(-0.5f, -0.5f, -0.5f);
vert[22] = new Vector3(0.5f, -0.5f, -0.5f);
vert[23] = new Vector3(-0.5f, -0.5f, 0.5f);
Vector2[] uv = new Vector2[8 * 3];
uv[0] = new Vector2(0.0f, 0.0f);
uv[1] = new Vector2(0.0f, 1.0f);
uv[2] = new Vector2(1.0f, 0.0f);
uv[3] = new Vector2(0.0f, 1.0f);
uv[4] = new Vector2(1.0f, 1.0f);
uv[5] = new Vector2(1.0f, 0.0f);
uv[6] = new Vector2(0.0f, 1.0f);
uv[7] = new Vector2(1.0f, 0.0f);
uv[8] = new Vector2(0.0f, 0.0f);
uv[9] = new Vector2(0.0f, 1.0f);
uv[10] = new Vector2(1.0f, 1.0f);
uv[11] = new Vector2(0.0f, 0.0f);
uv[12] = new Vector2(1.0f, 1.0f);
uv[13] = new Vector2(1.0f, 0.0f);
uv[14] = new Vector2(0.0f, 0.0f);
uv[15] = new Vector2(0.0f, 1.0f);
uv[16] = new Vector2(1.0f, 1.0f);
uv[17] = new Vector2(0.0f, 0.0f);
uv[18] = new Vector2(1.0f, 1.0f);
uv[19] = new Vector2(1.0f, 0.0f);
uv[20] = new Vector2(0.0f, 0.0f);
uv[21] = new Vector2(1.0f, 0.0f);
uv[22] = new Vector2(0.0f, 0.0f);
uv[23] = new Vector2(1.0f, 1.0f);
int[] ifs = new int[8 * 3];
ifs[0] = 0;
ifs[1] = 1;
ifs[2] = 2;
ifs[3] = 3;
ifs[4] = 4;
ifs[5] = 5;
ifs[6] = 6;
ifs[7] = 7;
ifs[8] = 8;
ifs[9] = 9;
ifs[10] = 10;
ifs[11] = 11;
ifs[12] = 12;
ifs[13] = 13;
ifs[14] = 14;
ifs[15] = 15;
ifs[16] = 16;
ifs[17] = 17;
ifs[18] = 18;
ifs[19] = 19;
ifs[20] = 20;
ifs[21] = 21;
ifs[22] = 22;
ifs[23] = 23;
Mesh mesh = new Mesh();
mesh.name = "HalfCube";
mesh.vertices = vert;
mesh.uv = uv;
mesh.triangles = ifs;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mf.sharedMesh = mesh;
MeshCollider mc = hCube.AddComponent<MeshCollider>();
mc.convex = true;
MeshRenderer mr = hCube.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Diffuse"));
mat.name = "Default-Diffuse";
mat.color = Color.white;
mr.renderer.sharedMaterial = mat;
return hCube;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment