1首先要遍歷所有的資源
過濾出.mat的資源,也就是Material,然后通過Shader名來查找對應的引用的材質(zhì)殊橙,然后可以將這些路徑名打出來。
改進
打印出所有的路徑名還要自己去找不是很方便膨蛮,后面將它輸出到面板上,并且可以快速定位敞葛,用到了編輯器開發(fā),editorlayout,
核心邏輯
EditorGUILayout.ObjectField("", go, typeof(Material), false);
最后因為路徑太長需要修改左側的長度惹谐,后面引入了GUIContent作為第一個參數(shù)驼卖,然后以為需要長度長一點氨肌,
引入邏輯EditorGUIUtility.labelWidth = 400酌畜,設置他的長度。
最后上代碼
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Threading;
public class SearchShader :EditorWindow
{
// public static string FilePath = "Assets/Materials";
public static string FilePath = "Assets";
//搜索固定文件夾中的所有Material的路徑
public static List<string> listMatrials;
public static List<string> listTargetMaterial;
public static string selectedShaderName;
public static StringBuilder sb;
static public SearchShader instance;
public Dictionary<string, BetterList<string>> dict;
public BetterList<string> allPath;
Vector2 mScroll = Vector2.zero;
void OnEnable()
{
// instance = this;
}
void OnDisable()
{
// instance = null;
}
void OnGUI()
{
if (listTargetMaterial == null)
{
return;
}
mScroll = GUILayout.BeginScrollView(mScroll);
List<string> list = listTargetMaterial;
if (list != null && list.Count > 0)
{
if (NGUIEditorTools.DrawHeader("Mat"))
{
int count = 0;
foreach (string item in list)
{
// Debug.Log("item ----------------"+item);
count += 1;
Material go = AssetDatabase.LoadAssetAtPath(item, typeof(Material)) as Material;
GUIContent label1 = new GUIContent(count+"---"+item,item);
EditorGUIUtility.labelWidth = EditorGUIUtility.currentViewWidth * 0.6f;
EditorGUILayout.ObjectField(label1, go, typeof(Material), false);
}
}
// Debug.Log("當前的窗體的的寬度是----------------------------------"+EditorGUIUtility.currentViewWidth);
list = null;
}
GUILayout.EndScrollView();
}
[MenuItem("Assets/SearchShader", true)]
private static bool OptionSelectAvailable()
{
if(Selection.activeObject == null)
{
return false;
}
return Selection.activeObject.GetType() == typeof(Shader);
}
[MenuItem("Assets/SearchShader")]
private static void SearchConstantShader()
{
Debug.Log("當前選中的Shader名字:" + Selection.activeObject.name);
sb = new StringBuilder();
selectedShaderName = Selection.activeObject.name;
listMatrials = new List<string>();
listMatrials.Clear();
listTargetMaterial = new List<string>();
listTargetMaterial.Clear();
//項目路徑 eg:projectPath = D:Project/Test/Assets
string projectPath = Application.dataPath;
//eg:projectPath = D:Project/Test
projectPath = projectPath.Substring(0, projectPath.IndexOf("Assets"));
try
{
//獲取某一文件夾中的所有Matrial的Path信息
GetMaterialsPath(projectPath, FilePath, "Material",ref listMatrials);
}
catch(System.Exception e)
{
Debug.LogError(e);
}
for (int i = 0; i < listMatrials.Count; i++)
{
EditorUtility.DisplayProgressBar("Check Materials", "Current Material :"
+ i + "/" + listMatrials.Count,(float)i/ listMatrials.Count);
try
{
//開始Check
BegainCheckMaterials(listMatrials[i]);
}
catch (System.Exception e)
{
EditorUtility.ClearProgressBar();
Debug.LogError(e);
}
}
// PrintToTxt();
//生成界面
EditorUtility.ClearProgressBar();
EditorWindow.GetWindow<SearchShader>(false, "SearchShader", true).Show();
Debug.Log("Check Success");
}
//獲取某一文件夾中的所有Matrial的Path信息
public static void GetMaterialsPath(string projectPath,string targetFilePath,string searchType,ref List<string> array)
{
if (Directory.Exists(targetFilePath))
{
string[] guids;
//搜索
guids = AssetDatabase.FindAssets("t:" + searchType, new[] { targetFilePath });
foreach (string guid in guids)
{
string source = AssetDatabase.GUIDToAssetPath(guid);
string[] allStr = source.Split('.');
// if(allStr[allStr.Length-1] == "mat")
if(source.EndsWith(".mat"))
{
listMatrials.Add(source);
// Debug.Log("當前的material------------"+source);
}
}
// Debug.Log("當前material的總數(shù)是---------------"+listMatrials.Count);
}
}
//開始檢查Material
public static void BegainCheckMaterials(string materialPath)
{
Material mat = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
if (mat.shader.name == selectedShaderName)
{
listTargetMaterial.Add(materialPath);
}
}
public static void PrintToTxt()
{
//加入shader的名字
listTargetMaterial.Add(selectedShaderName);
FileInfo fi = new FileInfo(Application.dataPath + "/Materials.txt");
if (!fi.Exists)
{
// fi.CreateText();
// fi.OpenWrite();
// Thread.Sleep(1000);
var stream = fi.Create();
stream.Dispose();
}
// else
{
StreamWriter sw = new StreamWriter(Application.dataPath + "/Materials.txt");
for (int i = 0; i < listTargetMaterial.Count - 1; i++)
{
sb.Append(listTargetMaterial[i] + "\n");
}
string useNum = string.Format("共有 {0} 個Material用到:{1}", listTargetMaterial.Count - 1, selectedShaderName);
sb.Append(useNum + "\n");
sb.Append("用到的shader名字為:" + selectedShaderName);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
ps:腳本放在Assets下面的Editor下面,沒有就創(chuàng)建一個