取出图集中的精灵

Babybus-u3d技术交流-取出图集中的精灵


using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
public class UIAtlasContextMenu
{
[MenuItem("CONTEXT/UIAtlas/Extract Sprite List", true)]
 
static bool ExtractSpriteListValidation(MenuCommand menuCommand)    
{        
return AssetDatabase.GetAssetPath(menuCommand.context) != "";    
}    
[MenuItem("CONTEXT/UIAtlas/Extract Sprite List")]    
static void ExtractSpriteList(MenuCommand menuCommand)    
       
var atlas = menuCommand.context as UIAtlas;
string path = AssetDatabase.GetAssetPath(atlas);
       
string directory = Regex.Match(path, "[^\\.]*").Value + "/";
       
if (!Directory.Exists(directory))            
  Directory.CreateDirectory(directory);
       
foreach (UISpriteData spriteData in atlas.spriteList)
{

           Debug.Log("Extract Sprite " + spriteData.name);
           UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(atlas, spriteData.name); 
           
if (se != null)            
{
               byte[] bytes = se.tex.EncodeToPNG();

               File.WriteAllBytes(directory + spriteData.name + ".png", bytes);

               AssetDatabase.ImportAsset(directory + spriteData.name + ".png");

               if (se.temporaryTexture) Object.DestroyImmediate(se.tex);

           }

       }

   }
}

NGUI有图集打包功能,以上代码是逆向,把打包好的图集分解,提取出图集中的原始精灵。

取出图集中的精灵_第1张图片
201504191429420309119021.png

你可能感兴趣的:(取出图集中的精灵)