将texturepacker生成的png和xml资源反向生成小图供unity使用

最近破解一个游戏apk,需要提取里面的资源文件,在unity中使用。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.IO;
public class ParseXml : MonoBehaviour {
 
	// Use this for initialization
    public Image image;
	void Start () {
        Object[] objs= Resources.LoadAll("xml/");
        for (int i = 0; i < objs.Length; i++ )
        {
            loadXml(objs[i].ToString());
        }
        //loadXml(file.ToString());
	}
	
	// Update is called once per frame
	void Update () {
	
	}
 
    void loadXml(string source)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(source);
        XmlNode rootNode = doc.SelectSingleNode("TextureAtlas");
        string imagePath = rootNode.Attributes["imagePath"].Value;
        int SOURCEHEIGHT = int.Parse(rootNode.Attributes["height"].Value);
        Texture2D tex = Resources.Load(imagePath.Substring(0, imagePath.Length -4)) as Texture2D;
        
        Debug.Log(tex.width  +"      "+tex.height);
        //image.sprite = tex as Sprite;
        XmlNodeList list = rootNode.ChildNodes;
        foreach(XmlNode node in list)
        {
            XmlElement e = (XmlElement)node;
            string name = e.GetAttribute("n").ToString();
            int x = int.Parse(e.GetAttribute("x").ToString());
            int y = int.Parse(e.GetAttribute("y").ToString());
            int w = int.Parse(e.GetAttribute("w").ToString());
            int h = int.Parse(e.GetAttribute("h").ToString());
 
            Texture2D mTexture = new Texture2D(w, h, TextureFormat.RGBA32, false);
                        
            for (int i = 0; i < w; i++)
            {
                for(int j = 1;j <=h; j++)
                {
                    mTexture.SetPixel(i, h-j, tex.GetPixel(x + i, SOURCEHEIGHT - (y + j)));
                }
            }
            
            Debug.Log(name + "---" + x + "---" + y + "---" + w + "---" + h);
 
            byte[] bt = mTexture.EncodeToPNG();
            Directory.CreateDirectory(Application.dataPath + "/result/" + imagePath);
            string savePath = Application.dataPath + "/result/" + imagePath +"/"+ name + ".png";
            File.WriteAllBytes(savePath, bt);
 
        }
    }
}

将xml文件放到Resources的xml目录下,png文件放到Resources目录下

猜你喜欢

转载自blog.csdn.net/lizhenxiqnmlgb/article/details/81430771