将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目录下

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值