當前位置:編程學習大全網 - 源碼下載 - 如何在 Unity 中獲取某個對象的依賴關系

如何在 Unity 中獲取某個對象的依賴關系

在 Unity 中目前我發現了獲取依賴關系的兩個 API 接口,分別是:

EditorUtility.CollectDependencies

AssetDatabase.GetDependencies

其中 AssetDatabase.GetDependencies 獲取到的結果就是上面演示的那樣,是大粒度的依賴關系。而 EditorUtility.CollectDependencies 獲取到的是小粒度的依賴關系,所依賴的組件和 Shader 等都會列出來,非常的仔細。

C#

#if UNITY_EDITOR

using UnityEngine;

using System.Collections;

using UnityEditor;

namespace PT.Find

{

[ExecuteInEditMode]

public static class Find

{

[MenuItem("Find/What objects in scene use this?", false, 20)]

public static void SelectSceneUsesOfAsset()

{

Object selectedObject = Selection.activeObject;

if (selectedObject == null)

{

return;

}

Object[] roots = new Object[]{ selectedObject };

var objs = EditorUtility.CollectDependencies(roots);

string path = AssetDatabase.GetAssetPath(selectedObject);

var objs2 = AssetDatabase.GetDependencies(path);

foreach (var obj in objs)

{

Debug.Log(obj.GetType().Name);

}

}

}

}

#endif

#if UNITY_EDITOR

using UnityEngine;

using System.Collections;

using UnityEditor;

namespace PT.Find

{

[ExecuteInEditMode]

public static class Find

{

[MenuItem("Find/What objects in scene use this?", false, 20)]

public static void SelectSceneUsesOfAsset()

{

Object selectedObject = Selection.activeObject;

if (selectedObject == null)

{

return;

}

Object[] roots = new Object[]{ selectedObject };

var objs = EditorUtility.CollectDependencies(roots);

string path = AssetDatabase.GetAssetPath(selectedObject);

var objs2 = AssetDatabase.GetDependencies(path);

foreach (var objin objs)

{

Debug.Log(obj.GetType().Name);

}

}

}

}

#endif

不得不說,Unity Editor 提供的默認的依賴查找的功能好弱,包括反向依賴關系,引用關系丟失等功能。或許我們可以利用這些接口自己做壹個好用點的依賴關系查找插件。

  • 上一篇:绲傛サ鍗?鍩熼亰鎴茬郴鍒?
  • 下一篇:如何使用redis做mysql的緩存
  • copyright 2024編程學習大全網