扩展编辑器的脚本都应该放到名为Editor的文件夹下。

这个脚本用于创建编辑器附加组件,并采用全定制方式在脚本中生成Quad,对应位置为网格的轴心点。

通过自定义编辑器可以生成顶点和三角面更少的平面,可以起到优化的作用。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;

using System.IO;

public class CreatePlan : ScriptableWizard

{

  public enum AnchorPoint

    {

        TopLeft,

        TopMiddle,

        TopRight,

        RightMiddle,

        BottomRight,

        BottomMiddle,

        BottomLeft,

        LeftMiddle,

        Center,

        Custom

    }

    public string MeshName = "Quad";

    public string GameObjectName = "Plane_Object";

    public string AssetFolder= "Assets";

    public float Width = 1.0f;

    public float Height = 1.0f;

    public AnchorPoint Anchor = AnchorPoint.Center;

    public float AnchorX = 0.5f;

    public float AnchorY = 0.5f;

    [MenuItem("GameObject/Create Other/Custom Plane")]

    static void CreateWizard()

    {

        DisplayWizard("CreatePlane", typeof(CreatePlan));

    }

    private void OnEnable()

    {

        OnSelectionChange();

    }

    void OnInspectorUpdate()

    {

        switch(Anchor)

        {

            case AnchorPoint.TopLeft:

                AnchorX = 0.0f * Width;

                AnchorY = 1.0f * Height;

                break;

            case AnchorPoint.TopMiddle:

                AnchorX = 0.5f * Width;

                AnchorY = 0.5f * Height;

                break;

            case AnchorPoint.TopRight:

                AnchorX = 1.0f * Width;

                AnchorY = 1.0f * Height;

                break;

            case AnchorPoint.RightMiddle:

                AnchorX = 1.0f * Width;

                AnchorY = 0.5f * Height;

                break;

            case AnchorPoint.BottomRight:

                AnchorX = 1.0f * Width;

                AnchorY = 0.0f * Height;

                break;

            case AnchorPoint.BottomMiddle:

                AnchorX = 0.5f * Width;

                AnchorY = 0.0f * Height;

                break;

            case AnchorPoint.BottomLeft:

                AnchorX = 0.0f * Width;

                AnchorY = 0.0f * Height;

                break;

            case AnchorPoint.LeftMiddle:

                AnchorX = 0.0f * Width;

                AnchorY = 0.5f * Height;

                break;

            case AnchorPoint.Center:

                AnchorX = 0.5f * Width;

                AnchorY = 0.5f * Height;

                break;

            case AnchorPoint.Custom:

            default:

                break;

        }

    }

    private void OnSelectionChange()

    {

        if(Selection.objects!=null && Selection.objects.Length==1)

        {

            AssetFolder = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Selection.objects[0]));

        }

    }

    private void OnWizardCreate()

    {

        Vector3[] Vertices = new Vector3[4];

        Vector2[] UVs = new Vector2[4];

        int[] Triangles = new int[6];

        Vertices[0].x = -AnchorX;

        Vertices[0].y = -AnchorY;

        Vertices[1].x = Vertices[0].x + Width;

        Vertices[1].y = Vertices[0].y;

        Vertices[2].x = Vertices[0].x;

        Vertices[2].y = Vertices[0].y + Height;

        Vertices[3].x = Vertices[0].x + Width;

        Vertices[3].y = Vertices[0].y + Height;

        UVs[0].x = 0.0f;

        UVs[0].y = 0.0f;

        UVs[1].x = 1.0f;

        UVs[1].y = 0.0f;

        UVs[2].x = 0.0f;

        UVs[2].y = 1.0f;

        UVs[3].x = 1.0f;

        UVs[3].y = 1.0f;

        Triangles[0] = 3;

        Triangles[1] = 1;

        Triangles[2] = 2;

        Triangles[3] = 2;

        Triangles[4] = 1;

        Triangles[5] = 0;

        Mesh mesh = new Mesh();

        mesh.name = MeshName;

        mesh.vertices = Vertices;

        mesh.uv = UVs;

        mesh.triangles = Triangles;

        mesh.RecalculateNormals();

        AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(AssetFolder + "/" + MeshName) + ".asset");

        AssetDatabase.SaveAssets();

        GameObject plane = new GameObject(GameObjectName);

        MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));

        meshFilter.sharedMesh = mesh;

        mesh.RecalculateBounds();

        plane.AddComponent<MeshRenderer>();

    }

}