CreatePlane.cs 5.5 KB

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