class CreateSimplePlane extends ScriptableWizard {
    var width:float = 10.0;
    var height:float = 10.0;
    var path:String;
    var fileName:String = "SimplePlane";

    @MenuItem("Assets/Create/Simple Plane")
    static function CreateWizard():void {
        
        ScriptableWizard.DisplayWizard("Create Simple Plane", typeof(CreateSimplePlane));
    }
    
    function OnWizardUpdate() {
        path = getSelectedPath();
    }
    
    function OnWizardCreate():void {
        var mesh:Mesh = CreatePlaneMesh(width, height);
        mesh.name = "SimplePlane";
        AssetDatabase.CreateAsset(mesh, path +fileName +".asset");

        var plane:GameObject = new GameObject("plane");
        plane.hideFlags = HideFlags.HideAndDontSave;
        var meshFilter:MeshFilter = plane.AddComponent(MeshFilter);
        meshFilter.sharedMesh = mesh;
        var meshRenderer = plane.AddComponent(MeshRenderer);
        var material:Material = new Material(Shader.Find("Diffuse"));
        meshRenderer.sharedMaterial = material;
        AssetDatabase.CreateAsset(material, path +fileName +".mat");
        
        var prefab:Object = EditorUtility.CreateEmptyPrefab(path +fileName +".prefab");
   EditorUtility.ReplacePrefab(plane, prefab);
   AssetDatabase.Refresh();
   DestroyImmediate(plane);
    }
    
    
    static function getSelectedPath():String {
        var filePath:String = "";
        if (Selection.objects.Length > 0) {
            var selectedObject:Object = Selection.objects[0];
            if (selectedObject.GetType() != typeof(UnityEngine.GameObject)){
                filePath = AssetDatabase.GetAssetPath(selectedObject);
                if (System.IO.Directory.Exists(filePath)) {
                    filePath = filePath +"/";
                } else {
                    filePath = System.IO.Path.GetDirectoryName(filePath) +"/";
                }
            }
        }

        if ((filePath == null) || (filePath == "")) {
            filePath = "Assets/";
        }
        return filePath;
    }
    
    
    
    function CreatePlaneMesh(width_:float, height_:float):Mesh {
        var mesh:Mesh = new Mesh();
        
        var vertices:Vector3[] = new Vector3[4];
        vertices[0] = Vector3(width_, 0, height_);
        vertices[1] = Vector3(width_, 0, 0);
        vertices[2] = Vector3(0, 0, height_);
        vertices[3] = Vector3(0, 0, 0);
        
        var uv:Vector2[] = new Vector2[4];
        uv[0] = new Vector2(1, 1);
        uv[1] = new Vector2(1, 0);
        uv[2] = new Vector2(0, 1);
        uv[3] = new Vector2(0, 0);
        
        var triangles:int[] = new int[6];
        triangles[0] = 0;
        triangles[1] = 1;
        triangles[2] = 2;
        triangles[3] = 2;
        triangles[4] = 1;
        triangles[5] = 3;
        
        var normal:Vector3[] = new Vector3[4];
        normal[0] = Vector3.up;
        normal[1] = Vector3.up;
        normal[2] = Vector3.up;
        normal[3] = Vector3.up;
        
        mesh.vertices = vertices;
        mesh.uv = uv;
        mesh.triangles = triangles;
        mesh.normals = normal;
        mesh.RecalculateBounds();

        return mesh;
    }
}