Tanoda
EasyWebCam.cs
Go to the documentation of this file.
1using TBEasyWebCam.CallBack;
2using TBEasyWebCam.Setting;
3using UnityEngine;
4
5namespace TBEasyWebCam
6{
7 public class EasyWebCam : MonoBehaviour
8 {
9 public static IEasyWebCam easyWebCamInterface;
10
11 public ResolutionMode mCamResolution = ResolutionMode.MediumResolution;
12 public static FocusMode mFocusMode = FocusMode.AutoFocus;
13 public static bool isActive = false;
14
15 private float time;
16 private int count;
17
18 public static string CAMERA_PERMISSION = "android.permission.CAMERA";
19
20 public static Texture2D WebCamPreview
21 {
22 get
23 {
24 if (easyWebCamInterface != null)
25 return easyWebCamInterface.WebCamPreview;
26 return null;
27 }
28 }
29
30 static EasyWebCam()
31 {
32 // Debug.Log("wo cha nimade enter le easywebcamX .....");
33 }
34
35 void Awake()
36 {
37#if UNITY_EDITOR
38
39#elif UNITY_ANDROID
40 isActive = true;
41 easyWebCamInterface = new EasyWebCamAndroid ();
42
43#elif UNITY_IOS
44 isActive = true;
45 easyWebCamInterface = new EasyWebCamiOS ();
46
47#endif
48
50 }
51
52 void Start()
53 {
54 OnPreviewStart += PreviewStart;
55 OnPreviewUpdate += CameraUpdate;
56 RenderListenerUtility.onPause += OnPause;
57 RenderListenerUtility.onQuit += OnRelease;
58 }
59
60 void PreviewStart()
61 {
63 }
64
65 void CameraUpdate()
66 {
67 }
68
69 void Update()
70 {
71 if (easyWebCamInterface != null && EasyWebCamBase.isRunning)
72 {
73 easyWebCamInterface.UpdateImage();
74 if (Input.GetMouseButtonDown(0))
75 if (isDoubleClick())
76 {
77 Debug.Log("current double clicked is enter !");
78 setFocusMode(FocusMode.TapToFocus);
79 }
80 }
81 }
82
83 public static event EasyWebCamStartedDelegate OnPreviewStart
84 {
85 add
86 {
87 if (easyWebCamInterface != null) EasyWebCamBase.onEasyWebCamStart += value;
88 }
89 remove
90 {
91 if (easyWebCamInterface != null) EasyWebCamBase.onEasyWebCamStart -= value;
92 }
93 }
94
95 public static event EasyWebCamUpdateDelegate OnPreviewUpdate
96 {
97 add
98 {
99 if (easyWebCamInterface != null) EasyWebCamBase.OnEasyWebCamUpdate += value;
100 }
101 remove
102 {
103 if (easyWebCamInterface != null) EasyWebCamBase.OnEasyWebCamUpdate -= value;
104 }
105 }
106
107 public static event EasyWebCamStopedDelegate OnPreviewStoped
108 {
109 add
110 {
111 if (easyWebCamInterface != null) EasyWebCamBase.OnEasyWebCamStoped += value;
112 }
113 remove
114 {
115 if (easyWebCamInterface != null) EasyWebCamBase.OnEasyWebCamStoped -= value;
116 }
117 }
118
122 public static void Play()
123 {
124#if UNITY_EDITOR
125
126#elif UNITY_ANDROID|| UNITY_IOS
127 if (easyWebCamInterface != null) {
128 easyWebCamInterface.Play ();
129 }
130 #endif
131 }
132
136 public static void Stop()
137 {
138#if UNITY_EDITOR
139
140#elif UNITY_ANDROID|| UNITY_IOS
141 if (easyWebCamInterface != null) {
142 easyWebCamInterface.Stop ();
143 }
144 #endif
145 }
146
152 public static void setPreviewResolution(ResolutionMode resolutionMode)
153 {
154 if (easyWebCamInterface != null)
155 {
156 var preWidth = 0;
157 var preHeight = 0;
158 resolutionMode.Dimensions(out preWidth, out preHeight);
159 easyWebCamInterface.setPreviewResolution(preWidth, preHeight);
160 }
161 }
162
166 public static void TakePhoto()
167 {
168 if (easyWebCamInterface != null) easyWebCamInterface.TakePhoto();
169 }
170
175 public static void setFocusMode(FocusMode paramode)
176 {
177 if (!isPlaying()) return;
178 if (easyWebCamInterface != null) easyWebCamInterface.setFocusMode((int) paramode);
179 mFocusMode = paramode;
180 }
181
182 public static void tapFocus()
183 {
184 if (!isPlaying()) return;
185 if (easyWebCamInterface != null) easyWebCamInterface.tapFocus();
186 }
187
192 public static void setFlashMode(FlashMode paramode)
193 {
194 if (!isPlaying()) return;
195 if (easyWebCamInterface != null) easyWebCamInterface.setFocusMode((int) paramode);
196 }
197
202 public static void setTorchMode(TorchMode paramode)
203 {
204 if (!isPlaying()) return;
205 if (easyWebCamInterface != null) easyWebCamInterface.setTorchMode((int) paramode);
206 }
207
208 public static void SwitchCamera(int mode)
209 {
210 if (easyWebCamInterface != null) easyWebCamInterface.SwitchCamera(mode);
211 }
212
213 public static int Width()
214 {
215 if (!isPlaying()) return 0;
216 if (easyWebCamInterface != null) return easyWebCamInterface.previewWidth;
217 return 0;
218 }
219
220 public static int Height()
221 {
222 if (!isPlaying()) return 0;
223 if (easyWebCamInterface != null) return easyWebCamInterface.previewHeight;
224 return 0;
225 }
226
227 public static int getFrame()
228 {
229 if (easyWebCamInterface != null)
230 return easyWebCamInterface.getEnterFrame();
231 return -1;
232 }
233
234 private void OnPause(bool isPaused)
235 {
236 if (easyWebCamInterface != null) easyWebCamInterface.OnPause(isPaused);
237 }
238
239 private void OnRelease()
240 {
241 if (easyWebCamInterface != null) easyWebCamInterface.Release();
242 }
243
244 public static void Release()
245 {
246 if (easyWebCamInterface != null) easyWebCamInterface.Release();
247 }
248
249 public static bool isPlaying()
250 {
251 return EasyWebCamBase.isRunning;
252 }
253
254 bool isDoubleClick()
255 {
256 count++;
257 if (count == 1) time = Time.time;
258 if (2 == count && Time.time - time <= 0.5f)
259 {
260 count = 0;
261 return true;
262 }
263
264 if (Time.time - time > 0.5f) count = 0;
265 return false;
266 }
267
268 public static bool checkPermissions()
269 {
270 if (Application.platform != RuntimePlatform.Android) return true;
272 }
273 }
274}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static bool IsPermissionGranted(string permissionName)
static void TakePhoto()
Takes the photo.
Definition: EasyWebCam.cs:166
static string CAMERA_PERMISSION
Definition: EasyWebCam.cs:18
static void Release()
Definition: EasyWebCam.cs:244
static EasyWebCamUpdateDelegate OnPreviewUpdate
Definition: EasyWebCam.cs:96
static bool isActive
Definition: EasyWebCam.cs:13
static void setTorchMode(TorchMode paramode)
Sets the torch mode.
Definition: EasyWebCam.cs:202
static void Play()
Play this instance.
Definition: EasyWebCam.cs:122
ResolutionMode mCamResolution
Definition: EasyWebCam.cs:11
static void tapFocus()
Definition: EasyWebCam.cs:182
static void setFlashMode(FlashMode paramode)
Sets the flash mode.
Definition: EasyWebCam.cs:192
static FocusMode mFocusMode
Definition: EasyWebCam.cs:12
static void setFocusMode(FocusMode paramode)
Sets the focus mode.
Definition: EasyWebCam.cs:175
static IEasyWebCam easyWebCamInterface
Definition: EasyWebCam.cs:9
static EasyWebCamStopedDelegate OnPreviewStoped
Definition: EasyWebCam.cs:108
static bool checkPermissions()
Definition: EasyWebCam.cs:268
static EasyWebCamStartedDelegate OnPreviewStart
Definition: EasyWebCam.cs:84
static void Stop()
Stop this instance.
Definition: EasyWebCam.cs:136
static void setPreviewResolution(ResolutionMode resolutionMode)
Sets the preview resolution.
Definition: EasyWebCam.cs:152
static bool isPlaying()
Definition: EasyWebCam.cs:249
static int getFrame()
Definition: EasyWebCam.cs:227
static void SwitchCamera(int mode)
Definition: EasyWebCam.cs:208
static Texture2D WebCamPreview
Definition: EasyWebCam.cs:21