|
| 1 | +// editor tools to create mesh plane with adjustable resolution |
| 2 | +// original : http://wiki.unity3d.com/index.php?title=CreatePlane#C.23_-_CreatePlane.cs |
| 3 | + |
| 4 | +using UnityEngine; |
| 5 | +using UnityEditor; |
| 6 | +using System.Collections; |
| 7 | + |
| 8 | +namespace UnityLibrary |
| 9 | +{ |
| 10 | + public class CreatePlane : ScriptableWizard |
| 11 | + { |
| 12 | + |
| 13 | + public enum Orientation |
| 14 | + { |
| 15 | + Horizontal, |
| 16 | + Vertical |
| 17 | + } |
| 18 | + |
| 19 | + public enum AnchorPoint |
| 20 | + { |
| 21 | + TopLeft, |
| 22 | + TopHalf, |
| 23 | + TopRight, |
| 24 | + RightHalf, |
| 25 | + BottomRight, |
| 26 | + BottomHalf, |
| 27 | + BottomLeft, |
| 28 | + LeftHalf, |
| 29 | + Center |
| 30 | + } |
| 31 | + |
| 32 | + public int widthSegments = 1; |
| 33 | + public int lengthSegments = 1; |
| 34 | + public float width = 1.0f; |
| 35 | + public float length = 1.0f; |
| 36 | + public Orientation orientation = Orientation.Horizontal; |
| 37 | + public AnchorPoint anchor = AnchorPoint.Center; |
| 38 | + public bool addCollider = false; |
| 39 | + public bool createAtOrigin = true; |
| 40 | + public bool flipYZ = false; |
| 41 | + public bool twoSided = false; |
| 42 | + public string optionalName; |
| 43 | + |
| 44 | + static Camera cam; |
| 45 | + static Camera lastUsedCam; |
| 46 | + |
| 47 | + |
| 48 | + [MenuItem("GameObject/Create Other/Custom Plane...")] |
| 49 | + static void CreateWizard() |
| 50 | + { |
| 51 | + cam = Camera.current; |
| 52 | + // Hack because camera.current doesn't return editor camera if scene view doesn't have focus |
| 53 | + if (!cam) |
| 54 | + cam = lastUsedCam; |
| 55 | + else |
| 56 | + lastUsedCam = cam; |
| 57 | + ScriptableWizard.DisplayWizard("Create Plane",typeof(CreatePlane)); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + void OnWizardUpdate() |
| 62 | + { |
| 63 | + widthSegments = Mathf.Clamp(widthSegments, 1, 254); |
| 64 | + lengthSegments = Mathf.Clamp(lengthSegments, 1, 254); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + void OnWizardCreate() |
| 69 | + { |
| 70 | + GameObject plane = new GameObject(); |
| 71 | + |
| 72 | + if (!string.IsNullOrEmpty(optionalName)) |
| 73 | + plane.name = optionalName; |
| 74 | + else |
| 75 | + plane.name = "Plane"; |
| 76 | + |
| 77 | + if (!createAtOrigin && cam) |
| 78 | + plane.transform.position = cam.transform.position + cam.transform.forward*5.0f; |
| 79 | + else |
| 80 | + plane.transform.position = Vector3.zero; |
| 81 | + |
| 82 | + Vector2 anchorOffset; |
| 83 | + string anchorId; |
| 84 | + switch (anchor) |
| 85 | + { |
| 86 | + case AnchorPoint.TopLeft: |
| 87 | + anchorOffset = new Vector2(-width/2.0f,length/2.0f); |
| 88 | + anchorId = "TL"; |
| 89 | + break; |
| 90 | + case AnchorPoint.TopHalf: |
| 91 | + anchorOffset = new Vector2(0.0f,length/2.0f); |
| 92 | + anchorId = "TH"; |
| 93 | + break; |
| 94 | + case AnchorPoint.TopRight: |
| 95 | + anchorOffset = new Vector2(width/2.0f,length/2.0f); |
| 96 | + anchorId = "TR"; |
| 97 | + break; |
| 98 | + case AnchorPoint.RightHalf: |
| 99 | + anchorOffset = new Vector2(width/2.0f,0.0f); |
| 100 | + anchorId = "RH"; |
| 101 | + break; |
| 102 | + case AnchorPoint.BottomRight: |
| 103 | + anchorOffset = new Vector2(width/2.0f,-length/2.0f); |
| 104 | + anchorId = "BR"; |
| 105 | + break; |
| 106 | + case AnchorPoint.BottomHalf: |
| 107 | + anchorOffset = new Vector2(0.0f,-length/2.0f); |
| 108 | + anchorId = "BH"; |
| 109 | + break; |
| 110 | + case AnchorPoint.BottomLeft: |
| 111 | + anchorOffset = new Vector2(-width/2.0f,-length/2.0f); |
| 112 | + anchorId = "BL"; |
| 113 | + break; |
| 114 | + case AnchorPoint.LeftHalf: |
| 115 | + anchorOffset = new Vector2(-width/2.0f,0.0f); |
| 116 | + anchorId = "LH"; |
| 117 | + break; |
| 118 | + case AnchorPoint.Center: |
| 119 | + default: |
| 120 | + anchorOffset = Vector2.zero; |
| 121 | + anchorId = "C"; |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter)); |
| 126 | + plane.AddComponent(typeof(MeshRenderer)); |
| 127 | + |
| 128 | + string planeAssetName = plane.name + widthSegments + "x" + lengthSegments + "W" + width + "L" + length + (orientation == Orientation.Horizontal? "H" : "V") + anchorId + ".asset"; |
| 129 | + Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath("Assets/Editor/" + planeAssetName,typeof(Mesh)); |
| 130 | + |
| 131 | + if (m == null) |
| 132 | + { |
| 133 | + m = new Mesh(); |
| 134 | + m.name = plane.name; |
| 135 | + |
| 136 | + int hCount2 = widthSegments+1; |
| 137 | + int vCount2 = lengthSegments+1; |
| 138 | + int numTriangles = widthSegments * lengthSegments * 6; |
| 139 | + if (twoSided) { |
| 140 | + numTriangles *= 2; |
| 141 | + } |
| 142 | + int numVertices = hCount2 * vCount2; |
| 143 | + |
| 144 | + Vector3[] vertices = new Vector3[numVertices]; |
| 145 | + Vector2[] uvs = new Vector2[numVertices]; |
| 146 | + int[] triangles = new int[numTriangles]; |
| 147 | + |
| 148 | + int index = 0; |
| 149 | + float uvFactorX = 1.0f/widthSegments; |
| 150 | + float uvFactorY = 1.0f/lengthSegments; |
| 151 | + float scaleX = width/widthSegments; |
| 152 | + float scaleY = length/lengthSegments; |
| 153 | + for (float y = 0.0f; y < vCount2; y++) |
| 154 | + { |
| 155 | + for (float x = 0.0f; x < hCount2; x++) |
| 156 | + { |
| 157 | + if (orientation == Orientation.Horizontal) |
| 158 | + { |
| 159 | + if (flipYZ) |
| 160 | + { |
| 161 | + vertices[index] = new Vector3(x*scaleX - width/2f - anchorOffset.x, y*scaleY - length/2f - anchorOffset.y, 0.0f); |
| 162 | + }else{ |
| 163 | + vertices[index] = new Vector3(x*scaleX - width/2f - anchorOffset.x, 0.0f, y*scaleY - length/2f - anchorOffset.y); |
| 164 | + } |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + if (flipYZ) |
| 169 | + { |
| 170 | + vertices[index] = new Vector3(x*scaleX - width/2f - anchorOffset.x, 0.0f, y*scaleY - length/2f - anchorOffset.y); |
| 171 | + }else{ |
| 172 | + vertices[index] = new Vector3(x*scaleX - width/2f - anchorOffset.x, y*scaleY - length/2f - anchorOffset.y, 0.0f); |
| 173 | + } |
| 174 | + } |
| 175 | + uvs[index++] = new Vector2(x*uvFactorX, y*uvFactorY); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + index = 0; |
| 180 | + for (int y = 0; y < lengthSegments; y++) |
| 181 | + { |
| 182 | + for (int x = 0; x < widthSegments; x++) |
| 183 | + { |
| 184 | + triangles[index] = (y * hCount2) + x; |
| 185 | + triangles[index+1] = ((y+1) * hCount2) + x; |
| 186 | + triangles[index+2] = (y * hCount2) + x + 1; |
| 187 | + |
| 188 | + triangles[index+3] = ((y+1) * hCount2) + x; |
| 189 | + triangles[index+4] = ((y+1) * hCount2) + x + 1; |
| 190 | + triangles[index+5] = (y * hCount2) + x + 1; |
| 191 | + index += 6; |
| 192 | + } |
| 193 | + if (twoSided) { |
| 194 | + // Same tri vertices with order reversed, so normals point in the opposite direction |
| 195 | + for (int x = 0; x < widthSegments; x++) |
| 196 | + { |
| 197 | + triangles[index] = (y * hCount2) + x; |
| 198 | + triangles[index+1] = (y * hCount2) + x + 1; |
| 199 | + triangles[index+2] = ((y+1) * hCount2) + x; |
| 200 | + |
| 201 | + triangles[index+3] = ((y+1) * hCount2) + x; |
| 202 | + triangles[index+4] = (y * hCount2) + x + 1; |
| 203 | + triangles[index+5] = ((y+1) * hCount2) + x + 1; |
| 204 | + index += 6; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + m.vertices = vertices; |
| 210 | + m.uv = uvs; |
| 211 | + m.triangles = triangles; |
| 212 | + m.RecalculateNormals(); |
| 213 | + |
| 214 | + AssetDatabase.CreateAsset(m, "Assets/Editor/" + planeAssetName); |
| 215 | + AssetDatabase.SaveAssets(); |
| 216 | + } |
| 217 | + |
| 218 | + meshFilter.sharedMesh = m; |
| 219 | + m.RecalculateBounds(); |
| 220 | + |
| 221 | + if (addCollider) |
| 222 | + plane.AddComponent(typeof(BoxCollider)); |
| 223 | + |
| 224 | + Selection.activeObject = plane; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
0 commit comments