制作Texture2d 的缩略图

更改Texture的尺寸,并返回新尺寸的 texture

用法:var newText = texture.NewSize(128, 128);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
     
/// <summary>
/// Texture 扩展
/// </summary>
public static class TextureExtent
{

/// <summary>
/// 创建新尺寸的 Texture
/// </summary>
/// <param name="tex"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Texture NewSize(this Texture tex, int width, int height)
{
if (null == tex)
return null;

tex.filterMode = FilterMode.Point;
RenderTexture rt = RenderTexture.GetTemporary(width, height);
rt.filterMode = FilterMode.Point;
RenderTexture.active = rt;
Graphics.Blit(tex, rt);
var nTex = new Texture2D(width, height);
nTex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
nTex.Apply();
RenderTexture.active = null;
return nTex;
}

}