Tanoda
DeviceCameraController.cs
Go to the documentation of this file.
1
4
5using System.Collections;
6using TBEasyWebCam;
7using UnityEngine;
8
9public class DeviceCameraController : MonoBehaviour
10{
11 public DeviceCamera dWebCam { get; private set; }
12
13 GameObject e_CameraPlaneObj;
14 bool isCorrected;
15 float screenVideoRatio = 1.0f;
16 public bool isUseEasyWebCam = true;
17
18 public bool isPlaying
19 {
20 get
21 {
22 if (dWebCam != null)
23 return dWebCam.isPlaying();
24 return false;
25 }
26 }
27
28 void Start()
29 {
31 e_CameraPlaneObj = transform.Find("CameraPlane").gameObject;
32 //StartWork ();
33 }
34
35 // Update is called once per frame
36 void Update()
37 {
38 if (dWebCam != null && dWebCam.isPlaying())
39 {
40 if (dWebCam.Width() > 200 && !isCorrected) correctScreenRatio();
41
42 if (e_CameraPlaneObj.activeSelf)
43 e_CameraPlaneObj.GetComponent<Renderer>().material.mainTexture = dWebCam.preview;
44 }
45 }
46
50 public void StartWork()
51 {
52#if UNITY_ANDROID
54 {
55 requestCameraPermissions();
56 return;
57 }
58#endif
59 if (dWebCam != null) dWebCam.Play();
60 }
61
66 public void StopWork()
67 {
68 if (dWebCam != null && dWebCam.isPlaying()) dWebCam.Stop();
69 if (e_CameraPlaneObj.activeSelf) e_CameraPlaneObj.GetComponent<Renderer>().material.mainTexture = null;
70 }
71
75 void correctScreenRatio()
76 {
77 var videoWidth = 640;
78 var videoHeight = 480;
79 var ScreenWidth = 640;
80 var ScreenHeight = 480;
81
82 float videoRatio = 1;
83 float screenRatio = 1;
84
85 if (dWebCam != null)
86 {
87 videoWidth = dWebCam.Width();
88 videoHeight = dWebCam.Height();
89 }
90
91 videoRatio = videoWidth * 1.0f / videoHeight;
92#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
93 ScreenWidth = Mathf.Max (Screen.width, Screen.height);
94 ScreenHeight = Mathf.Min (Screen.width, Screen.height);
95#else
96 ScreenWidth = Screen.width;
97 ScreenHeight = Screen.height;
98#endif
99 screenRatio = ScreenWidth * 1.0f / ScreenHeight;
100
101 screenVideoRatio = screenRatio / videoRatio;
102 isCorrected = true;
103
104 if (e_CameraPlaneObj != null)
105 e_CameraPlaneObj.GetComponent<CameraPlaneController>().correctPlaneScale(screenVideoRatio);
106 }
107
108 void requestCameraPermissions()
109 {
112 grantedPermission =>
113 {
114 StartCoroutine(waitTimeToOpenCamera());
115 // The permission was successfully granted, restart the change avatar routine
116 },
117 deniedPermission =>
118 {
119 // The permission was denied
120 },
121 deniedPermissionAndDontAskAgain =>
122 {
123 // The permission was denied, and the user has selected "Don't ask again"
124 // Show in-game pop-up message stating that the user can change permissions in Android Application Settings
125 // if he changes his mind (also required by Google Featuring program)
126 }));
127 }
128
129 IEnumerator waitTimeToOpenCamera()
130 {
131 yield return new WaitForEndOfFrame(); // (0.1f);
132 StartWork();
133 }
134}
static void RequestPermission(string permissionName, AndroidPermissionCallback callback)
void StartWork()
start the work.
void StopWork()
Stops the work. when you need to leave current scene ,you must call this func firstly
int Width()
get the width of the camera
int Height()
get the height of the camera
bool isPlaying()
get status of the camera
void Play()
open the camera
Definition: DeviceCamera.cs:89
Texture preview
Definition: DeviceCamera.cs:7
void Stop()
Stop this camera.
static string CAMERA_PERMISSION
Definition: EasyWebCam.cs:18
static bool checkPermissions()
Definition: EasyWebCam.cs:268