unity查找被prefab引用中的重复资源

贴图,模型(来源雨松u3d游戏开发)
查询项目中的无用资源,然后删除,直接使用这个工具吧
https://jingyan.baidu.com/article/3f16e00318b2582591c1030d.html
有些资源也许需要动态加载,慎重

using System.Collections;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;
using System;
using System.IO;
using System.Collections.Generic;

public class ReportWindow:EditorWindow  {

    /// <summary>
    /// 查找重复贴图和模型
    /// </summary>
    [MenuItem("Tools/查找重复贴图和模型")]
	static void ReportTexture()
	{
		Dictionary<string,string> md5dic = new Dictionary<string, string> (); //使用string-string,不用集合,直接判断是否已经有value即可
		string[] paths = AssetDatabase.FindAssets("t:prefab",new string[]{"Assets/Resources"});  //获得的是guid string集合

		foreach (var prefabGuid in paths) {
			string prefabAssetPath = AssetDatabase.GUIDToAssetPath(prefabGuid);
			string[] depend = AssetDatabase.GetDependencies (prefabAssetPath,true);
			for (int i = 0; i < depend.Length; i++) {
				string assetPath = depend [i];
				AssetImporter importer = AssetImporter.GetAtPath(assetPath);
				//满足贴图和模型资源
				if (importer is TextureImporter || importer is ModelImporter) { //Audio,视频也是外部导入文件,但一般不加入prefab内存中,资源过大,而是运行中动态加入,material不是外部导入文件,一个角色一般都有一个material
					string md5 = GetMD5Hash(Path.Combine(Directory.GetCurrentDirectory(),assetPath));
					string path;
				
					if (!md5dic.TryGetValue (md5, out path)) {
						md5dic [md5] = assetPath;
					}else {
						if (path != assetPath) {
							Debug.LogFormat ("{0} {1} 资源发生重复!被{2}所引用", path, assetPath,prefabAssetPath);
						}
					}
				}
			}
		}
	}
	/// <summary>
	/// 获取文件Md5
	/// </summary>
	/// <returns>The M d5 hash.</returns>
	/// <param name="filePath">File path.</param>
	static string GetMD5Hash(string filePath)
	{
		MD5 md5 = new MD5CryptoServiceProvider();
		return BitConverter.ToString(md5.ComputeHash(File.ReadAllBytes(filePath))).Replace("-", "").ToLower();
	}
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值