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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章