void OnDestroy()
 {
     if (m_Instance == this)
     {
         m_Instance = null;
     }
 }
    public static void Init()
    {
        if (m_Instance == null)
        {
            GameObject go = new GameObject();
            go.name = "File Browser Dispatcher";

            m_Instance = go.AddComponent <FileBrowserDispatcher>();
        }
    }
 void Awake()
 {
     if (m_Instance != null)
     {
         DestroyImmediate(this);
     }
     else
     {
         m_Instance   = this;
         m_MainThread = Thread.CurrentThread;
     }
 }
    private static void CommonPanel(string title, string startingDirectory, string defaultName, string[] extensionArray, DialogType dialogType, bool allowMultipleSelection, Action <bool, string> onDone)
    {
        FileBrowserDispatcher.Init();

        if (title == null)
        {
            title = string.Empty;
        }
        if (startingDirectory == null)
        {
            startingDirectory = string.Empty;
        }
        startingDirectory = startingDirectory.Replace(@"\\", "/").Replace(@"\", "/");
        if (defaultName == null)
        {
            defaultName = string.Empty;
        }

        string extensionString = "";

        if (extensionArray != null && extensionArray.Length != 0)
        {
            extensionString = "Files (";

            for (int i = 0; i < extensionArray.Length; i++)
            {
                if (extensionArray[i].Contains(",") || extensionArray[i].Contains(".") || extensionArray[i].Contains("*"))
                {
                    Debug.LogError("[FileBrowserWindows] Extensions should not contain , . or *");
                    return;
                }

                extensionString += "*." + extensionArray[i] + ", ";
            }

            if (extensionString.EndsWith(", "))
            {
                extensionString = extensionString.Substring(0, extensionString.Length - 2);
            }

            extensionString += ")|";

            for (int i = 0; i < extensionArray.Length; i++)
            {
                extensionString += "*." + extensionArray[i] + ";";
            }
        }

        string      fileBrowserExePath = Application.streamingAssetsPath + "/PygmyMonkey/FileBrowser/FileBrowser.exe";
        ThreadStart threadStart        = new ThreadStart(() =>
        {
            Process process = new Process();
            process.StartInfo.CreateNoWindow         = true;
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.StartInfo.FileName  = "cmd.exe";
            process.StartInfo.Arguments = "/c \""
                                          + "\"" + fileBrowserExePath + "\""
                                          + " \"" + Process.GetCurrentProcess().Id + "\""
                                          + " \"" + title + "\""
                                          + " \"" + startingDirectory + "\""
                                          + " \"" + defaultName + "\""
                                          + " \"" + extensionString + "\""
                                          + " \"" + allowMultipleSelection.ToString() + "\""
                                          + " \"" + (int)dialogType + "\""
                                          + "\"";

            process.Start();
            process.WaitForExit();

            string error = process.StandardError.ReadToEnd();
            if (!string.IsNullOrEmpty(error))
            {
                FileBrowserDispatcher.InvokeAsync(() =>
                {
                    error = GetStringFromUnicode(error.Trim());
                    Debug.LogError("[FileBrowserWindows] Error:" + error);
                    onDone(true, error.Trim());
                });
            }

            string result = process.StandardOutput.ReadToEnd();
            if (!string.IsNullOrEmpty(result) && string.IsNullOrEmpty(error))
            {
                FileBrowserDispatcher.InvokeAsync(() =>
                {
                    result = GetStringFromUnicode(result.Trim());
                    if (!result.Equals("cancel"))
                    {
                        result = System.IO.File.ReadAllText(result);                         // We read the content of the tmp file, containing the result
                    }

                    onDone(false, result);
                });
            }

            process.Close();
        });

        Thread thread = new Thread(threadStart);

        thread.Start();
    }