您当前的位置: 首页 >  鱼儿-1226 unity

Unity Editor 编写unity插件类

鱼儿-1226 发布时间:2022-10-14 10:33:15 ,浏览量:5

在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("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();
        go.GetComponent().initialized = true;
        go.GetComponent().pathName = pathName;
        go.GetComponent().pathColor = pathColor;
        go.GetComponent().nodeCount = nodeCount;
        List listNodes = new List();
        for (int i = 0; i < nodes.Length; i++)
            listNodes.Add(nodes[i]);
        go.GetComponent().nodes = listNodes;

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

        EditorWindow.GetWindow().Close();
    }
}


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

关注
打赏
查看更多评论

鱼儿-1226

暂无认证

  • 5浏览

    0关注

    855博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录