unity3d 图片替换工具

最近写了个替换系统图片资源的工具,拖拽项目内图片资源路径文件夹,会生成显示图片缩略图和名字的item,在右侧把需要替换的图片拖进来点击确定就可以了,主要是省了重命名这一步

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public Class MyItem
{
    public string name;
    public Texture2D image;
	public Texture2D draggedTex;
	public string draggedTexName;
	public string path;
	public MyItem()
	{
		name = "";
		image = null;
		draggedTex = null;
	}
}
public class ImageReplace: EditorWindow
{
    [MenuItem("Window/ImageReplace")]
    public static void OpenWindow()
    {
        var win = GetWindom(ImageReplace)();
        win.titleContent = new GUIContent("Image Place");
        win.Show();
    }
	private List items = new List();
	private string folderPath;
	private Vector2 ScrollPosition; 
    void OnGUI()
    {
        GUILayout.Label("Drop an Image file here", EditorStyles.boldLabel);
        Event currentEvent = Event.current;
        Rect dropArea = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
        switch (currentEvent.type)
        {
            case EventType.DragUpdated:
            case EventType.DragPerform:
                if (!dropArea.Contains(currentEvent.mousePosition))
                    break;

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (currentEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    foreach (var path in DragAndDrop.paths)
                    {
                       if(File.GetAttributes(path).HasFlag(FileAttributes.Directory))
                       {
							var allFiles = Directory.GetFiles(path,"*.*",SearchOption.AllDirectories);
							{
								foreach(var file in allFiles)
								{
									if(Path.GetExtension(file) == ".png" || Path.GetExtension(file) == ".jng" )
									{
											CreateItem(file);
									}
								}
							}
						}
						else
						{
							if(Path.GetExtension(file) == ".png" || Path.GetExtension(file) == ".jng" )
							{
								CreateItem(file);
							}
						}
                    }
                }

                Event.current.Use();
                break;
        }
	 GUILayout.Space(10);
	 scrollPosition = GUILayout.BeginScrollView(scrollPosition);
	 foreach (var item in items)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(item.name,GUILayout.Width(200));
            GUILayout.Box(item.image,GUILayout.Width(100),GUILayout.Height(100));
            GUIStyle style = new GUIStyle();
          	style.normal.textColor = Color.white;
          	Rect dropArea2 = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Width(100),GUILayout.Height(100));
          	Event dropEvent = Event.current;
        	switch (dropEvent .type)
        	{
            	case EventType.DragUpdated:
            	case EventType.DragPerform:
                if (!dropArea.Contains(dropEvent .mousePosition))
                    break;

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (currentEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    foreach (var path in DragAndDrop.paths)
                    {
							if(Path.GetExtension(file) == ".png" || Path.GetExtension(file) == ".jng" )
							{
								Texture2D tex = new Texture2D (2,2);
								tex.LoadImage(File.ReadAllBytes(path));
								item.draggedTex = tex;
								item.draggedTexName =Path.GetFileName(path);
							}
                    }
                }

                dropEvent .Use();
                break;
        }
         GUILayout.Box(item.draggedTex,GUILayout.Width(100),GUILayout.Height(100));
          GUILayout.EndHorizontal();
        }
        GUILayout.EndScrollView();
        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        if(GUILayout.Button("确定"))
        {
			ApplyChanges();
		}
		if(GUILayout.Button("清除"))
        {
			ClearItems();
		}
         GUILayout.EndHorizontal();
    }
    private void CreateItem(string file)
    {
		MyItem newItem = new MyItem
		{
			name = Path.GetFileName(file);
			path = file;
			image = AssetDatabase.LoadAssetAtPath(file);
		}
		items.Add(newItem);
	}
	private void ApplyChanges()
	{
		 foreach (var item in items)
        {
			if(item.draggedTex)
			{
				byte[] imgBytes = item.draggedTex.EncodeToPNG();
				File.WriteAllBytes(item.path,imgBytes );
			}
		}
		Debug.Log("Finish");
	}
	private void ClearItems()
	{
		items.Clear();
	}
}

你可能感兴趣的:(unity3d)