在UGUI使用過程中,我們經常會替換已有圖片,除了在文件夾裏操作外,也經常直接將圖片拖拽到Unity編輯器中,這個主要是爲了防止抽象派美術們不會傳meta文件,導致各種衝突。本方法也並不會切斷UI和圖片之間的引用關係。
當然,只要是文件都可以這樣判斷,不侷限於圖片,不過,手段比較暴力,因爲我並沒有找到Unity相關支持。
using System.IO;
using UnityEditor;
public class DuplicateCoverEditor : AssetPostprocessor
{
public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
if (importedAsset.Length > 0 && importedAsset.Length == DragAndDrop.paths.Length)
{
foreach (var path in importedAsset)
{
if (path.ToLower().EndsWith(".png") || path.ToLower().EndsWith(".jpg"))
{
File.Delete(path);
}
}
string targetDirectory = Path.GetDirectoryName(importedAsset[0]);
foreach (var path in DragAndDrop.paths)
{
string fileName = Path.GetFileName(path);
if (fileName.ToLower().EndsWith(".png") || fileName.ToLower().EndsWith(".jpg"))
{
File.Copy(path, targetDirectory + @"\" + fileName, true);
}
}
AssetDatabase.Refresh();
}
}
}