1首先要遍历所有的资源
过滤出.mat的资源,也就是Material,然后通过Shader名来查找对应的引用的材质,然后可以将这些路径名打出来。
改进
打印出所有的路径名还要自己去找不是很方便,后面将它输出到面板上,并且可以快速定位,用到了编辑器开发,editorlayout,
核心逻辑
EditorGUILayout.ObjectField("", go, typeof(Material), false);
最后因为路径太长需要修改左侧的长度,后面引入了GUIContent作为第一个参数,然后以为需要长度长一点,
引入逻辑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的总数是---------------"+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下面,没有就创建一个
网友评论