Advertisement
kasru

CreatePlane

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