123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281 |
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.IO;
- using AOT;
- using OpenCVForUnity.CoreModule;
- namespace OpenCVForUnity.UnityUtils
- {
- public static class Utils
- {
-
- public static string getVersion ()
- {
- return "2.3.3";
- }
-
- public static void copyFromMat (Mat mat, IntPtr intPtr)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (intPtr == IntPtr.Zero)
- throw new ArgumentNullException ("intPtr == IntPtr.Zero");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- OpenCVForUnity_MatDataToByteArray (mat.nativeObj, intPtr);
- #else
- return;
- #endif
- }
-
- public static void copyToMat (IntPtr intPtr, Mat mat)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (intPtr == IntPtr.Zero)
- throw new ArgumentNullException ("intPtr == IntPtr.Zero");
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- OpenCVForUnity_ByteArrayToMatData (intPtr, mat.nativeObj);
- #else
- return;
- #endif
- }
-
- public static void copyFromMat<T> (Mat mat, IList<T> array)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (array == null)
- throw new ArgumentNullException ("array == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- GCHandle arrayHandle = GCHandle.Alloc (array, GCHandleType.Pinned);
- OpenCVForUnity_MatDataToByteArray (mat.nativeObj, arrayHandle.AddrOfPinnedObject ());
- arrayHandle.Free ();
- #else
- return;
- #endif
- }
-
- public static void copyToMat<T> (IList<T> array, Mat mat)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (array == null)
- throw new ArgumentNullException ("array == null");
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- GCHandle arrayHandle = GCHandle.Alloc (array, GCHandleType.Pinned);
-
- OpenCVForUnity_ByteArrayToMatData (arrayHandle.AddrOfPinnedObject (), mat.nativeObj);
- arrayHandle.Free ();
- #else
- return;
- #endif
- }
-
- public static void matToTexture2D (Mat mat, Texture2D texture2D, bool flip = true, int flipCode = 0, bool flipAfter = false, bool updateMipmaps = false, bool makeNoLongerReadable = false)
- {
- matToTexture2D (mat, texture2D, null, flip, flipCode, flipAfter, updateMipmaps, makeNoLongerReadable);
- }
-
- public static void matToTexture2D (Mat mat, Texture2D texture2D, Color32[] bufferColors, bool flip = true, int flipCode = 0, bool flipAfter = false, bool updateMipmaps = false, bool makeNoLongerReadable = false)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (texture2D == null)
- throw new ArgumentNullException ("texture2D == null");
- if (mat.cols () != texture2D.width || mat.rows () != texture2D.height)
- throw new ArgumentException ("The Texture2D object has to be of the same size");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- #if UNITY_5_3_OR_NEWER
- int type = mat.type ();
- if (mat.isContinuous () && (texture2D.format == TextureFormat.RGBA32 && type == CvType.CV_8UC4) || (texture2D.format == TextureFormat.RGB24 && type == CvType.CV_8UC3) || (texture2D.format == TextureFormat.Alpha8 && type == CvType.CV_8UC1)) {
- if (flip) {
- Core.flip (mat, mat, flipCode);
- }
- texture2D.LoadRawTextureData ((IntPtr)mat.dataAddr (), (int)mat.total () * (int)mat.elemSize ());
- texture2D.Apply (updateMipmaps, makeNoLongerReadable);
- if (flipAfter) {
- Core.flip (mat, mat, flipCode);
- }
- return;
- }
- #endif
- GCHandle colorsHandle;
- if (bufferColors == null) {
- Color32[] colors = texture2D.GetPixels32 ();
-
- colorsHandle = GCHandle.Alloc (colors, GCHandleType.Pinned);
- OpenCVForUnity_MatToTexture (mat.nativeObj, colorsHandle.AddrOfPinnedObject (), flip, flipCode, flipAfter);
-
- texture2D.SetPixels32 (colors);
- } else {
- colorsHandle = GCHandle.Alloc (bufferColors, GCHandleType.Pinned);
- OpenCVForUnity_MatToTexture (mat.nativeObj, colorsHandle.AddrOfPinnedObject (), flip, flipCode, flipAfter);
-
- texture2D.SetPixels32 (bufferColors);
- }
- texture2D.Apply (updateMipmaps, makeNoLongerReadable);
-
- colorsHandle.Free ();
- #else
- return;
- #endif
- }
-
- public static void fastMatToTexture2D (Mat mat, Texture2D texture2D, bool flip = true, int flipCode = 0, bool flipAfter = false, bool updateMipmaps = false, bool makeNoLongerReadable = false)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (texture2D == null)
- throw new ArgumentNullException ("texture2D == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- #if UNITY_5_3_OR_NEWER
- if (!mat.isContinuous ()) {
- throw new ArgumentException ("mat.isContinuous() must be true.");
- }
- if (flip) {
- Core.flip (mat, mat, flipCode);
- }
- texture2D.LoadRawTextureData ((IntPtr)mat.dataAddr (), (int)mat.total () * (int)mat.elemSize ());
- texture2D.Apply (updateMipmaps, makeNoLongerReadable);
- if (flipAfter) {
- Core.flip (mat, mat, flipCode);
- }
- return;
- #else
- return;
- #endif
- #else
- return;
- #endif
- }
-
- public static void texture2DToMat (Texture2D texture2D, Mat mat, bool flip = true, int flipCode = 0)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (texture2D == null)
- throw new ArgumentNullException ("texture2D == null");
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (mat.cols () != texture2D.width || mat.rows () != texture2D.height)
- throw new ArgumentException ("The Mat object has to be of the same size");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- #if (UNITY_5 && !UNITY_5_0) || UNITY_5_3_OR_NEWER
- int type = mat.type ();
- if ((texture2D.format == TextureFormat.RGBA32 && type == CvType.CV_8UC4) || (texture2D.format == TextureFormat.RGB24 && type == CvType.CV_8UC3) || (texture2D.format == TextureFormat.Alpha8 && type == CvType.CV_8UC1)) {
- mat.put (0, 0, texture2D.GetRawTextureData ());
- if (flip) {
- Core.flip (mat, mat, flipCode);
- }
- return;
- }
- #endif
-
- Color32[] colors = texture2D.GetPixels32 ();
-
- GCHandle colorsHandle = GCHandle.Alloc (colors, GCHandleType.Pinned);
-
- OpenCVForUnity_TextureToMat (colorsHandle.AddrOfPinnedObject (), mat.nativeObj, flip, flipCode);
-
- colorsHandle.Free ();
-
- #else
- return;
- #endif
- }
-
- public static void fastTexture2DToMat (Texture2D texture2D, Mat mat, bool flip = true, int flipCode = 0)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (texture2D == null)
- throw new ArgumentNullException ("texture2D == null");
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- #if (UNITY_5 && !UNITY_5_0) || UNITY_5_3_OR_NEWER
- Utils.copyToMat (texture2D.GetRawTextureData (), mat);
- if (flip) {
- Core.flip (mat, mat, flipCode);
- }
- return;
- #else
- return;
- #endif
- #else
- return;
- #endif
- }
-
- public static void webCamTextureToMat (WebCamTexture webCamTexture, Mat mat, bool flip = true, int flipCode = 0)
- {
- webCamTextureToMat (webCamTexture, mat, null, flip, flipCode);
- }
-
- public static void webCamTextureToMat (WebCamTexture webCamTexture, Mat mat, Color32[] bufferColors, bool flip = true, int flipCode = 0)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (webCamTexture == null)
- throw new ArgumentNullException ("webCamTexture == null");
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (mat.cols () != webCamTexture.width || mat.rows () != webCamTexture.height)
- throw new ArgumentException ("The Mat object has to be of the same size");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if (UNITY_IOS && !UNITY_EDITOR && (UNITY_4_6_3 || UNITY_5_0_0 || UNITY_5_0_1))
- if (mat.type () == CvType.CV_8UC4) {
- OpenCVForUnity_LowLevelTextureToMat (webCamTexture.GetNativeTexturePtr (), webCamTexture.width, webCamTexture.height, mat.nativeObj);
- if (Utils.getLowLevelGraphicsDeviceType() == 16 && Utils.getLowLevelTextureFormat (webCamTexture) == 80) {
- Imgproc.cvtColor (mat, mat, Imgproc.COLOR_BGRA2RGBA);
- }
- Core.flip (mat, mat, 0);
- return;
- }
- #endif
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- GCHandle colorsHandle;
- if (bufferColors == null) {
- Color32[] colors = webCamTexture.GetPixels32 ();
-
- colorsHandle = GCHandle.Alloc (colors, GCHandleType.Pinned);
- } else {
- webCamTexture.GetPixels32 (bufferColors);
-
- colorsHandle = GCHandle.Alloc (bufferColors, GCHandleType.Pinned);
- }
-
- OpenCVForUnity_TextureToMat (colorsHandle.AddrOfPinnedObject (), mat.nativeObj, flip, flipCode);
-
- colorsHandle.Free ();
-
- #else
- return;
- #endif
- }
-
- public static void textureToTexture2D (Texture texture, Texture2D texture2D)
- {
- if (texture == null)
- throw new ArgumentNullException ("texture == null");
- if (texture2D == null)
- throw new ArgumentNullException ("texture2D == null");
- if (texture.width != texture2D.width || texture.height != texture2D.height)
- throw new ArgumentException ("texture and texture2D need to be the same size.");
- RenderTexture prevRT = RenderTexture.active;
- if (texture is RenderTexture) {
- RenderTexture.active = (RenderTexture)texture;
- texture2D.ReadPixels (new UnityEngine.Rect (0f, 0f, texture.width, texture.height), 0, 0, false);
- texture2D.Apply (false, false);
- } else {
- RenderTexture tempRT = RenderTexture.GetTemporary (texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
- Graphics.Blit (texture, tempRT);
- RenderTexture.active = tempRT;
- texture2D.ReadPixels (new UnityEngine.Rect (0f, 0f, texture.width, texture.height), 0, 0, false);
- texture2D.Apply (false, false);
- RenderTexture.ReleaseTemporary (tempRT);
- }
- RenderTexture.active = prevRT;
- }
-
- public static void registerWebGLPlugin ()
- {
- #if UNITY_WEBGL && !UNITY_EDITOR
- if(isWebGLPluginRegistered)return;
- OpenCVForUnity_RegisterPlugin();
- isWebGLPluginRegistered = true;
- #endif
- }
-
- public static void intPtrToTextureInRenderThread (IntPtr intPtr, Texture texture)
- {
- if (intPtr == IntPtr.Zero)
- throw new ArgumentNullException ("intPtr == IntPtr.Zero");
- if (texture == null)
- throw new ArgumentNullException ("texture == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- OpenCVForUnity_SetByteArrayFromUnity (intPtr, 0, 0, 0);
- OpenCVForUnity_SetTextureFromUnity (texture.GetNativeTexturePtr (), texture.width, texture.height, 4);
- GL.IssuePluginEvent (OpenCVForUnity_GetRenderEventFunc (), 1);
- #else
- return;
- #endif
- }
-
- public static void arrayToTextureInRenderThread<T> (IList<T> array, Texture texture)
- {
- if (array == null)
- throw new ArgumentNullException ("array == null");
- if (texture == null)
- throw new ArgumentNullException ("texture == null");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- GCHandle arrayHandle = GCHandle.Alloc (array, GCHandleType.Pinned);
- OpenCVForUnity_SetByteArrayFromUnity (arrayHandle.AddrOfPinnedObject (), 0, 0, 0);
- OpenCVForUnity_SetTextureFromUnity (texture.GetNativeTexturePtr (), texture.width, texture.height, 4);
- GL.IssuePluginEvent (OpenCVForUnity_GetRenderEventFunc (), 1);
- arrayHandle.Free ();
- #else
- return;
- #endif
- }
-
- public static void matToTextureInRenderThread (Mat mat, Texture texture)
- {
- if (mat != null)
- mat.ThrowIfDisposed ();
- if (mat == null)
- throw new ArgumentNullException ("mat == null");
- if (texture == null)
- throw new ArgumentNullException ("texture == null");
- if (mat.cols () != texture.width || mat.rows () != texture.height)
- throw new ArgumentException ("The Texture object has to be of the same size");
- #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
- if (!mat.isContinuous ()) {
- throw new ArgumentException ("mat.isContinuous() must be true.");
- }
- OpenCVForUnity_SetByteArrayFromUnity ((IntPtr)mat.dataAddr (), mat.width (), mat.height (), (int)mat.elemSize ());
- OpenCVForUnity_SetTextureFromUnity (texture.GetNativeTexturePtr (), texture.width, texture.height, 4);
- GL.IssuePluginEvent (OpenCVForUnity_GetRenderEventFunc (), 1);
- #else
- return;
- #endif
- }
-
- public static string getFilePath (string filepath, bool refresh = false, int timeout = 0)
- {
- #if UNITY_ANDROID && !UNITY_EDITOR
- filepath = filepath.TrimStart (chTrims);
- string srcPath = Path.Combine (Application.streamingAssetsPath, filepath);
- string destPath = Path.Combine (Application.persistentDataPath, "opencvforunity");
- destPath = Path.Combine (destPath, filepath);
- if (!refresh && File.Exists (destPath))
- return destPath;
- #if UNITY_2017_1_OR_NEWER
- using (UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.Get (srcPath)) {
- request.timeout = timeout;
- #if UNITY_2018_2_OR_NEWER
- request.SendWebRequest ();
- #else
- request.Send ();
- #endif
- while (!request.isDone) {;}
- #if UNITY_2017_1_OR_NEWER
- if (request.isHttpError || request.isNetworkError) {
- #else
- if (request.isError) {
- #endif
- Debug.LogWarning (request.error);
- Debug.LogWarning (request.responseCode);
- return String.Empty;
- }
-
- String dirPath = Path.GetDirectoryName (destPath);
- if (!Directory.Exists (dirPath))
- Directory.CreateDirectory (dirPath);
- File.WriteAllBytes (destPath, request.downloadHandler.data);
- }
- #else
- using (WWW request = new WWW (srcPath)) {
- while (!request.isDone) {;}
- if (!string.IsNullOrEmpty(request.error)) {
- Debug.LogWarning (request.error);
- return String.Empty;
- }
-
- String dirPath = Path.GetDirectoryName (destPath);
- if (!Directory.Exists (dirPath))
- Directory.CreateDirectory (dirPath);
- File.WriteAllBytes (destPath, request.bytes);
- }
- #endif
- return destPath;
- #elif UNITY_WEBGL && !UNITY_EDITOR
- filepath = filepath.TrimStart (chTrims);
- string destPath = Path.Combine(Path.AltDirectorySeparatorChar.ToString(), "opencvforunity");
- destPath = Path.Combine(destPath, filepath);
- if (File.Exists(destPath)){
- return destPath;
- }else{
- return String.Empty;
- }
- #else
- filepath = filepath.TrimStart (chTrims);
- string destPath = Path.Combine (Application.streamingAssetsPath, filepath);
- if (File.Exists (destPath)) {
- return destPath;
- } else {
- return String.Empty;
- }
- #endif
- }
-
- public static string[] getMultipleFilePaths (string[] filepaths, bool refresh = false, int timeout = 0)
- {
- string[] result = new string[filepaths.Length];
- for (int i = 0; i < filepaths.Length; i++) {
- result [i] = getFilePath (filepaths [i], refresh, timeout);
- }
- return result;
- }
-
- public static IEnumerator getFilePathAsync (string filepath, Action<string> completed, bool refresh = false, int timeout = 0)
- {
- return getFilePathAsync (filepath, completed, null, null, refresh, timeout);
- }
-
- public static IEnumerator getFilePathAsync (string filepath, Action<string> completed, Action<string, float> progressChanged, bool refresh = false, int timeout = 0)
- {
- return getFilePathAsync (filepath, completed, progressChanged, null, refresh, timeout);
- }
-
-
- public static IEnumerator getFilePathAsync (string filepath, Action<string> completed, Action<string, float> progressChanged, Action<string, string, long> errorOccurred, bool refresh = false, int timeout = 0)
- {
- #if (UNITY_ANDROID || UNITY_WEBGL) && !UNITY_EDITOR
- filepath = filepath.TrimStart (chTrims);
- string srcPath = Path.Combine(Application.streamingAssetsPath, filepath);
- #if UNITY_ANDROID
- string destPath = Path.Combine(Application.persistentDataPath, "opencvforunity");
- #else
- string destPath = Path.Combine(Path.AltDirectorySeparatorChar.ToString(), "opencvforunity");
- #endif
- destPath = Path.Combine(destPath, filepath);
- if (!refresh && File.Exists(destPath)){
- if (progressChanged != null)
- progressChanged (filepath, 0);
- yield return null;
- if (progressChanged != null)
- progressChanged (filepath, 1);
- if (completed != null)
- completed (destPath);
- } else {
- #if UNITY_WEBGL || (UNITY_ANDROID && UNITY_2017_1_OR_NEWER)
- using (UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.Get (srcPath)) {
- request.timeout = timeout;
- #if UNITY_2018_2_OR_NEWER
- request.SendWebRequest ();
- #else
- request.Send ();
- #endif
- while (!request.isDone) {
- if (progressChanged != null)
- progressChanged (filepath, request.downloadProgress);
- yield return null;
- }
- if (progressChanged != null)
- progressChanged (filepath, request.downloadProgress);
- #if UNITY_2017_1_OR_NEWER
- if (request.isHttpError || request.isNetworkError) {
- #else
- if (request.isError) {
- #endif
- Debug.LogWarning (request.error);
- Debug.LogWarning (request.responseCode);
- if (errorOccurred != null) {
- errorOccurred (filepath, request.error, request.responseCode);
- } else {
- if (completed != null)
- completed (String.Empty);
- }
- yield break;
- }
-
- String dirPath = Path.GetDirectoryName (destPath);
- if (!Directory.Exists (dirPath))
- Directory.CreateDirectory (dirPath);
- File.WriteAllBytes (destPath, request.downloadHandler.data);
- }
- #else
- using (WWW request = new WWW (srcPath)) {
- while (!request.isDone) {
- if (progressChanged != null)
- progressChanged(filepath, request.progress);
- yield return null;
- }
- if (progressChanged != null)
- progressChanged (filepath, request.progress);
- if (!string.IsNullOrEmpty(request.error)) {
- Debug.LogWarning (request.error);
- if (errorOccurred != null) {
- errorOccurred (filepath, request.error, 0);
- } else {
- if (completed != null)
- completed (String.Empty);
- }
- yield break;
- }
-
- String dirPath = Path.GetDirectoryName (destPath);
- if (!Directory.Exists (dirPath))
- Directory.CreateDirectory (dirPath);
- File.WriteAllBytes(destPath, request.bytes);
- }
- #endif
- if (completed != null)
- completed (destPath);
- }
- #else
- filepath = filepath.TrimStart (chTrims);
- string destPath = Path.Combine (Application.streamingAssetsPath, filepath);
- if (progressChanged != null)
- progressChanged (filepath, 0);
- yield return null;
- if (progressChanged != null)
- progressChanged (filepath, 1);
-
- if (File.Exists (destPath)) {
- if (completed != null)
- completed (destPath);
- } else {
- if (errorOccurred != null) {
- errorOccurred (filepath, "Unknown Error", 0);
- } else {
- if (completed != null)
- completed (String.Empty);
- }
- }
- #endif
- yield break;
- }
-
- public static IEnumerator getMultipleFilePathsAsync (string[] filepaths, Action<string[]> allCompleted, bool refresh = false, int timeout = 0)
- {
- return getMultipleFilePathsAsync (filepaths, allCompleted, null, null, refresh, timeout);
- }
-
- public static IEnumerator getMultipleFilePathsAsync (string[] filepaths, Action<string[]> allCompleted, Action<string, float> progressChanged, bool refresh = false, int timeout = 0)
- {
- return getMultipleFilePathsAsync (filepaths, allCompleted, progressChanged, null, refresh, timeout);
- }
-
- public static IEnumerator getMultipleFilePathsAsync (string[] filepaths, Action<string[]> allCompleted, Action<string, float> progressChanged, Action<string, string, long> errorOccurred, bool refresh = false, int timeout = 0)
- {
- string[] readableFilePaths = new string[filepaths.Length];
- for (int i = 0; i < filepaths.Length; i++) {
- yield return getFilePathAsync (filepaths [i], (result) => {
- readableFilePaths [i] = result;
- }, progressChanged, errorOccurred, refresh, timeout);
- }
- if (allCompleted != null)
- allCompleted (readableFilePaths);
- }
- private static char[] chTrims = {
- '.',
- #if UNITY_WINRT_8_1 && !UNITY_EDITOR
- '/',
- '\\'
- #else
- System.IO.Path.DirectorySeparatorChar,
- System.IO.Path.AltDirectorySeparatorChar
- #endif
- };
-
-
-
- #pragma warning disable 0414
- private static bool throwOpenCVException = false;
- #pragma warning restore 0414
-
- public static void setDebugMode (bool debugMode, bool throwException = false)
- {
- #if (UNITY_PRO_LICENSE || UNITY_5 || UNITY_5_3_OR_NEWER)
- OpenCVForUnity_SetDebugMode (debugMode);
- if (debugMode) {
- OpenCVForUnity_SetDebugLogFunc (debugLogFunc);
- } else {
- OpenCVForUnity_SetDebugLogFunc (null);
- }
-
- throwOpenCVException = throwException;
-
- #endif
- }
- #if (UNITY_PRO_LICENSE || UNITY_5 || UNITY_5_3_OR_NEWER)
- private delegate void DebugLogDelegate (string str);
- [MonoPInvokeCallback (typeof(DebugLogDelegate))]
- private static void debugLogFunc (string str)
- {
- if (throwOpenCVException) {
- throw new CvException (str);
- } else {
- Debug.LogError (str);
- }
- }
- #endif
- internal static int URShift (int number, int bits)
- {
- if (number >= 0)
- return number >> bits;
- else
- return (number >> bits) + (2 << ~bits);
- }
- internal static long URShift (long number, int bits)
- {
- if (number >= 0)
- return number >> bits;
- else
- return (number >> bits) + (2 << ~bits);
- }
- internal static int HashContents<T> (this IEnumerable<T> enumerable)
- {
- int hash = 0x218A9B2C;
- foreach (var item in enumerable) {
- int thisHash = item.GetHashCode ();
-
- hash = thisHash ^ ((hash << 5) + hash);
- }
- return hash;
- }
- #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
- const string LIBNAME = "__Internal";
-
- #else
- const string LIBNAME = "opencvforunity";
- #endif
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_SetDebugMode (bool flag);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_SetDebugLogFunc (DebugLogDelegate func);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_DebugLogTest ();
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_MatToTexture (IntPtr mat, IntPtr textureColors, bool flip, int flipCode, bool flipAfter);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_TextureToMat (IntPtr textureColors, IntPtr Mat, bool flip, int flipCode);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_MatDataToByteArray (IntPtr mat, IntPtr byteArray);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_ByteArrayToMatData (IntPtr byteArray, IntPtr Mat);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_SetTextureFromUnity (System.IntPtr texture, int width, int height, int bytesPerPixel);
- [DllImport (LIBNAME)]
- private static extern void OpenCVForUnity_SetByteArrayFromUnity (System.IntPtr byteArray, int width, int height, int bytesPerPixel);
- [DllImport (LIBNAME)]
- private static extern IntPtr OpenCVForUnity_GetRenderEventFunc ();
- #if UNITY_WEBGL && !UNITY_EDITOR
- private static bool isWebGLPluginRegistered = false;
- [DllImport(LIBNAME)]
- private static extern void OpenCVForUnity_RegisterPlugin();
- #endif
- }
- }
|