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

本文介绍了一个Unity脚本,该脚本可以批量将Sprites自动转换为Prefabs,并提供了选择文件夹及保存路径的功能。使用System.Windows.Forms DLL实现文件夹选择对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先 下载一个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文件百度很容易找到!

转载于:https://www.cnblogs.com/xxxtony/p/7638165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值