-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenshotController.cs
49 lines (41 loc) · 1.4 KB
/
ScreenshotController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cuboid
{
public class ScreenshotController : MonoBehaviour
{
private static ScreenshotController _instance;
public static ScreenshotController Instance => _instance;
[SerializeField] private Camera _screenshotCamera;
private RenderTexture _renderTexture;
private int _width = 256;
private int _height = 256;
private void Awake()
{
// Singleton implementation
if (_instance != null && _instance != this) { Destroy(this); } else { _instance = this; }
}
private void Start()
{
_width = UnityPlugin.Constants.k_ThumbnailSize;
_height = _width;
_renderTexture = new RenderTexture(_width, _height, 16);
}
public byte[] CaptureScreenshot()
{
_screenshotCamera.targetTexture = _renderTexture;
RenderTexture.active = _renderTexture;
_screenshotCamera.Render();
Texture2D renderedTexture = new Texture2D(_width, _height);
renderedTexture.ReadPixels(new Rect(0, 0, _width, _height), 0, 0);
RenderTexture.active = null;
return renderedTexture.EncodeToPNG();
}
}
}