【工作笔记】UnityEditor Project视图新增图片重名覆盖

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值