Utils_GetFilePathExample.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using UnityEngine.UI;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.VideoioModule;
  9. using OpenCVForUnity.ImgprocModule;
  10. using OpenCVForUnity.UnityUtils;
  11. namespace OpenCVForUnitySample
  12. {
  13. /// <summary>
  14. /// Utils_GetFilePath Example
  15. /// An example of how to get the readable path of a file in the "StreamingAssets" folder using the Utils class.
  16. /// </summary>
  17. public class Utils_GetFilePathExample : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// The file path dropdown.
  21. /// </summary>
  22. public Dropdown filePathDropdown;
  23. /// <summary>
  24. /// The refresh toggle.
  25. /// </summary>
  26. public Toggle refreshToggle;
  27. /// <summary>
  28. /// The timeout dropdown.
  29. /// </summary>
  30. public Dropdown timeoutDropdown;
  31. /// <summary>
  32. /// The get file path button.
  33. /// </summary>
  34. public Button getFilePathButton;
  35. /// <summary>
  36. /// The get multiple file paths button.
  37. /// </summary>
  38. public Button getMultipleFilePathsButton;
  39. /// <summary>
  40. /// The get file path async button.
  41. /// </summary>
  42. public Button getFilePathAsyncButton;
  43. /// <summary>
  44. /// The get multiple file paths async button.
  45. /// </summary>
  46. public Button getMultipleFilePathsAsyncButton;
  47. /// <summary>
  48. /// The abort button.
  49. /// </summary>
  50. public Button abortButton;
  51. /// <summary>
  52. /// The file path input field.
  53. /// </summary>
  54. // public InputField filePathInputField;
  55. public Text filePathInputField;
  56. string[] filePathPreset = new string[] {
  57. "768x576_mjpeg.mjpeg",
  58. "/lbpcascade_frontalface.xml",
  59. "calibration_images/left01.jpg",
  60. "xxx"
  61. };
  62. IEnumerator getFilePath_Coroutine;
  63. // Use this for initialization
  64. void Start ()
  65. {
  66. abortButton.interactable = false;
  67. }
  68. private void GetFilePath (string filePath, bool refresh, int timeout)
  69. {
  70. string readableFilePath = Utils.getFilePath (filePath, refresh, timeout);
  71. #if UNITY_WEBGL && !UNITY_EDITOR
  72. Debug.Log ("The Utils.getFilePath() method is not supported on WebGL platform.");
  73. filePathInputField.text = filePathInputField.text + "The Utils.getFilePath() method is not supported on WebGL platform." + "\n";
  74. if (!string.IsNullOrEmpty (readableFilePath)) {
  75. Debug.Log ("completed: " + "readableFilePath=" + readableFilePath);
  76. filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath=" + readableFilePath;
  77. }
  78. #else
  79. if (string.IsNullOrEmpty (readableFilePath)) {
  80. Debug.LogError ("completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  81. filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
  82. } else {
  83. Debug.Log ("completed: " + "readableFilePath= " + readableFilePath);
  84. filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + readableFilePath + "\n";
  85. }
  86. #endif
  87. }
  88. private void GetMultipleFilePaths (string[] filePaths, bool refresh, int timeout)
  89. {
  90. string[] readableFilePaths = Utils.getMultipleFilePaths (filePaths, refresh, timeout);
  91. #if UNITY_WEBGL && !UNITY_EDITOR
  92. Debug.Log ("The Utils.getFilePath() method is not supported on WebGL platform.");
  93. filePathInputField.text = filePathInputField.text + "The Utils.getFilePath() method is not supported on WebGL platform." + "\n";
  94. for (int i = 0; i < readableFilePaths.Length; i++) {
  95. if (!string.IsNullOrEmpty (readableFilePaths [i])) {
  96. Debug.Log ("readableFilePath[" + i + "]=" + readableFilePaths [i]);
  97. filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]=" + readableFilePaths [i];
  98. }
  99. }
  100. #else
  101. Debug.Log ("allCompleted:" + "\n");
  102. filePathInputField.text = filePathInputField.text + "allCompleted:" + "\n";
  103. for (int i = 0; i < readableFilePaths.Length; i++) {
  104. if (string.IsNullOrEmpty (readableFilePaths [i])) {
  105. Debug.LogError ("readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  106. filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
  107. } else {
  108. Debug.Log ("readableFilePath[" + i + "]= " + readableFilePaths [i]);
  109. filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + readableFilePaths [i] + "\n";
  110. }
  111. }
  112. #endif
  113. }
  114. private void GetFilePathAsync (string filePath, bool refresh, int timeout)
  115. {
  116. HideButton ();
  117. getFilePath_Coroutine = Utils.getFilePathAsync (filePath, (result) => { // completed callback
  118. getFilePath_Coroutine = null;
  119. ShowButton ();
  120. string readableFilePath = result;
  121. if (string.IsNullOrEmpty (readableFilePath)) {
  122. Debug.LogError ("completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  123. filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
  124. }
  125. Debug.Log ("completed: " + "readableFilePath= " + readableFilePath);
  126. filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + readableFilePath + "\n";
  127. }, (path, progress) => { // progressChanged callback
  128. Debug.Log ("progressChanged: " + "path= " + path + " progress= " + progress);
  129. filePathInputField.text = filePathInputField.text + "progressChanged: " + "path= " + path + " progress= " + progress + "\n";
  130. }, (path, error, responseCode) => { // errorOccurred callback
  131. getFilePath_Coroutine = null;
  132. ShowButton ();
  133. Debug.Log ("errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode);
  134. filePathInputField.text = filePathInputField.text + "errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode + "\n";
  135. }, refresh, timeout);
  136. StartCoroutine (getFilePath_Coroutine);
  137. }
  138. private void GetMultipleFilePathsAsync (string[] filePaths, bool refresh, int timeout)
  139. {
  140. HideButton ();
  141. getFilePath_Coroutine = Utils.getMultipleFilePathsAsync (filePaths, (result) => { // allCompleted callback
  142. getFilePath_Coroutine = null;
  143. ShowButton ();
  144. string[] readableFilePaths = result;
  145. Debug.Log ("allCompleted:" + "\n");
  146. filePathInputField.text = filePathInputField.text + "allCompleted:" + "\n";
  147. for (int i = 0; i < readableFilePaths.Length; i++) {
  148. if (string.IsNullOrEmpty (readableFilePaths [i])) {
  149. Debug.LogError ("readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  150. filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
  151. } else {
  152. Debug.Log ("readableFilePath[" + i + "]= " + readableFilePaths [i]);
  153. filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + readableFilePaths [i] + "\n";
  154. }
  155. }
  156. }, (path, progress) => { // progressChanged callback
  157. Debug.Log ("progressChanged: " + "path= " + path + " progress= " + progress);
  158. filePathInputField.text = filePathInputField.text + "progressChanged: " + "path= " + path + " progress= " + progress + "\n";
  159. }, (path, error, responseCode) => { // errorOccurred callback
  160. Debug.Log ("errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode);
  161. filePathInputField.text = filePathInputField.text + "errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode + "\n";
  162. }, refresh, timeout);
  163. StartCoroutine (getFilePath_Coroutine);
  164. }
  165. private void ShowButton ()
  166. {
  167. getFilePathButton.interactable = true;
  168. getMultipleFilePathsButton.interactable = true;
  169. getFilePathAsyncButton.interactable = true;
  170. getMultipleFilePathsAsyncButton.interactable = true;
  171. abortButton.interactable = false;
  172. }
  173. private void HideButton ()
  174. {
  175. getFilePathButton.interactable = false;
  176. getMultipleFilePathsButton.interactable = false;
  177. getFilePathAsyncButton.interactable = false;
  178. getMultipleFilePathsAsyncButton.interactable = false;
  179. abortButton.interactable = true;
  180. }
  181. // Update is called once per frame
  182. void Update ()
  183. {
  184. }
  185. /// <summary>
  186. /// Raises the destroy event.
  187. /// </summary>
  188. void OnDestroy ()
  189. {
  190. if (getFilePath_Coroutine != null) {
  191. StopCoroutine (getFilePath_Coroutine);
  192. ((IDisposable)getFilePath_Coroutine).Dispose ();
  193. }
  194. }
  195. /// <summary>
  196. /// Raises the back button click event.
  197. /// </summary>
  198. public void OnBackButtonClick ()
  199. {
  200. SceneManager.LoadScene ("OpenCVForUnityExample");
  201. }
  202. /// <summary>
  203. /// Raises the get file path button click event.
  204. /// </summary>
  205. public void OnGetFilePathButtonClick ()
  206. {
  207. bool refresh = refreshToggle.isOn;
  208. string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
  209. int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
  210. filePathInputField.text = "";
  211. GetFilePath (filePathPreset [filePathDropdown.value], refresh, timeout);
  212. }
  213. /// <summary>
  214. /// Raises the get multiple file paths button click event.
  215. /// </summary>
  216. public void OnGetMultipleFilePathsButtonClick ()
  217. {
  218. bool refresh = refreshToggle.isOn;
  219. string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
  220. int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
  221. filePathInputField.text = "";
  222. GetMultipleFilePaths (filePathPreset, refresh, timeout);
  223. }
  224. /// <summary>
  225. /// Raises the get file path async button click event.
  226. /// </summary>
  227. public void OnGetFilePathAsyncButtonClick ()
  228. {
  229. bool refresh = refreshToggle.isOn;
  230. string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
  231. int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
  232. filePathInputField.text = "";
  233. GetFilePathAsync (filePathPreset [filePathDropdown.value], refresh, timeout);
  234. }
  235. /// <summary>
  236. /// Raises the get multiple file paths async button click event.
  237. /// </summary>
  238. public void OnGetMultipleFilePathsAsyncButtonClick ()
  239. {
  240. bool refresh = refreshToggle.isOn;
  241. string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
  242. int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
  243. filePathInputField.text = "";
  244. GetMultipleFilePathsAsync (filePathPreset, refresh, timeout);
  245. }
  246. /// <summary>
  247. /// Raises the abort button click event.
  248. /// </summary>
  249. public void OnAbortButtonClick ()
  250. {
  251. if (getFilePath_Coroutine != null) {
  252. StopCoroutine (getFilePath_Coroutine);
  253. ((IDisposable)getFilePath_Coroutine).Dispose ();
  254. }
  255. ShowButton ();
  256. }
  257. /// <summary>
  258. /// Raises the on scroll rect value changed event.
  259. /// </summary>
  260. public void OnScrollRectValueChanged ()
  261. {
  262. if (filePathInputField.text.Length > 10000) {
  263. filePathInputField.text = filePathInputField.text.Substring (filePathInputField.text.Length - 10000);
  264. }
  265. }
  266. public enum TimeoutPreset : int
  267. {
  268. _0 = 0,
  269. _1 = 1,
  270. _10 = 10,
  271. }
  272. }
  273. }