123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.VideoioModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.UnityUtils;
- namespace OpenCVForUnitySample
- {
- /// <summary>
- /// Utils_GetFilePath Example
- /// An example of how to get the readable path of a file in the "StreamingAssets" folder using the Utils class.
- /// </summary>
- public class Utils_GetFilePathExample : MonoBehaviour
- {
- /// <summary>
- /// The file path dropdown.
- /// </summary>
- public Dropdown filePathDropdown;
- /// <summary>
- /// The refresh toggle.
- /// </summary>
- public Toggle refreshToggle;
- /// <summary>
- /// The timeout dropdown.
- /// </summary>
- public Dropdown timeoutDropdown;
- /// <summary>
- /// The get file path button.
- /// </summary>
- public Button getFilePathButton;
- /// <summary>
- /// The get multiple file paths button.
- /// </summary>
- public Button getMultipleFilePathsButton;
- /// <summary>
- /// The get file path async button.
- /// </summary>
- public Button getFilePathAsyncButton;
- /// <summary>
- /// The get multiple file paths async button.
- /// </summary>
- public Button getMultipleFilePathsAsyncButton;
- /// <summary>
- /// The abort button.
- /// </summary>
- public Button abortButton;
- /// <summary>
- /// The file path input field.
- /// </summary>
- // public InputField filePathInputField;
- public Text filePathInputField;
- string[] filePathPreset = new string[] {
- "768x576_mjpeg.mjpeg",
- "/lbpcascade_frontalface.xml",
- "calibration_images/left01.jpg",
- "xxx"
- };
- IEnumerator getFilePath_Coroutine;
- // Use this for initialization
- void Start ()
- {
- abortButton.interactable = false;
- }
- private void GetFilePath (string filePath, bool refresh, int timeout)
- {
- string readableFilePath = Utils.getFilePath (filePath, refresh, timeout);
- #if UNITY_WEBGL && !UNITY_EDITOR
- Debug.Log ("The Utils.getFilePath() method is not supported on WebGL platform.");
- filePathInputField.text = filePathInputField.text + "The Utils.getFilePath() method is not supported on WebGL platform." + "\n";
- if (!string.IsNullOrEmpty (readableFilePath)) {
- Debug.Log ("completed: " + "readableFilePath=" + readableFilePath);
- filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath=" + readableFilePath;
- }
- #else
- if (string.IsNullOrEmpty (readableFilePath)) {
- Debug.LogError ("completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
- filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
- } else {
- Debug.Log ("completed: " + "readableFilePath= " + readableFilePath);
- filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + readableFilePath + "\n";
- }
- #endif
- }
- private void GetMultipleFilePaths (string[] filePaths, bool refresh, int timeout)
- {
- string[] readableFilePaths = Utils.getMultipleFilePaths (filePaths, refresh, timeout);
- #if UNITY_WEBGL && !UNITY_EDITOR
- Debug.Log ("The Utils.getFilePath() method is not supported on WebGL platform.");
- filePathInputField.text = filePathInputField.text + "The Utils.getFilePath() method is not supported on WebGL platform." + "\n";
- for (int i = 0; i < readableFilePaths.Length; i++) {
- if (!string.IsNullOrEmpty (readableFilePaths [i])) {
- Debug.Log ("readableFilePath[" + i + "]=" + readableFilePaths [i]);
- filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]=" + readableFilePaths [i];
- }
- }
- #else
- Debug.Log ("allCompleted:" + "\n");
- filePathInputField.text = filePathInputField.text + "allCompleted:" + "\n";
- for (int i = 0; i < readableFilePaths.Length; i++) {
- if (string.IsNullOrEmpty (readableFilePaths [i])) {
- Debug.LogError ("readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
- filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
- } else {
- Debug.Log ("readableFilePath[" + i + "]= " + readableFilePaths [i]);
- filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + readableFilePaths [i] + "\n";
- }
- }
- #endif
- }
- private void GetFilePathAsync (string filePath, bool refresh, int timeout)
- {
- HideButton ();
- getFilePath_Coroutine = Utils.getFilePathAsync (filePath, (result) => { // completed callback
- getFilePath_Coroutine = null;
- ShowButton ();
- string readableFilePath = result;
- if (string.IsNullOrEmpty (readableFilePath)) {
- Debug.LogError ("completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
- filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + filePath + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
- }
- Debug.Log ("completed: " + "readableFilePath= " + readableFilePath);
- filePathInputField.text = filePathInputField.text + "completed: " + "readableFilePath= " + readableFilePath + "\n";
- }, (path, progress) => { // progressChanged callback
- Debug.Log ("progressChanged: " + "path= " + path + " progress= " + progress);
- filePathInputField.text = filePathInputField.text + "progressChanged: " + "path= " + path + " progress= " + progress + "\n";
- }, (path, error, responseCode) => { // errorOccurred callback
- getFilePath_Coroutine = null;
- ShowButton ();
- Debug.Log ("errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode);
- filePathInputField.text = filePathInputField.text + "errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode + "\n";
- }, refresh, timeout);
-
- StartCoroutine (getFilePath_Coroutine);
- }
- private void GetMultipleFilePathsAsync (string[] filePaths, bool refresh, int timeout)
- {
- HideButton ();
- getFilePath_Coroutine = Utils.getMultipleFilePathsAsync (filePaths, (result) => { // allCompleted callback
- getFilePath_Coroutine = null;
- ShowButton ();
- string[] readableFilePaths = result;
- Debug.Log ("allCompleted:" + "\n");
- filePathInputField.text = filePathInputField.text + "allCompleted:" + "\n";
- for (int i = 0; i < readableFilePaths.Length; i++) {
- if (string.IsNullOrEmpty (readableFilePaths [i])) {
- Debug.LogError ("readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
- filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + filePaths [i] + " is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. " + "\n";
- } else {
- Debug.Log ("readableFilePath[" + i + "]= " + readableFilePaths [i]);
- filePathInputField.text = filePathInputField.text + "readableFilePath[" + i + "]= " + readableFilePaths [i] + "\n";
- }
- }
- }, (path, progress) => { // progressChanged callback
- Debug.Log ("progressChanged: " + "path= " + path + " progress= " + progress);
- filePathInputField.text = filePathInputField.text + "progressChanged: " + "path= " + path + " progress= " + progress + "\n";
- }, (path, error, responseCode) => { // errorOccurred callback
- Debug.Log ("errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode);
- filePathInputField.text = filePathInputField.text + "errorOccurred: " + "path= " + path + " error= " + error + " responseCode= " + responseCode + "\n";
- }, refresh, timeout);
- StartCoroutine (getFilePath_Coroutine);
- }
- private void ShowButton ()
- {
- getFilePathButton.interactable = true;
- getMultipleFilePathsButton.interactable = true;
- getFilePathAsyncButton.interactable = true;
- getMultipleFilePathsAsyncButton.interactable = true;
- abortButton.interactable = false;
- }
- private void HideButton ()
- {
- getFilePathButton.interactable = false;
- getMultipleFilePathsButton.interactable = false;
- getFilePathAsyncButton.interactable = false;
- getMultipleFilePathsAsyncButton.interactable = false;
- abortButton.interactable = true;
- }
- // Update is called once per frame
- void Update ()
- {
- }
- /// <summary>
- /// Raises the destroy event.
- /// </summary>
- void OnDestroy ()
- {
- if (getFilePath_Coroutine != null) {
- StopCoroutine (getFilePath_Coroutine);
- ((IDisposable)getFilePath_Coroutine).Dispose ();
- }
- }
- /// <summary>
- /// Raises the back button click event.
- /// </summary>
- public void OnBackButtonClick ()
- {
- SceneManager.LoadScene ("OpenCVForUnityExample");
- }
- /// <summary>
- /// Raises the get file path button click event.
- /// </summary>
- public void OnGetFilePathButtonClick ()
- {
- bool refresh = refreshToggle.isOn;
- string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
- int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
- filePathInputField.text = "";
- GetFilePath (filePathPreset [filePathDropdown.value], refresh, timeout);
- }
- /// <summary>
- /// Raises the get multiple file paths button click event.
- /// </summary>
- public void OnGetMultipleFilePathsButtonClick ()
- {
- bool refresh = refreshToggle.isOn;
- string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
- int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
- filePathInputField.text = "";
- GetMultipleFilePaths (filePathPreset, refresh, timeout);
- }
- /// <summary>
- /// Raises the get file path async button click event.
- /// </summary>
- public void OnGetFilePathAsyncButtonClick ()
- {
- bool refresh = refreshToggle.isOn;
- string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
- int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
- filePathInputField.text = "";
- GetFilePathAsync (filePathPreset [filePathDropdown.value], refresh, timeout);
- }
- /// <summary>
- /// Raises the get multiple file paths async button click event.
- /// </summary>
- public void OnGetMultipleFilePathsAsyncButtonClick ()
- {
- bool refresh = refreshToggle.isOn;
- string[] enumNames = Enum.GetNames (typeof(TimeoutPreset));
- int timeout = (int)System.Enum.Parse (typeof(TimeoutPreset), enumNames [timeoutDropdown.value], true);
- filePathInputField.text = "";
- GetMultipleFilePathsAsync (filePathPreset, refresh, timeout);
- }
- /// <summary>
- /// Raises the abort button click event.
- /// </summary>
- public void OnAbortButtonClick ()
- {
- if (getFilePath_Coroutine != null) {
- StopCoroutine (getFilePath_Coroutine);
- ((IDisposable)getFilePath_Coroutine).Dispose ();
- }
- ShowButton ();
- }
- /// <summary>
- /// Raises the on scroll rect value changed event.
- /// </summary>
- public void OnScrollRectValueChanged ()
- {
- if (filePathInputField.text.Length > 10000) {
- filePathInputField.text = filePathInputField.text.Substring (filePathInputField.text.Length - 10000);
- }
- }
- public enum TimeoutPreset : int
- {
- _0 = 0,
- _1 = 1,
- _10 = 10,
- }
- }
- }
|