Unity——资源管理工具_检查某个文件夹下的图片是否被 预制体 引用过

一、策划需要进行资源管理,所以要知道某个文件夹下的图片有没有用到。

二、效果如下:

三、代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEngine.UI;
public class FindTrashRes : Editor {


	public static string prefabPath  = "Assets/GameData/UI/Prefab";
	public static string texturePath = "";

	[MenuItem("Assets/FindTrashRes(检查未使用的图片)")]
	private static void Check(){
		if (Selection.activeObject != null ) {
			if(Selection.activeObject.GetType () == typeof(DefaultAsset)){
				texturePath = AssetDatabase.GetAssetPath((DefaultAsset)Selection.activeObject);
				CheckTextureForPrefab ();
			}
		}
	}

	private static void CheckTextureForPrefab(){
		string log = "检查  图片文件夹:"+texturePath+"\t预制体文件夹:"+prefabPath;
		Hashtable texturePathTab = new Hashtable ();
		DirectoryInfo textureDir = new DirectoryInfo(texturePath);
		DirectoryInfo prefabDir = new DirectoryInfo(prefabPath);

		//获取全部图片文件
		FileInfo[] files = textureDir.GetFiles("*.jpg",SearchOption.AllDirectories);
		EditorUtility.DisplayProgressBar ("查找文件夹下所有图片", "", 0);
		for (int i = 0; i < files.Length; i++) {
			string subPath = files [i].FullName.Remove(0,Application.dataPath.Length-6);
			subPath = subPath.Replace ("\\","/");
			texturePathTab.Add (subPath, "");
		}
		files = textureDir.GetFiles("*.png",SearchOption.AllDirectories);
		string tlog = "";
		for (int i = 0; i < files.Length; i++) {
			string subPath = files [i].FullName.Remove(0,Application.dataPath.Length-6);
			subPath = subPath.Replace ("\\","/");
			texturePathTab.Add (subPath, "");
		}

		EditorUtility.DisplayProgressBar ("查找文件夹下所有图片", "", 1);

		//判断是否被使用过
		files = prefabDir.GetFiles("*.prefab",SearchOption.AllDirectories);
		for(int i=0;i<files.Length;i++){
			EditorUtility.DisplayProgressBar ("检查预制体", files[i].Name, i/(float)files.Length);
			string subPath = files [i].FullName.Remove(0,Application.dataPath.Length-6);
			GameObject obj = AssetDatabase.LoadAssetAtPath(subPath, typeof(GameObject)) as GameObject;
			Image[] images = obj.GetComponentsInChildren<Image> ();
			for(int im=0;im<images.Length;im++){
				var texture = images [im].mainTexture;
				if (texture != null) {
					string assetPath = AssetDatabase.GetAssetPath (texture);
					if (texturePathTab.ContainsKey (assetPath)) {
						texturePathTab[assetPath]=subPath.Remove(0,prefabPath.Length+1);
					}
				}
			}
		}
		//合成打印日志
		EditorUtility.DisplayProgressBar ("合成打印日志", "", 0);
		log += "\n未被使用的图片:";
		foreach (string k in texturePathTab.Keys)
		{
			string prefab_path = (string)texturePathTab[k];
			if (string.IsNullOrEmpty (prefab_path)) {
				log += ("\n"+k.Remove(0,texturePath.Length+1));
			}
		}
		log += "\n\n\n被使用过的图片:";
		foreach (string k in texturePathTab.Keys)
		{
			string prefab_path = (string)texturePathTab[k];
			if (!string.IsNullOrEmpty (prefab_path)) {
				log += ("\n图片:"+k.Remove(0,texturePath.Length+1)+"\t使用的预制体:"+prefab_path);
			}
		}
		EditorUtility.DisplayProgressBar ("合成打印日志", "", 1);
		Debug.Log (log);
		EditorUtility.ClearProgressBar ();
	}



}

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 Unity 中拷贝了一个文件夹,并且想要将该文件夹中的所有预制引用替换为新文件夹中的预制,你可以使用 Unity 的 AssetDatabase API 来实现。 以下是一个示例代码,可以将一个名为 "OldFolder" 的文件夹中所有预制引用替换为名为 "NewFolder" 的文件夹中的预制: ```csharp using UnityEngine; using UnityEditor; using System.IO; public class ReplacePrefabReferences : AssetPostprocessor { // 监听资源导入事件 void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { // 遍历所有导入的资源 foreach (string str in importedAssets) { // 判断是否预制 if (str.EndsWith(".prefab")) { // 加载预制 GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(str); // 获取预制中所有组件 Component[] components = prefab.GetComponentsInChildren<Component>(true); // 遍历组件 foreach (Component component in components) { // 判断组件是否引用类型 if (component == null || component.GetType().IsValueType) continue; // 获取组件的 SerializedObject SerializedObject so = new SerializedObject(component); SerializedProperty sp = so.GetIterator(); // 遍历组件的 SerializedProperty while (sp.NextVisible(true)) { // 判断 SerializedProperty 是否为 ObjectReference 类型 if (sp.propertyType == SerializedPropertyType.ObjectReference) { // 获取 SerializedProperty 引用的对象 Object obj = sp.objectReferenceValue; // 判断对象是否预制或场景中的 GameObject if (obj != null && (PrefabUtility.IsPartOfPrefabAsset(obj) || PrefabUtility.IsPartOfPrefabInstance(obj) || PrefabUtility.GetPrefabAssetType(obj) == PrefabAssetType.Regular || obj is GameObject)) { // 获取对象的 AssetPath string assetPath = AssetDatabase.GetAssetPath(obj); // 判断 AssetPath 是否在 OldFolder 中 if (assetPath.StartsWith("Assets/OldFolder/")) { // 替换对象的引用 string newPath = assetPath.Replace("Assets/OldFolder/", "Assets/NewFolder/"); Object newObject = AssetDatabase.LoadAssetAtPath(newPath, obj.GetType()); sp.objectReferenceValue = newObject; } } } } // 应用修改 so.ApplyModifiedProperties(); } // 保存修改 PrefabUtility.SaveAsPrefabAssetAndConnect(prefab, str, InteractionMode.AutomatedAction); } } } } ``` 这段代码监听了 Unity 中的资源导入事件,当导入的资源为预制时,遍历预制中的所有组件,查找所有 ObjectReference 类型的 SerializedProperty,并判断该 SerializedProperty 引用的对象是否在 OldFolder 中,如果是,则将对象的引用替换为 NewFolder 中的对应对象。最后,保存修改后的预制

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值