Search Unity

I'm converting the "famous" CreatePlane script to javascript! Only 3 errors left!

Discussion in 'Scripting' started by keenanwoodall, Jul 23, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    After a good bit of "ctrl + h", "ctrl + c and v", I've only got three errors left!

    1: Assets/Editor/PlaneCreator/CustomPlaneScript.js(108,17): BCE0043: Unexpected token: default.
    2: Assets/Editor/PlaneCreator/CustomPlaneScript.js(115,9): BCE0044: expecting }, found 'plane'.
    2. Assets/Editor/PlaneCreator/CustomPlaneScript.js(204,5): BCE0044: expecting EOF, found '}'.

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