微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity 提取像素图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class Test : MonoBehavIoUr
{
    
    public int m_x;
    public int m_y;



    IEnumerator enumerator()
    {
        int a = 0;
        Texture2D[] temp = Resources.LoadAll<Texture2D>("Imgs");
        foreach (Texture2D tx in temp)
        {
            Texture2D texture = new Texture2D(15, 15);
            int w = tx.width;
            int h = tx.height;
            float x = w / m_x;
            float y = h / m_y;
            float startx = x / 2;
            float starty = y / 2;
            Color color;
            for (int i =0;i<m_x;i++)
            {
                for (int j=0;j<m_y; j++)
                {
                    color = tx.GetPixel((int)(startx +x*i), (int)(starty + y * j));
                    texture.SetPixel(i, j, color);
                }
            }
            ToPng(texture,
        Application.dataPath + "/temp",a.ToString ("000"));
            yield return 1;
            a++;
        }
    }

    void Start()
    {
        StartCoroutine(enumerator());
    }

    void ToPng(Texture2D texture, string contents, string pngName)
    {
        if (!Directory.Exists(contents))
            Directory.CreateDirectory(contents);
        var temp = texture.EncodetoPNG();
        FileStream file = File.Open(contents + "/" + pngName + ".png", FileMode.Create);
        BinaryWriter writer = new BinaryWriter(file);
        writer.Write(temp);
        file.Close();
    }

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐