zoukankan      html  css  js  c++  java
  • 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,显示一下界面,点击即可创建路径


  • 相关阅读:
    Webpack4不求人(5) ——编写自定义插件
    Webpack4不求人(4)——编写自定义Loader
    Webpack4不求人(3) ——性能优化
    Webpack4不求人(2) ——手把手搭建TypeScript+React16+ReactRouter5同构应用脚手架
    Shell脚本快速入门(1)
    kafka二进制协议分析与PHP客户端开发
    深入浅出ES6的标准内置对象Proxy
    ES6的Set类型
    深入浅出ES6的迭代器
    Javascript事件系统
  • 原文地址:https://www.cnblogs.com/liang123/p/6325907.html
Copyright © 2011-2022 走看看