/// <summary> /// Copies directory located at sourcePath to destinationPath. Displays a progress bar. /// Same as the previous method, but uses recursion to copy all nested folders as well. /// </summary> /// <param name="source">Directory to be copied.</param> /// <param name="destination">Destination directory (created automatically if needed).</param> /// <param name="totalBytes">Total bytes to be copied. Calculated automatically, initialize at 0.</param> /// <param name="copiedBytes">To track already copied bytes. Calculated automatically, initialize at 0.</param> /// <param name="progressBarPrefix">Optional string added to the beginning of the progress bar window header.</param> private static void CopyDirectoryWithProgressBarRecursive(DirectoryInfo source, DirectoryInfo destination, ref long totalBytes, ref long copiedBytes, string progressBarPrefix = "") { /// Directory cannot be copied into itself. if (source.FullName.ToLower() == destination.FullName.ToLower()) { Debug.LogError("Cannot copy directory into itself."); return; } /// Calculate total bytes, if required. if (totalBytes == 0) { totalBytes = ClonesManager.GetDirectorySize(source, true, progressBarPrefix); } /// Create destination directory, if required. if (!Directory.Exists(destination.FullName)) { Directory.CreateDirectory(destination.FullName); } /// Copy all files from the source. foreach (FileInfo file in source.GetFiles()) { try { file.CopyTo(Path.Combine(destination.ToString(), file.Name), true); } catch (IOException) { /// Some files may throw IOException if they are currently open in Unity editor. /// Just ignore them in such case. } /// Account the copied file size. copiedBytes += file.Length; /// Display the progress bar. float progress = (float)copiedBytes / (float)totalBytes; bool cancelCopy = EditorUtility.DisplayCancelableProgressBar( progressBarPrefix + "Copying '" + source.FullName + "' to '" + destination.FullName + "'...", "(" + (progress * 100f).ToString("F2") + "%) Copying file '" + file.Name + "'...", progress); if (cancelCopy) { return; } } /// Copy all nested directories from the source. foreach (DirectoryInfo sourceNestedDir in source.GetDirectories()) { DirectoryInfo nextDestingationNestedDir = destination.CreateSubdirectory(sourceNestedDir.Name); ClonesManager.CopyDirectoryWithProgressBarRecursive(sourceNestedDir, nextDestingationNestedDir, ref totalBytes, ref copiedBytes, progressBarPrefix); } }
/// <summary> /// Copies directory located at sourcePath to destinationPath. Displays a progress bar. /// </summary> /// <param name="source">Directory to be copied.</param> /// <param name="destination">Destination directory (created automatically if needed).</param> /// <param name="progressBarPrefix">Optional string added to the beginning of the progress bar window header.</param> public static void CopyDirectoryWithProgressBar(string sourcePath, string destinationPath, string progressBarPrefix = "") { var source = new DirectoryInfo(sourcePath); var destination = new DirectoryInfo(destinationPath); long totalBytes = 0; long copiedBytes = 0; ClonesManager.CopyDirectoryWithProgressBarRecursive(source, destination, ref totalBytes, ref copiedBytes, progressBarPrefix); EditorUtility.ClearProgressBar(); }