private bool LoadAssetRelation() { if (m_TargetObject != null) { if (m_LastTargetObject != m_TargetObject) { string assetPath = AssetDatabase.GetAssetPath(m_TargetObject); if (string.IsNullOrEmpty(assetPath)) { EditorGUILayout.HelpBox("这个Asset不存在,是不是修改了项目文件?重新加载引用关系可以解决", MessageType.Warning); } else if (!m_Graph.Nodes.ContainsKey(assetPath)) { EditorGUILayout.HelpBox("未找到这个Asset的引用。请尝试重新加载引用关系", MessageType.Warning); } else { RelationGraph.Node node = m_Graph.Nodes[assetPath]; m_ReferencesBuffer = LoadAssets(node.References); m_DependenciesBuffer = LoadAssets(node.Dependencies); } } } else { EditorGUILayout.HelpBox("拖拽Asset至上方\"Target\" Field后,会显示Asset的引用关系", MessageType.Info); m_ReferencesBuffer = null; m_DependenciesBuffer = null; } return(m_ReferencesBuffer != null && m_DependenciesBuffer != null); }
/// <summary> /// 加载项目里所有文件的引用关系 /// </summary> /// <param name="loadAssetCount">加载的Asset数量,小于0时,加载所有Asset。载所有Asset时间很长,为了方便测试,加这个参数</param> private IEnumerator LoadAllRelationsAndSaveRecordFile(int loadAssetCount = -1) { m_Loading = true; m_LoadingProgress = 0; if (m_Graph == null) { m_Graph = new RelationGraph(); } m_Graph.Nodes.Clear(); string[] guids = AssetDatabase.FindAssets(""); for (int guidIdx = 0; guidIdx < (loadAssetCount < 0 ? guids.Length : loadAssetCount); guidIdx++) { RelationGraph.Node node = new RelationGraph.Node(); node.AssetPath = AssetDatabase.GUIDToAssetPath(guids[guidIdx]); if (!m_Graph.Nodes.ContainsKey(node.AssetPath)) { m_Graph.Nodes.Add(node.AssetPath, node); } } yield return(null); int allCount = m_Graph.Nodes.Count; int loadedCount = 0; foreach (KeyValuePair <string, RelationGraph.Node> kv in m_Graph.Nodes) { RelationGraph.Node node = kv.Value; node.Dependencies = AssetDatabase.GetDependencies(node.AssetPath, false); for (int dependencieIdx = 0; dependencieIdx < node.Dependencies.Length; dependencieIdx++) { string dependencie = node.Dependencies[dependencieIdx]; if (m_Graph.Nodes.ContainsKey(dependencie)) { m_Graph.Nodes[dependencie].ReferencesCache.Add(node.AssetPath); } } if (++loadedCount % FRAME_LOAD_COUNT == 0) { m_LoadingProgress = (float)loadedCount / (float)allCount; yield return(null); Focus(); } } yield return(null); // 将ReferencesCache转换到References foreach (KeyValuePair <string, RelationGraph.Node> kv in m_Graph.Nodes) { RelationGraph.Node node = kv.Value; node.References = node.ReferencesCache.ToArray(); } try { SerializeUtility.WriteFile(m_RecordFilePath, m_Graph); EditorUtility.DisplayDialog("提示", "加载引用关系并保存到文件成功", "确定"); } catch (Exception ex) { EditorUtility.DisplayDialog("提示", "加载引用关系并保存到文件失败\n" + ex.ToString(), "确定"); } finally { m_Loading = false; } }