C# 将纹理转换为精灵
我有一个纹理,我用它作为屏幕截图。 当我将纹理保存为png文件时,它看起来很完美,但我也想在屏幕上显示捕获的图像。因此,我将纹理转换为Sprite,但出于某种原因,最终结果Sprite只是屏幕上另一个UI图像的随机图像,而不是保存到png文件中的纹理,这是正确的 这是我的密码:C# 将纹理转换为精灵,c#,unity3d,textures,sprite,C#,Unity3d,Textures,Sprite,我有一个纹理,我用它作为屏幕截图。 当我将纹理保存为png文件时,它看起来很完美,但我也想在屏幕上显示捕获的图像。因此,我将纹理转换为Sprite,但出于某种原因,最终结果Sprite只是屏幕上另一个UI图像的随机图像,而不是保存到png文件中的纹理,这是正确的 这是我的密码: var tex = new Texture2D(recwidth, recheight, TextureFormat.RGB24, false); Rect rex = new Rect(
var tex = new Texture2D(recwidth, recheight, TextureFormat.RGB24, false);
Rect rex = new Rect(startX, startY, (float)recwidth, (float)recheight);
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
tex.ReadPixels(rex, rdPXX, rdPXY);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Sprite sprite = new Sprite();
sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
image.GetComponent<Image>().overrideSprite = sprite;
Destroy(tex);
var tex=new Texture2D(recwidth,recheight,TextureFormat.RGB24,false);
Rect rex=新的Rect(startX,startY,(浮动)recwidth,(浮动)recheight);
WaitForEndOfFrame frameEnd=新的WaitForEndOfFrame();
tex.ReadPixels(rex、rdPXX、rdPXY);
tex.Apply();
//将纹理编码为PNG
var bytes=tex.EncodeToPNG();
精灵精灵=新精灵();
sprite=sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(tex.width/2,tex.height/2));
image.GetComponent().overrideSprite=sprite;
销毁(tex);
不要破坏纹理。精灵始终需要纹理参考。精灵对象仅保留有关纹理的元数据,如轴或UV(精灵图集中的位置和大小)不要破坏纹理。精灵始终需要纹理参考。精灵对象只保留有关纹理的元数据,如轴或UV(精灵图集中的位置和大小)只需使用带有参数的Sprite.create()直接创建精灵即可。Unity现在是Create()的默认构造函数。也不要破坏纹理
试试这个
var tex = new Texture2D(recwidth, recheight, TextureFormat.RGB24, false);
Rect rex = new Rect(startX, startY, (float)recwidth, (float)recheight);
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
tex.ReadPixels(rex, rdPXX, rdPXY);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
image.GetComponent<Image>().overrideSprite = sprite;
var tex=new Texture2D(recwidth,recheight,TextureFormat.RGB24,false);
Rect rex=新的Rect(startX,startY,(浮动)recwidth,(浮动)recheight);
WaitForEndOfFrame frameEnd=新的WaitForEndOfFrame();
tex.ReadPixels(rex、rdPXX、rdPXY);
tex.Apply();
//将纹理编码为PNG
var bytes=tex.EncodeToPNG();
Sprite Sprite=Sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(tex.width/2,tex.height/2));
image.GetComponent().overrideSprite=sprite;
只需使用带参数的sprite.create()直接创建精灵即可。Unity现在是Create()的默认构造函数。也不要破坏纹理
试试这个
var tex = new Texture2D(recwidth, recheight, TextureFormat.RGB24, false);
Rect rex = new Rect(startX, startY, (float)recwidth, (float)recheight);
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
tex.ReadPixels(rex, rdPXX, rdPXY);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width / 2, tex.height / 2));
image.GetComponent<Image>().overrideSprite = sprite;
var tex=new Texture2D(recwidth,recheight,TextureFormat.RGB24,false);
Rect rex=新的Rect(startX,startY,(浮动)recwidth,(浮动)recheight);
WaitForEndOfFrame frameEnd=新的WaitForEndOfFrame();
tex.ReadPixels(rex、rdPXX、rdPXY);
tex.Apply();
//将纹理编码为PNG
var bytes=tex.EncodeToPNG();
Sprite Sprite=Sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(tex.width/2,tex.height/2));
image.GetComponent().overrideSprite=sprite;