This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshotCamera.cs
61 lines (53 loc) · 1.95 KB
/
SnapshotCamera.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
50
51
52
53
54
55
56
57
58
59
60
61
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SnapshotCamera
{
private int textureWidth;
private int textureHeight;
private Transform defaultPosition;
private Transform anchor;
public Camera camera;
public bool isActive
{
get { return this.camera.gameObject.activeInHierarchy; }
}
public SnapshotCamera(Camera mainCamera, Transform anchor, int textureWidth, int textureHeight)
{
this.anchor = anchor;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
if (GameObject.Find("Snapshot Camera") == null)
{
this.defaultPosition = mainCamera.transform;
this.camera = Camera.Instantiate(mainCamera);
this.camera.name = "Snapshot Camera";
this.camera.targetTexture = new RenderTexture(this.textureWidth, this.textureHeight, 24);
this.SetActive(false);
}
}
public void RandomizePosition()
{
float offsetX = Random.Range(-0.25f, 0.25f);
float offsetY = Random.Range(-0.25f, 0.25f);
this.camera.transform.position = this.defaultPosition.position + new Vector3(offsetX, offsetY, 0.0f);
this.camera.transform.LookAt(this.anchor);
}
public void SetActive(bool value)
{
this.camera.gameObject.SetActive(value);
}
public byte[] Capture()
{
Texture2D snapshot = new Texture2D(this.textureWidth, this.textureHeight, TextureFormat.RGB24, false);
snapshot.hideFlags = HideFlags.HideAndDontSave;
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = this.camera.targetTexture;
this.camera.Render();
snapshot.ReadPixels(new Rect(0, 0, this.textureWidth, this.textureHeight), 0, 0);
byte[] bytes = snapshot.EncodeToPNG();
RenderTexture.active = currentActiveRT;
Object.Destroy(snapshot);
return bytes;
}
}