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

}

 

相关文章

实现Unity AssetBundle资源加载管理器 AssetBundle是实现资源...
Unity3D 使用LineRenderer绘制尾迹与虚线 1.添加LineRendere...
Unity 添加新建Lua脚本选项 最近学习Unity的XLua热更新框架的...
挂载脚本时文件名和类名的关联方式 写过Unity脚本的人应该都...
Unity单例基类的实现方式 游戏开发的过程中我们经常会将各种...
这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...