//获取角色材质 MatDetail SetCharactersMaterial(Object select, Object character) { characterObj = PrefabUtility.InstantiatePrefab(character) as GameObject; if (characterObj == null) { return(null); } SkinnedMeshRenderer[] renderers = characterObj.GetComponentsInChildren <SkinnedMeshRenderer>(true); foreach (SkinnedMeshRenderer renderer in renderers) { Material[] mats = renderer.sharedMaterials; foreach (Material mat in mats) { string assetPath = AssetDatabase.GetAssetPath(select); if (assetPath == AssetDatabase.GetAssetPath(mat)) { MatDetail detail = new MatDetail(); detail.assetPath = assetPath; detail.mat = mat; detail.type = "Character"; detail.hierarcyPath = GetHierarcyPath(renderer.gameObject); return(detail); } } } return(null); }
//设置并获取MatDetail MatDetail GetMatDetail(Renderer[] renderers, Object select, string scenePath) { foreach (Renderer renderer in renderers) { //获取材质 Material[] mats = renderer.sharedMaterials; foreach (Material mat in mats) { string assetPath = AssetDatabase.GetAssetPath(select); if (assetPath == AssetDatabase.GetAssetPath(mat)) { MatDetail detail = new MatDetail(); detail.assetPath = assetPath; detail.mat = mat; detail.type = scenePath; detail.hierarcyPath = GetHierarcyPath(renderer.gameObject); return(detail); } } } return(null); }
//开始查找引用 void OnCheckReferences() { Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); if (objs == null || objs.Length <= 0) { return; } dictMat.Clear(); for (int i = 0; i < objs.Length; i++) { List <MatDetail> listDetail = new List <MatDetail>(); if (objs[i] is Material) { //遍历所有场景关联物体 if (isCheckScene) { if (EditorBuildSettings.scenes.Length <= 0) { Debug.LogError("你的可查找场景为空,请在Builder Setting中添加场景"); return; } foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) { if (scene.enabled) { EditorApplication.OpenScene(scene.path); if (objs[i] is Material) { MatDetail detail = SetMaterial(objs[i], scene.path); if (detail != null) { listDetail.Add(detail); } } } } } //遍历Resources特效目录 if (isCheckEffect) { Object[] effects = Resources.LoadAll("Effects"); //Resources下的地址,根据需求修改 for (int j = 0; j < effects.Length; j++) { if (objs[i] is Material) { MatDetail detail = SetEffectMaterial(objs[i], effects[j]); DestroyImmediate(effectObj); if (detail != null) { listDetail.Add(detail); } } } } //遍历角色目录 if (isCheckCharacter) { Object[] characters = GetAssetsOfType(Application.dataPath + "/Characters/", typeof(GameObject), ".prefab"); //自定义地址,根据需求修改 for (int j = 0; j < characters.Length; j++) { if (objs[i] is Material) { MatDetail detail = SetCharactersMaterial(objs[i], characters[j]); DestroyImmediate(characterObj); if (detail != null) { listDetail.Add(detail); } } } } } //找不到关联的处理 if (listDetail.Count <= 0) { MatDetail detail = new MatDetail(); detail.mat = objs[i] as Material; detail.assetPath = AssetDatabase.GetAssetPath(objs[i]); listDetail.Add(detail); } dictMat.Add(objs[i].name, listDetail); } }