廢話少說,直接上代碼。
using UnityEditor;
using UnityEngine;
public class FindDependencies : MonoBehaviour
{
static bool m_bIsSaveFile = false;
static TextWriteHelper m_szMaterialList = new TextWriteHelper();
static TextWriteHelper m_szPrefabList = new TextWriteHelper();
[MenuItem("Assets/FindImageDependencies")]
static void FindDepend()
{
m_szMaterialList.ClearData();
string imagePath = GetObjPath(); // "Assets/Path/To/Your/Image.png";
string[] guids = AssetDatabase.FindAssets("t:Material");
foreach (string guid in guids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
Material material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
if (material != null && HasTexture(material, imagePath))
{
Debug.Log(string.Format("Material referencing the image: {0}, and path = {1}", material.name, assetPath));
//m_szMaterialList.AddContent(string.Format("Material referencing the image: {0}, and path = {1}", material.name, assetPath));
FindPrefab(assetPath);
}
}
if (m_bIsSaveFile)
{
m_szMaterialList.SaveFile("MaterialReferenceList");
m_szPrefabList.SaveFile("PrefabReferenceList");
}
}
static string GetObjPath()
{
string path = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
return path;
}
static bool HasTexture(Material material, string texturePath)
{
SerializedObject serializedMaterial = new SerializedObject(material);
SerializedProperty texturesProperty = serializedMaterial.FindProperty("m_SavedProperties.m_TexEnvs");
for (int i = 0; i < texturesProperty.arraySize; i++)
{
SerializedProperty textureProperty = texturesProperty.GetArrayElementAtIndex(i);
SerializedProperty textureValueProperty = textureProperty.FindPropertyRelative("second.m_Texture");
if (textureValueProperty != null)
{
Texture texture = textureValueProperty.objectReferenceValue as Texture;
if (texture != null && AssetDatabase.GetAssetPath(texture) == texturePath)
{
return true;
}
}
}
return false;
}
static void FindPrefab(string materialPath)
{
//string materialPath = "Assets/Path/To/Your/Material.mat";
string[] guids = AssetDatabase.FindAssets("t:Prefab");
foreach (string guid in guids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
if (prefab != null && HasMaterialReference(prefab, materialPath))
{
Debug.Log(string.Format("Prefab referencing the material: {0},and prefab path ={1} ", prefab.name, assetPath));
//m_szPrefabList.AddContent(string.Format("Prefab referencing the material: {0},and prefab path ={1} ", prefab.name, assetPath));
}
}
}
static bool HasMaterialReference(GameObject gameObject, string materialPath)
{
Renderer[] renderers = gameObject.GetComponentsInChildren<Renderer>(true);
foreach (Renderer renderer in renderers)
{
foreach (Material material in renderer.sharedMaterials)
{
if (material != null && AssetDatabase.GetAssetPath(material) == materialPath)
{
return true;
}
}
}
return false;
}
}
上述代碼中,我們首先使用AssetDatabase.FindAssets
方法通過過濾類型("t:Material")獲取所有材質(zhì)球的GUID。然后遍歷每個GUID,加載對應(yīng)的材質(zhì)球,判斷該材質(zhì)球是否引用了指定的圖片。我們定義了一個輔助方法HasTexture
來檢查材質(zhì)球中的紋理是否引用了目標(biāo)圖片,通過檢查SerializedProperty來判斷紋理是否匹配。
上述代碼中,我們首先使用AssetDatabase.FindAssets
方法通過過濾類型("t:Prefab")獲取所有預(yù)設(shè)的GUID。然后遍歷每個GUID,加載對應(yīng)的預(yù)設(shè),并判斷該預(yù)設(shè)是否引用了指定的材質(zhì)球。我們定義了一個輔助方法HasMaterialReference
來檢查預(yù)設(shè)及其子物體的渲染組件是否使用了目標(biāo)材質(zhì)球,通過遍歷渲染組件的sharedMaterials
數(shù)組來進行匹配。文章來源:http://www.zghlxwxcb.cn/news/detail-632526.html
綜上所訴,我們也可以通過相似的方式去查找其他類型的資源引用。好了大功告成。。。文章來源地址http://www.zghlxwxcb.cn/news/detail-632526.html
到了這里,關(guān)于Unity項目中查找所有使用某一張圖片的材質(zhì)球,再查找所有使用材質(zhì)球的預(yù)設(shè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!