Unity Editor 编写unity插件类

在unity写了一个编辑类,基于iTweenpath插件,为了更方便的操作iTweenpath,顺便练习UnityEditor的操作,写了一个CreateiTweenPath,放在Editor文件夹中。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

public class CreateiTweenPath :EditorWindow
{
    [MenuItem("GameObject/CreatePath")]
    static void main()
    {
        EditorWindow.GetWindow<CreateiTweenPath>("CreatePath");
    }

    private Vector2 scrollVec2;
    private Transform target=null;
    private string pathName="new path 1";
    private int nodeCount=2;
    private int speed=7;
    private Vector3[] nodes = new Vector3[]{Vector3.zero,new Vector3(10,0,0)};
    private PathLoopType loopType=PathLoopType.once;
    private Color pathColor = Color.cyan;

    void OnGUI()
    {
        scrollVec2=GUILayout.BeginScrollView(scrollVec2);
        target=EditorGUILayout.ObjectField("移动物体:", target, typeof(Transform)) as Transform;
        GUILayout.BeginHorizontal();
        GUILayout.Label("路径名称:");
        pathName=EditorGUILayout.TextField(pathName);
        GUILayout.Label("速度:");
        speed=EditorGUILayout.IntField(speed);
        GUILayout.EndHorizontal();
        loopType = (PathLoopType)EditorGUILayout.EnumPopup("循环类型:", loopType);
        pathColor = EditorGUILayout.ColorField("路径颜色:", pathColor);
        GUILayout.BeginHorizontal();
        GUILayout.Label("路径节点数:");
        nodeCount = EditorGUILayout.IntField(nodeCount);
        GUILayout.EndHorizontal();
        if (nodeCount > 0)
        {
            if (nodes.Length != nodeCount)
            {
                Vector3[] temp = nodes;
                nodes = new Vector3[nodeCount];
                for (int i = 0; i < temp.Length; i++)
                {
                    if (i < nodes.Length)
                        nodes[i] = temp[i];
                }
            }
            for (int i = 0; i < nodeCount; i++)
                nodes[i] = EditorGUILayout.Vector3Field("节点 "+(i+1)+":",nodes[i]);
        }
        if (GUILayout.Button("创建"))
            CreatePath();
        GUILayout.EndScrollView();
    }

    void CreatePath()
    {
        if (target == null)
        {
            EditorUtility.DisplayDialog("Error", "移动物体不能为null", "OK");
            return;
        }
        if (pathName == null || pathName == "")
            return;
        GameObject go = new GameObject();
        go.name = "iTweenPath_"+target.name;
        go.AddComponent<iTweenPath>();
        go.GetComponent<iTweenPath>().initialized = true;
        go.GetComponent<iTweenPath>().pathName = pathName;
        go.GetComponent<iTweenPath>().pathColor = pathColor;
        go.GetComponent<iTweenPath>().nodeCount = nodeCount;
        List<Vector3> listNodes = new List<Vector3>();
        for (int i = 0; i < nodes.Length; i++)
            listNodes.Add(nodes[i]);
        go.GetComponent<iTweenPath>().nodes = listNodes;

        go.AddComponent<GoPath>();
        go.GetComponent<GoPath>().target = target;
        go.GetComponent<GoPath>().pathName = pathName;
        go.GetComponent<GoPath>().speed = speed;
        go.GetComponent<GoPath>().loopType = loopType;

        EditorWindow.GetWindow<CreateiTweenPath>().Close();
    }
}


点击GameObject/CreatePath,显示一下界面,点击即可创建路径

猜你喜欢

转载自blog.csdn.net/qq_21743659/article/details/127315641
今日推荐