コード例 #1
0
        /// <summary>
        /// Opens a file with the OS default application.
        /// NOTE: only works for standalone platforms
        /// </summary>
        /// <param name="file">File path</param>
        public static void OpenFile(string file)
        {
            if (isStandalonePlatform || isEditor)
            {
#if UNITY_STANDALONE || UNITY_EDITOR
                if (System.IO.File.Exists(file))
                {
#if ENABLE_IL2CPP
                    using (CTProcess process = new CTProcess())
                    {
                        if (isWindowsPlatform || isWindowsEditor)
                        {
                            process.StartInfo.FileName       = "explorer.exe";
                            process.StartInfo.Arguments      = "\"" + file + "\"";
                            process.StartInfo.UseCmdExecute  = true;
                            process.StartInfo.CreateNoWindow = true;
                        }
                        else if (isMacOSPlatform || isMacOSEditor)
                        {
                            process.StartInfo.FileName  = "open";
                            process.StartInfo.Arguments = "\"" + file + "\"";
                        }
                        else
                        {
                            process.StartInfo.FileName  = "xdg-open";
                            process.StartInfo.Arguments = "\"" + file + "\"";
                        }

                        process.Start();
                    }
#else
                    using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                    {
                        if (isMacOSPlatform || isMacOSEditor)
                        {
                            process.StartInfo.FileName         = "open";
                            process.StartInfo.WorkingDirectory =
                                System.IO.Path.GetDirectoryName(file) + BaseConstants.PATH_DELIMITER_UNIX;
                            process.StartInfo.Arguments = "-t " + System.IO.Path.GetFileName(file);
                        }
                        else if (isLinuxPlatform || isLinuxEditor)
                        {
                            process.StartInfo.FileName         = "xdg-open";
                            process.StartInfo.WorkingDirectory =
                                System.IO.Path.GetDirectoryName(file) + BaseConstants.PATH_DELIMITER_UNIX;
                            process.StartInfo.Arguments = System.IO.Path.GetFileName(file);
                        }
                        else
                        {
                            process.StartInfo.FileName = file;
                        }

                        process.Start();
                    }
#endif
                }
                else
                {
                    Debug.LogWarning("File doesn't exist: " + file);
                }
#endif
            }
            else
            {
                Debug.LogWarning("'OpenFile' is not supported on the current platform!");
            }
        }
コード例 #2
0
        /// <summary>
        /// Shows the location of a path or file in OS file explorer.
        /// NOTE: only works for standalone platforms
        /// </summary>
        /// <param name="file">File path</param>
        public static void ShowFileLocation(string file)
        {
            if (isStandalonePlatform || isEditor)
            {
#if UNITY_STANDALONE || UNITY_EDITOR
                string path;

                if (string.IsNullOrEmpty(file) || file.Equals("."))
                {
                    path = ".";
                }
                else if ((isWindowsPlatform || isWindowsEditor) && file.Length < 4)
                {
                    path = file; //root directory
                }
                else
                {
                    path = ValidatePath(System.IO.Path.GetDirectoryName(file));
                }

                //Debug.Log("'" + path + "'");

                if (System.IO.Directory.Exists(path))
                {
#if ENABLE_IL2CPP
                    using (CTProcess process = new CTProcess())
                    {
                        if (isWindowsPlatform || isWindowsEditor)
                        {
                            process.StartInfo.FileName       = "explorer.exe";
                            process.StartInfo.Arguments      = "\"" + path + "\"";
                            process.StartInfo.UseCmdExecute  = true;
                            process.StartInfo.CreateNoWindow = true;
                        }
                        else if (isMacOSPlatform || isMacOSEditor)
                        {
                            process.StartInfo.FileName  = "open";
                            process.StartInfo.Arguments = "\"" + path + "\"";
                        }
                        else
                        {
                            process.StartInfo.FileName  = "xdg-open";
                            process.StartInfo.Arguments = "\"" + path + "\"";
                        }

                        process.Start();
                    }
#else
                    System.Diagnostics.Process.Start(path);
#endif
                }
                else
                {
                    Debug.LogWarning("Path to file doesn't exist: " + path);
                }
#endif
            }
            else
            {
                Debug.LogWarning("'ShowFileLocation' is not supported on the current platform!");
            }
        }