Unity3d NGUI Atlas 图集批量解包、分割图片

using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class NGUI_atlas_split : MonoBehaviour
{
    //所有 Atlas 图集 放到  Atlas_path路径下,扫描该路径下的所有.prefab图集包
    public static string Atlas_path = @"C:\Game\Assets\Atlas_prefab";
    //解包后保存路径
    public static string save_Atlas_path = @"D:\Atlas_unpack";
    //忽略不解的包
    static string[] arStrings = {
      "Atlas_Raid",
      "Atlas_Public3",
      "Atlas_Public2",
  };
    [MenuItem("NGUI atlas/unpack")]
    static void Awake()
    {
        //获取指定路径下面的所有资源文件  
        if (!Directory.Exists(Atlas_path))
        {
            Debug.LogError("路径不存在");
        }
        DirectoryInfo direction = new DirectoryInfo(Atlas_path);
        var prefab_path = direction.GetFiles("*.prefab", SearchOption.AllDirectories)
            .Where((FileInfo file) =>
            {
                var file_name_no = file.Name.Substring(0, file.Name.LastIndexOf("."));
                bool c = Is_Contain(arStrings, file_name_no);
                return !c;
            })
            .Select((FileInfo file) =>
             {
                 Debug.Log(file.FullName);
                 var fullName = file.FullName;
                 int index = fullName.IndexOf("Assets");
                 if (index != -1)
                 {
                     var assets_path = fullName.Substring(index);
                     return assets_path;
                 }
                 else
                 {
                     return "";
                 }
             }).ToList();
        prefab_path.ForEach(x => Debug.Log(x));
        foreach (var item in prefab_path)
        {
            var atlas = AssetDatabase.LoadAssetAtPath(item, typeof(UIAtlas)) as UIAtlas;
            if (atlas == null || string.IsNullOrEmpty(atlas.name))
            {
                Debug.LogError("atlas == null || string.IsNullOrEmpty(atlas.name ,path: " + item);
            }
            else
            {
                CutTextures(atlas, false);
            }
            Resources.UnloadAsset(atlas);
        }
    }
    /*
        string[] array = { "the", "quick", "brown", "fox"};
        string searchString = "the";
        true == Is_Contain(array,searchString)
    */
    /// <summary>
    /// 是否包含该字符串
    /// </summary>
    /// <param name="array"></param>
    /// <param name="searchString"></param>
    /// <returns></returns>
    public static bool Is_Contain(string[] array, string searchString)
    {
        int index = Array.IndexOf(array, searchString);
        if (index == -1) //没找到
        {
            return false;
        }
        else //找到
        {
            return true;
        }
    }
    /*
    string str = "the_quick_brown_fox"};
    string searchString = "the";
    true == Is_Contain(str,searchString)
*/
    /// <summary>
    /// 是否包含该字符串
    /// </summary>
    /// <param name="array"></param>
    /// <param name="searchString"></param>
    /// <returns></returns>
    public static bool Is_Contain(string str, string searchString)
    {
        int index = str.IndexOf(searchString);
        if (index == -1) //没找到
        {
            return false;
        }
        else //找到
        {
            return true;
        }
    }
    /// <summary>
    /// 将图集中的所有图片拆分并保存
    /// </summary>
    /// <param name="_atlas">图集</param>
    /// <param name="_refresh">是否立即更新</param>
    public static void CutTextures(UIAtlas _atlas, bool _refresh = true)
    {
        if (_atlas == null) return;
        if (_atlas.GetListOfSprites() == null) return;
        if (_atlas.GetListOfSprites().buffer == null) return;
        if (_atlas.GetListOfSprites().buffer.Length <= 0) return;
        // 创建路径
        var path = save_Atlas_path;
        if (!System.IO.Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        path = save_Atlas_path + "/" + _atlas.name;
        if (!System.IO.Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        // 开始
        var sprites = _atlas.GetListOfSprites();
        foreach (string spriteName in sprites)
        {
            path = save_Atlas_path + "/" + _atlas.name + "/" + _atlas.name + "_" + spriteName + ".png";
            SaveSpriteAsTexture(_atlas, spriteName, path);
        }
        if (_refresh)
            AssetDatabase.Refresh();
    }
    /// <summary>
    /// 保存图集中的图片
    /// </summary>
    /// <param name="_atlas">图片所在的图集</param>
    /// <param name="_sprite">图集中要保存的图片名字</param>
    /// <param name="_path">要保存的路径</param>
    public static void SaveSpriteAsTexture(UIAtlas _atlas, string _sprite, string _path)
    {
        UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(_atlas, _sprite);
        if (se != null)
        {
            byte[] bytes = se.tex.EncodeToPNG();
            System.IO.File.WriteAllBytes(_path, bytes);
            AssetDatabase.ImportAsset(_path);
            if (se.temporaryTexture)
                Object.DestroyImmediate(se.tex);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值