12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using XGame.Framework.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- namespace XGame.Editor.Asset.Tools
- {
- public class PrefabDependenciesTask
- {
- private class PrefabInfo
- {
- public string assetPath;
- public string[] dependencies;
- }
- private HashSet<string> _prefabPaths;
- private List<string> _tempDependencies;
- private List<PrefabInfo> _prefabInfos;
- public void Run()
- {
- Init();
- FindDependencies();
- Export();
- }
- private void Init()
- {
- var assetRoots = FileUtil.GetProductAssetsRoots();
- var prefabGUIDs = AssetDatabase.FindAssets("t:Prefab", assetRoots);
- _prefabPaths = new HashSet<string>();
- foreach (var guid in prefabGUIDs)
- {
- var assetPath = AssetDatabase.GUIDToAssetPath(guid);
- _prefabPaths.Add(assetPath);
- }
- _tempDependencies = new List<string>();
- _prefabInfos = new List<PrefabInfo>();
- }
- private void FindDependencies()
- {
- var index = 0;
- var count = _prefabPaths.Count;
- foreach (var assetPath in _prefabPaths)
- {
- EditorUtility.DisplayProgressBar("检测可加载资源的预制依赖...", $"Path:{assetPath}", (++index) / (float)count);
- FindDependencies(assetPath);
- }
- EditorUtility.ClearProgressBar();
- }
- private void FindDependencies(string assetPath)
- {
- // 只检测直接依赖
- var deps = AssetDatabase.GetDependencies(assetPath, false);
- foreach (var dep in deps)
- {
- if (dep == assetPath)
- {
- continue;
- }
- if (_prefabPaths.Contains(dep))
- {
- _tempDependencies.Add(dep);
- }
- }
- if (_tempDependencies.Count > 0)
- {
- _prefabInfos.Add(new PrefabInfo()
- {
- assetPath = assetPath,
- dependencies = _tempDependencies.ToArray()
- });
- _tempDependencies.Clear();
- }
- }
- private void Export()
- {
- try
- {
- if (_prefabInfos.Count == 0)
- {
- UnityEngine.Debug.Log("PrefabDependencies Completed.");
- return;
- }
- var str = XJson.ToJson(_prefabInfos);
- var path = Path.Combine(Path.GetTempPath(), "PrefabDependencies.json");
- File.WriteAllText(path, str);
- UnityEngine.Debug.Log($"PrefabDependencies 输出路径:{path}");
- #if UNITY_EDITOR_WIN
- var proc = System.Diagnostics.Process.Start("notepad.exe", path);
- #endif
- }
- catch (Exception e)
- {
- UnityEngine.Debug.LogException(e);
- }
- }
- }
- }
|