在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();
}
}
}