forked from chamuth/unity-webcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileCam.cs
57 lines (43 loc) · 1.43 KB
/
MobileCam.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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MobileCam : MonoBehaviour {
private bool camAvailable;
private WebCamTexture cameraTexture;
private Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
public bool frontFacing;
// Use this for initialization
void Start () {
defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
return;
for (int i = 0; i < devices.Length; i++)
{
var curr = devices[i];
if (curr.isFrontFacing == frontFacing)
{
cameraTexture = new WebCamTexture(curr.name, Screen.width, Screen.height);
break;
}
}
if (cameraTexture == null)
return;
cameraTexture.Play (); // Start the camera
background.texture = cameraTexture; // Set the texture
camAvailable = true; // Set the camAvailable for future purposes.
}
// Update is called once per frame
void Update () {
if (!camAvailable)
return;
float ratio = (float)cameraTexture.width / (float)cameraTexture.height;
fit.aspectRatio = ratio; // Set the aspect ratio
float scaleY = cameraTexture.videoVerticallyMirrored ? -1f : 1f; // Find if the camera is mirrored or not
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f); // Swap the mirrored camera
int orient = -cameraTexture.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0,0, orient);
}
}