unity编辑器学习,创建自己的窗口

又是一个体力活,资源搬家的。具体的任务我就不透漏了,反正是底层的资管管理之类的东东。

为了简化操作,我还是坚持写了一个插件,然后感觉做个自己的小窗口也不错,就写了个小小的编辑器插件。

OK,这个小窗口完成的功能很简单,就是移动资源到某一文件夹的。好了,代码分享如下,

看效果的话,直接放到项目文件里面重启unity就行了。代码很简单,注释就不写了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.IO;
public class OneKeyMove : EditorWindow
{
    public ResPath resPath;
    void Start(){
        if(resPath==null){
            resPath = new ResPath();
        }
    
    }
    [MenuItem("MyUtil/一键移动")]
    public static void Init()
    {
       
        if (Resources.FindObjectsOfTypeAll<OneKeyMove>().Length == 0)
        {
            var win = ScriptableObject.CreateInstance<OneKeyMove>();
            win.title = "一键移动";
            win.Show();
        }
    }
    public void OnGUI()
    {
        if (resPath == null)
        {
            resPath = new ResPath();
        }
        EditorGUILayout.LabelField("移动文件", EditorStyles.boldLabel);
        EditorGUILayout.Space();
        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("当前文件", GUILayout.Width(80));
        Rect firstRect = EditorGUILayout.GetControlRect(GUILayout.Width(200));
        resPath._FirstFile = EditorGUI.TextField(firstRect, resPath._FirstFile);
        if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited) && firstRect.Contains(Event.current.mousePosition))
        {
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                string sfxPath = DragAndDrop.paths[0];
                if (!string.IsNullOrEmpty(sfxPath) && Event.current.type == EventType.DragUpdated)
                {
                    DragAndDrop.AcceptDrag();
                    resPath._FirstFile = sfxPath;
                }
            }
        }
        GUILayout.EndHorizontal();
        EditorGUILayout.Space();
        GUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("目标", GUILayout.Width(80));
        Rect secondRect = EditorGUILayout.GetControlRect(GUILayout.Width(200));
        resPath._SecondFile = EditorGUI.TextField(secondRect, resPath._SecondFile);
        if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited) && secondRect.Contains(Event.current.mousePosition))
        {
            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                string sfxPath = DragAndDrop.paths[0];
                if (!string.IsNullOrEmpty(sfxPath) && Event.current.type == EventType.DragUpdated)
                {
                    DragAndDrop.AcceptDrag();
                    resPath._SecondFile = sfxPath;
                }
            }
        }
        GUILayout.EndHorizontal();
        EditorGUILayout.Space();
        if (GUILayout.Button("一键移动"))
        {
            string resName = resPath._FirstFile.Remove(0, resPath._FirstFile.LastIndexOf('/') + 1);
            Debug.Log(resName);

           
            string targetPath = resPath._SecondFile + "/layout/" + resName;
            string realPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/'))+"/"+resPath._SecondFile + "/layout";
            if (!Directory.Exists(realPath))
            {
                Directory.CreateDirectory(realPath);
            }
            AssetDatabase.Refresh();
            Debug.Log(targetPath);
            AssetDatabase.MoveAsset(resPath._FirstFile, targetPath);
        }
        EditorGUILayout.Space();
        
    }
    
}
[Serializable]
public class ResPath {
    public string _FirstFile="";
    public string _SecondFile="";
}

 

posted @ 2015-10-16 15:33  决晴谷  阅读(2284)  评论(0编辑  收藏  举报