一个挺好用的自己写的小插件(用与把一般的图片转换成预制)——UNITY3D

首先 下载一个DLL文件,名字:System.Windows.Forms。

然后把这个文件放在资源目录,位置随便。

接着上代码 :

using System.IO;
using UnityEditor;
using UnityEngine;
using System.Windows.Forms;

public class SpriteToPrefabs : MonoBehaviour
{

    static private string OpenPath()//选择路径
    {
        string path = null;
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.Description = "选择转换的文件";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            if (dialog.SelectedPath == "")
            {
                print("你没有选择任何内容");
            }
            else
            {
                DirectoryInfo wenjianjia = new DirectoryInfo(dialog.SelectedPath);
                path = dialog.SelectedPath.Replace("\\", "/");

            }
        }
        return path;
    }
    static private string SavePath()
    {
        string path = null;
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.Description = "文件保存的目录";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            if (dialog.SelectedPath == "")
            {
                print("你没有选择任何内容");
            }
            else
            {
                DirectoryInfo wenjianjia = new DirectoryInfo(dialog.SelectedPath);
                path = dialog.SelectedPath.Replace("\\", "/");

            }
        }
        return path;
    }

    [UnityEditor.MenuItem("MyMenu/AtlasMaker")]//转换格式
    static private void MakeAtlas()
    {
        DirectoryInfo rootDirInfo = new DirectoryInfo(OpenPath());
        string spriteDir = SavePath();

        if (!Directory.Exists(spriteDir))
        {
            Directory.CreateDirectory(spriteDir);
        }
        foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories())
        {
            foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories))
            {
                string allPath = pngFile.FullName;
                string assetPath = allPath.Substring(allPath.IndexOf("Assets"));
                //Sprite sprite = Resources.LoadAssetAtPath<Sprite>(assetPath);
                Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
                GameObject go = new GameObject(sprite.name);
                go.AddComponent<SpriteRenderer>().sprite = sprite;
                allPath = spriteDir + "/" + sprite.name + ".prefab";
                string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));
                PrefabUtility.CreatePrefab(prefabPath, go);
                GameObject.DestroyImmediate(go);
            }
        }
        Debug.Log("done");
    }

    
}

然后unity会多出一个栏“MyMenu”

点进去会一个“AtlasMaker”按钮

点击后出现一个选择文件路径栏

选好路径第二次弹出就是要保存的预制Prefabs的路径!然后系统会自动转换,转换好的会出现done!

至于这么做比打图集有什么优势这里不作说明!这还能扩展自动打包AB包功能!很好用!这个DLL文件百度很容易找到!

posted on 2017-10-08 18:42  tony86  阅读(1067)  评论(0编辑  收藏  举报

导航