Unity Editor 编辑器拓展 0

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

namespace Core{
    public enum CreatorType
    {
        T,
        N,
        DPS
    }
	public class ControlWindow : EditorWindow {
        Rect headerRect;
        Rect contentRect;
        Texture2D headerImg;
        Texture2D contentImg;
        GUISkin guiSkin;
    

        string charName;
        float health;
        GameObject prefab;
        CreatorType type;
        

        string myString = "Hello World";
        bool groupEnabled;
        bool myBool = true;
        float myFloat = 1.23f;
        string showText = "agasssssda";

        

        [MenuItem("KxpProject/MyWindow")]
        static void Init()
        {
            ControlWindow myWindow = (ControlWindow)GetWindow(typeof(ControlWindow));
            myWindow.minSize = new Vector2(300f, 300);
            myWindow.Show();
        }

        /// <summary>
        /// 创建调用 只在创建窗口时调用一次
        /// </summary>
        private void OnEnable()
        {
            headerImg = Resources.Load<Texture2D>("headerImg");
            contentImg = Resources.Load<Texture2D>("contentImg");
            guiSkin = Resources.Load<GUISkin>("CreatorGUISkin");
        }

        /// <summary>
        /// 刷新调用的方法
        /// </summary>
        private void OnGUI()
        {
            headerRect.x = 0;
            headerRect.y = 0;
            headerRect.width = 500;
            headerRect.height = 30;

            contentRect.x = 0;
            contentRect.y = 30;
            contentRect.width = Screen.width;
            contentRect.height = Screen.height-30;

            //背景颜色应该在最先画出来
            GUI.DrawTexture(headerRect, headerImg);
            GUI.DrawTexture(contentRect, contentImg);

            GUILayout.BeginArea(headerRect);
            GUILayout.Label("Header",guiSkin.GetStyle("BigLong")); //自己在Asset中创建GUISkin的文件,里面可以设置GUI样式很好用biglong就是customstyle的名字
            GUILayout.EndArea();

            GUILayout.BeginArea(contentRect);
            GUILayout.Label("Content");

            GUILayout.BeginHorizontal();//开启水平布置  默认是垂直布置
            GUILayout.Label("Name:");
            charName = EditorGUILayout.TextField(charName);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Health:");
            EditorGUILayout.FloatField(health);
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Prefab:");
            prefab = (GameObject)EditorGUILayout.ObjectField(prefab, typeof(GameObject), false); //最后的false是拖拽进去的物体是一定要是Prefab 如果是 true 则可以是非prefab
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Type:");
            type = (CreatorType)EditorGUILayout.EnumPopup(type);//popup就是下拉框
            GUILayout.EndHorizontal();

            if(GUILayout.Button("Creat!", GUILayout.Height(30)))
            {
                CreatThing();
            }

            GUILayout.EndArea();

            //如果写在了 Label之后,那么画上去的图片会把前面的label给覆盖掉
            //GUI.DrawTexture(headerRect, headerImg);
            //GUI.DrawTexture(contentRect, contentImg);
        }

        void CreatThing()
        {
            string oldPrefabPath;
            string newPrefabPath = "Assets/Resources/Texture/" + charName + ".prefab";

            oldPrefabPath = AssetDatabase.GetAssetPath(prefab);//获得先前物体的路径
            AssetDatabase.CopyAsset(oldPrefabPath, newPrefabPath);//旧物体拷贝一份到新的位置
            AssetDatabase.Refresh(); //刷新
            Debug.Log("调用");
           // GameObject newPrefab=AssetDatabase.LoadAssetAtPath
        }



    }
}

猜你喜欢

转载自blog.csdn.net/qq_14812585/article/details/86184139