Unity复制粘贴功能
生活随笔
收集整理的這篇文章主要介紹了
Unity复制粘贴功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?https://github.com/4000white/UnityCopyPaste
同一項目內,復制粘貼資源,不用打開文件夾操作,或者Dublicate再移動。可以批量復制。
復制文件夾時,文件夾內的資源引用了本文件夾內的其它資源,復制出來的資源的依賴會替換新復制出來的資源
using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using UnityEngine; using System; using Object = UnityEngine.Object;namespace UnityEditor {public class AssetCopyer{//復制粘貼一個文件夾時,如果有資源引用了文件夾里的其它資源,新復制出來的資源的引用會替換新文件夾里對應的文件,而不是繼續引用舊文件夾里的資源//執行這種操作的資源的后綴private static List<string> includeList = new List<string> { ".prefab", ".mat", ".anim", ".controller" };private const string META = ".meta"; #if UNITY_EDITOR_OSXprivate const string DS_STORE = ".DS_Store";private static List<string> ignoreList = new List<string> { META, DS_STORE }; #elseprivate static List<string> ignoreList = new List<string> {META}; #endifprivate static List<string> copyPaths = new List<string>();[MenuItem("Assets/Copy", false, -10000)]static void AssetCopy(){var objs = Selection.GetFiltered<Object>(SelectionMode.Assets);copyPaths.Clear();foreach (var obj in objs){copyPaths.Add(AssetDatabase.GetAssetPath(obj));}}[MenuItem("Assets/Paste", true)]static bool ValidAssetPaste(){return copyPaths.Count > 0;}[MenuItem("Assets/Paste", false, -10000)]static void AssetPaste(){var pastePosition = AssetDatabase.GetAssetPath(Selection.activeObject);if (Path.HasExtension(pastePosition)){pastePosition = Path.GetDirectoryName(pastePosition);}foreach (var copyPath in copyPaths){if (!string.IsNullOrEmpty(copyPath)){var fileName = Path.GetFileName(copyPath);var pastePath = pastePosition + "/" + fileName;Copy(copyPath, pastePath);}}}public static void Copy(string copyPath, string pastePath){Debug.Log("copy " + copyPath + " to " + pastePath);try{if (!string.IsNullOrEmpty(copyPath)){if (Path.HasExtension(copyPath)){CopyFile(copyPath, pastePath);}else{CopyFolder(copyPath, pastePath);}}}catch (Exception e){Debug.Log(e);}}//復制單個文件private static void CopyFile(string copyPath, string pastePath){if (!Path.HasExtension(copyPath) || !Path.HasExtension(pastePath)){throw new Exception();}pastePath = NextAvailableFilename(pastePath);File.Copy(copyPath, pastePath);AssetDatabase.Refresh();AssetDatabase.SaveAssets();Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(pastePath);}//復制文件夾private static void CopyFolder(string copyPath, string pastePath){if (Path.HasExtension(copyPath) || Path.HasExtension(pastePath)){throw new Exception();}pastePath = NextAvailableFilename(pastePath);Debug.Log("pastePath " + pastePath);RecursivelyCopyFolder(copyPath, pastePath);AssetDatabase.Refresh();ReplaceReferences(copyPath, pastePath);AssetDatabase.Refresh();AssetDatabase.SaveAssets();Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(pastePath);}//剛復制出來的新文件夾里的資源還是會有對舊文件夾中資源的引用,通過替換guid的方式,把引用替換成對新文件夾中對應文件的引用private static void ReplaceReferences(string oldPath, string newPath){var guidMap = new Dictionary<string, string>();var fullPath = Path.GetFullPath(oldPath);var filePaths = GetAllFiles(fullPath);var length = fullPath.Length + 1;foreach (var filePath in filePaths){string extension = Path.GetExtension(filePath);if (!ignoreList.Contains(extension)){string assetPath = GetRelativeAssetPath(filePath);string relativePath = filePath.Remove(0, length);string guid = AssetDatabase.AssetPathToGUID(assetPath);string copyPath = newPath + "/" + relativePath;string copyGuid = AssetDatabase.AssetPathToGUID(copyPath);if (copyGuid != null){guidMap[guid] = copyGuid;}}}fullPath = Path.GetFullPath(newPath);filePaths = GetAllFiles(fullPath);foreach (var filePath in filePaths){string extension = Path.GetExtension(filePath);if (includeList.Contains(extension)){var assetPath = GetRelativeAssetPath(filePath);string[] deps = AssetDatabase.GetDependencies(assetPath, true);var fileString = File.ReadAllText(filePath);bool bChanged = false;foreach (var v in deps){var guid = AssetDatabase.AssetPathToGUID(v);if (guidMap.ContainsKey(guid)){if (Regex.IsMatch(fileString, guid)){fileString = Regex.Replace(fileString, guid, guidMap[guid]);bChanged = true;var oldFile = AssetDatabase.GUIDToAssetPath(guid);var newFile = AssetDatabase.GUIDToAssetPath(guidMap[guid]);}}}if (bChanged){File.WriteAllText(filePath, fileString);}}}}private static string GetRelativeAssetPath(string fullPath){fullPath = fullPath.Replace("\\", "/");int index = fullPath.IndexOf("Assets");string relativePath = fullPath.Substring(index);return relativePath;}private static string[] GetAllFiles(string fullPath){List<string> files = new List<string>();foreach (string file in GetFiles(fullPath)){files.Add(file);}return files.ToArray();}private static IEnumerable<string> GetFiles(string path){Queue<string> queue = new Queue<string>();queue.Enqueue(path);while (queue.Count > 0){path = queue.Dequeue();try{foreach (string subDir in Directory.GetDirectories(path)){queue.Enqueue(subDir);}}catch (Exception ex){Console.Error.WriteLine(ex);}string[] files = null;try{files = Directory.GetFiles(path);}catch (Exception ex){Console.Error.WriteLine(ex);}if (files != null){for (int i = 0; i < files.Length; i++){yield return files[i];}}}}static void RecursivelyCopyFolder(string sourcePath, string destPath){if (sourcePath == destPath){throw new Exception("sourcePath == destPath");}if (Directory.Exists(sourcePath)){if (!Directory.Exists(destPath)){try{Directory.CreateDirectory(destPath);}catch (Exception ex){Debug.LogError(ex);}}List<string> files = new List<string>(Directory.GetFiles(sourcePath));files.ForEach(c =>{ #if UNITY_EDITOR_OSXif (!c.EndsWith(META) && !c.EndsWith(DS_STORE)) #elseif (!c.EndsWith(META)) #endif{string destFile = Path.Combine(destPath, Path.GetFileName(c));File.Copy(c, destFile, true);}});List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));folders.ForEach(c =>{string destDir = Path.Combine(destPath, Path.GetFileName(c));RecursivelyCopyFolder(c, destDir);});}else{throw new Exception("sourcePath is not exist!");}}private static string numberPattern = " ({0})";private static bool FileExist(string filePath, bool isFolder){if (isFolder){return Directory.Exists(filePath);}else{return File.Exists(filePath);}}//獲取一個不重復的文件名,如 aa (1).prefabpublic static string NextAvailableFilename(string path){bool isFolder = !Path.HasExtension(path);if (!FileExist(path, isFolder)){return path;}string tmp;if (Path.HasExtension(path)){tmp = path.Insert(path.LastIndexOf(Path.GetExtension(path)), numberPattern);}else{tmp = path + numberPattern;}return GetNextFilename(tmp, isFolder);}private static string GetNextFilename(string pattern, bool isFolder){string tmp = string.Format(pattern, 1);if (tmp == pattern){throw new ArgumentException("The pattern must include an index place-holder", "pattern");}if (!FileExist(tmp, isFolder)){return tmp;}int min = 1, max = 2;while (FileExist(string.Format(pattern, max), isFolder)){min = max;max *= 2;}while (max != min + 1){int pivot = (max + min) / 2;if (FileExist(string.Format(pattern, pivot), isFolder))min = pivot;elsemax = pivot;}return string.Format(pattern, max);}} }總結
以上是生活随笔為你收集整理的Unity复制粘贴功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六级写作^_^
- 下一篇: Java_语法基础_定义规范的接口类型