Skip to content

Instantly share code, notes, and snippets.

@rngtm
Created December 16, 2024 23:30
Show Gist options
  • Save rngtm/eb94adc3f4c8cc2d61749d7f82ac1633 to your computer and use it in GitHub Desktop.
Save rngtm/eb94adc3f4c8cc2d61749d7f82ac1633 to your computer and use it in GitHub Desktop.
//
// スクリーンショットをPNG画像として保存
// 動作環境 : Unity2022.3.31f1, Universal RP
//
using System.Collections;
using Unio; // https://github.com/hadashiA/Unio
using Unity.Collections;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
public class MyScreenCapture : MonoBehaviour
{
public Camera _targetCamera;
public RenderTexture _renderTexture;
private IEnumerator Start()
{
_renderTexture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32);
yield return null;
CaptureStart(Camera.main);
yield return null;
CaptureEnd();
yield return null;
SaveTexture(_renderTexture);
SaveNativeArray(_renderTexture);
}
private void OnDestroy()
{
Destroy(_renderTexture);
}
/// <summary>
/// RenderTextureからTexture2Dに作成して保存
/// </summary>
private static void SaveTexture(RenderTexture rt)
{
AsyncGPUReadback.Request(rt, 0, request =>
{
if (request.hasError)
{
// エラー
Debug.LogError("Error.");
}
else
{
Debug.Log("Save Start SS");
var customSampler = CustomSampler.Create("# Save Texture2D");
customSampler.Begin();
NativeArray<Color32> data = request.GetData<Color32>();
var texture = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
texture.LoadRawTextureData(data);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
NativeFile.WriteAllBytes($"SS_Texture.png", bytes);
Destroy(texture);
customSampler.End();
Debug.Log("Save End SS");
}
});
}
/// <summary>
/// RenderTextureをNativeArrayに変換してから保存
/// </summary>
private static void SaveNativeArray(RenderTexture rt)
{
AsyncGPUReadback.Request(rt, 0, request =>
{
if (request.hasError)
{
// エラー
Debug.LogError("Error.");
}
else
{
Debug.Log("Save Start SS2");
var customSampler = CustomSampler.Create("# Save NativeArray");
customSampler.Begin();
NativeArray<Color32> data = request.GetData<Color32>();
NativeArray<byte> bytes = ImageConversion.EncodeNativeArrayToPNG(
data,
GraphicsFormat.R8G8B8A8_SRGB,
(uint)rt.width,
(uint)rt.height);
NativeFile.WriteAllBytes($"SS2_NativeArray.png", bytes);
customSampler.End();
Debug.Log("Save End SS2");
}
});
}
/// <summary>
/// 指定したカメラが移しているもののキャプチャの開始
/// </summary>
/// <param name="camera">キャプチャしたい描画対象を写しているカメラ</param>
public void CaptureStart(Camera camera)
{
_targetCamera = camera;
CameraCaptureBridge.AddCaptureAction(_targetCamera, Capture);
}
/// <summary>
/// キャプチャ終了
/// </summary>
public void CaptureEnd()
{
CameraCaptureBridge.RemoveCaptureAction(_targetCamera, Capture);
}
/// <summary>
/// キャプチャ処理
/// </summary>
private void Capture(RenderTargetIdentifier renderTargetIdentifier, CommandBuffer commandBuffer)
{
commandBuffer.Blit(renderTargetIdentifier, _renderTexture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment