unity編輯器學習,建立本身的窗口

又是一個體力活,資源搬家的。具體的任務我就不透漏了,反正是底層的資管管理之類的東東。編輯器

爲了簡化操做,我仍是堅持寫了一個插件,而後感受作個本身的小窗口也不錯,就寫了個小小的編輯器插件。spa

OK,這個小窗口完成的功能很簡單,就是移動資源到某一文件夾的。好了,代碼分享以下,插件

看效果的話,直接放到項目文件裏面重啓unity就好了。代碼很簡單,註釋就不寫了:code

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="";
}
相關文章
相關標籤/搜索