截屏功能-压缩Texture保存本地

public void SaveTexture(string path, Texture2D texture, float scaleValue)
{
float tempX = texture.width;
float tempY = texture.height;
float x = tempX * scaleValue;
float y = tempY * scaleValue;
Texture2D result = new Texture2D(Convert.ToInt32(x), Convert.ToInt32(y), texture.format, false);
//result.filterMode = FilterMode.Bilinear;
for (int i = 0; i < result.height; i++)
{
for (int j = 0; j < result.width; j++)
{

//计算位置的比例,以这个比例在原图提取像素颜色
//GetPixelBilinear(x,y的位置比例来提取这个点的颜色);

Color newColor = texture.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
//将提取出来的颜色赋值给这个位置
result.SetPixel(j, i, newColor);
}
}
result.Apply();
byte[] byt = result.EncodeToPNG();
File.WriteAllBytes(path, byt);
Debug.Log("压缩完成");
}

posted on 2019-02-27 22:00  年轮下的  阅读(128)  评论(0编辑  收藏  举报

导航