有那么一种需求,就是将NGUI的UITexture.mainTexture或RawImage.mainTexture内容保存成一张图片,因为mainTexture是Texture类型,没有可以保存的方法,所以需要转换成Texture2D。方法就是先将Texture转换到RenderTexture,然后渲染到缓存池中,再用Texture2D去读取出来。
//将Texture转换为Texture2D public Texture2D TexToTex2D(Texture t) { Texture2D tex2d = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false); RenderTexture currentRT = RenderTexture.active;//临时保存原来的渲染目标 RenderTexture renTex = RenderTexture.GetTemporary(t.width, t.height, 32);//获取临时渲染纹理 Graphics.Blit(t, renTex);//将Texture渲染到RenderTexture RenderTexture.active = renTex; tex2d.ReadPixels(new Rect(0, 0, renTex.width, renTex.height), 0, 0); tex2d.Apply(); RenderTexture.active = currentRT; RenderTexture.ReleaseTemporary(renTex); return tex2d; }