发布网友 发布时间:2022-04-24 02:13
共1个回答
热心网友 时间:2023-10-21 06:41
由于项目需求,需要在unity中播放高清视频,视频分辨率达到了3840x1200。采用的是c++
plugin解码视频,提供图片纹理给unity渲染的方式。而在unity中使用的是rendertexture来保存解码的视频图片。为了方面调试,需要保存某一些时刻的图片数据到本地,可以采用下面的函数实现:
[csharp]
view
plain
copy
[contextmenu("save
png")]
private
void
savetexturetofile()
{
if
(outputtexture
!=
null)
{
rendertexture
prev
=
rendertexture.active;
rendertexture.active
=
target;
texture2d
png
=
new
texture2d(outputtexture.width,
outputtexture.height,
textureformat.argb32,
false);
png.readpixels(new
rect(0,
0,
outputtexture.width,
outputtexture.height),
0,
0);
byte[]
bytes
=
png.encodetopng();
string
path
=
string.format("mp/raw
{0}.png",
random.range(0,
65536).tostring("x"));
filestream
file
=
file.open(path,
filemode.create);
binarywriter
writer
=
new
binarywriter(file);
writer.write(bytes);
file.close();
texture2d.destroy(png);
png
=
null;
rendertexture.active
=
prev;
}
}
热心网友 时间:2023-10-21 06:41
由于项目需求,需要在unity中播放高清视频,视频分辨率达到了3840x1200。采用的是c++
plugin解码视频,提供图片纹理给unity渲染的方式。而在unity中使用的是rendertexture来保存解码的视频图片。为了方面调试,需要保存某一些时刻的图片数据到本地,可以采用下面的函数实现:
[csharp]
view
plain
copy
[contextmenu("save
png")]
private
void
savetexturetofile()
{
if
(outputtexture
!=
null)
{
rendertexture
prev
=
rendertexture.active;
rendertexture.active
=
target;
texture2d
png
=
new
texture2d(outputtexture.width,
outputtexture.height,
textureformat.argb32,
false);
png.readpixels(new
rect(0,
0,
outputtexture.width,
outputtexture.height),
0,
0);
byte[]
bytes
=
png.encodetopng();
string
path
=
string.format("mp/raw
{0}.png",
random.range(0,
65536).tostring("x"));
filestream
file
=
file.open(path,
filemode.create);
binarywriter
writer
=
new
binarywriter(file);
writer.write(bytes);
file.close();
texture2d.destroy(png);
png
=
null;
rendertexture.active
=
prev;
}
}