C# 在Unity UI层捕获360屏幕截图
问题:我想在UnityEditor中拍摄一些界面元素的360度等矩形屏幕截图。我找到的这个工具可以拍摄360张截图,但是没有画布元素。如果我在场景中放置一个立方体,它将出现在屏幕截图上。如果我在场景中放置画布,它将在屏幕截图中不可见 Screenshotcontroller如下所示:C# 在Unity UI层捕获360屏幕截图,c#,unity3d,C#,Unity3d,问题:我想在UnityEditor中拍摄一些界面元素的360度等矩形屏幕截图。我找到的这个工具可以拍摄360张截图,但是没有画布元素。如果我在场景中放置一个立方体,它将出现在屏幕截图上。如果我在场景中放置画布,它将在屏幕截图中不可见 Screenshotcontroller如下所示: void Update() { if (Input.GetKeyDown("s")) { byte[] screenshot = I360Re
void Update()
{
if (Input.GetKeyDown("s"))
{
byte[] screenshot = I360Render.Capture( 2048, true, null, true );
File.WriteAllBytes("D:/screenshot.jpg", screenshot);
}
}
执行屏幕截图的实际脚本如下所示:
public static class I360Render
{
private static Material equirectangularConverter = null;
private static int paddingX;
public static byte[] Capture( int width = 1024, bool encodeAsJPEG = true, Camera renderCam = null, bool faceCameraDirection = true )
{
if( renderCam == null )
{
renderCam = Camera.main;
if( renderCam == null )
{
Debug.LogError( "Error: no camera detected" );
return null;
}
}
RenderTexture camTarget = renderCam.targetTexture;
if( equirectangularConverter == null )
{
equirectangularConverter = new Material( Shader.Find( "Hidden/I360CubemapToEquirectangular" ) );
paddingX = Shader.PropertyToID( "_PaddingX" );
}
int cubemapSize = Mathf.Min( Mathf.NextPowerOfTwo( width ), 8192 );
RenderTexture activeRT = RenderTexture.active;
RenderTexture cubemap = null, equirectangularTexture = null;
Texture2D output = null;
try
{
cubemap = RenderTexture.GetTemporary( cubemapSize, cubemapSize, 0 );
cubemap.dimension = UnityEngine.Rendering.TextureDimension.Cube;
equirectangularTexture = RenderTexture.GetTemporary( cubemapSize, cubemapSize / 2, 0 );
equirectangularTexture.dimension = UnityEngine.Rendering.TextureDimension.Tex2D;
if( !renderCam.RenderToCubemap( cubemap, 63 ) )
{
Debug.LogError( "Rendering to cubemap is not supported on device/platform!" );
return null;
}
equirectangularConverter.SetFloat( paddingX, faceCameraDirection ? ( renderCam.transform.eulerAngles.y / 360f ) : 0f );
Graphics.Blit( cubemap, equirectangularTexture, equirectangularConverter );
RenderTexture.active = equirectangularTexture;
output = new Texture2D( equirectangularTexture.width, equirectangularTexture.height, TextureFormat.RGB24, false );
output.ReadPixels( new Rect( 0, 0, equirectangularTexture.width, equirectangularTexture.height ), 0, 0 );
return encodeAsJPEG ? InsertXMPIntoTexture2D_JPEG( output ) : InsertXMPIntoTexture2D_PNG( output );
}
catch( Exception e )
{
Debug.LogException( e );
return null;
}
finally
{
renderCam.targetTexture = camTarget;
RenderTexture.active = activeRT;
if( cubemap != null )
RenderTexture.ReleaseTemporary( cubemap );
if( equirectangularTexture != null )
RenderTexture.ReleaseTemporary( equirectangularTexture );
if( output != null )
UnityEngine.Object.DestroyImmediate( output );
}
}
出什么事了?为什么此解决方案似乎忽略了UI元素?屏幕空间-叠加UI元素通常不会渲染到相机。它们直接渲染到屏幕上。由于您的屏幕截图基于相机的纹理,因此您不会捕获直接渲染到屏幕上的画布或其他UI元素
<>你可以考虑改变你的画布来渲染屏幕空间相机。您可能需要稍微修改一下UI。找到了答案。这实际上是Unity的360 Capture SDK中的一个bug,该SDK已经存在一年多了,与RenderToCubemap函数有关。
答案#4和#10中有一个棘手的解决办法。但它不适用于使用TextMesh Pro的文本元素,会导致性能下降。谢谢您的回复。我的UI元素位于世界空间中,当我将其输出到平面时,它们会出现在渲染纹理上。对不起,我忘了提。问题与Unity的RenderToCubemap函数有关,请参阅我的下一个答复。@nrs这是有道理的。这似乎是SDK的一个bug,已经存在很长时间了。其他一些解决方案提到单独捕获UI并将其绘制在立方体地图上。嗨,你试过玩“层”吗?也许你的工具是截屏特定的层,而不是UI经典的,我试过了,但没有成功。正如我刚刚发现的那样,这是一个已经在Unity论坛上讨论过的bug。请阅读下面我的答案。