Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active January 30, 2017 13:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/366e0cb8069146be1eb0 to your computer and use it in GitHub Desktop.
Save tsubaki/366e0cb8069146be1eb0 to your computer and use it in GitHub Desktop.
シェーダーを使用しているマテリアルの一覧を取得
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Linq;
public class SearchMaterialUseShader : EditorWindow
{
const string menuItemName = "Assets/Find/shader <- material";
[MenuItem(menuItemName, false, 53)]
static void Init()
{
var window = SearchMaterialUseShader.CreateInstance<SearchMaterialUseShader> ();
window.CollectionFileReferenceList();
window.Search(Selection.activeObject);
window.Show ();
}
[MenuItem (menuItemName, true)]
static bool ValidateLogSelectedTransformName () {
if( Selection.activeObject == null ){
return false;
}
var path = AssetDatabase.GetAssetPath(Selection.activeObject);
return Path.GetExtension(path) == ".shader";
}
Vector2 scrollPos;
string[] searchResults;
void OnGUI()
{
using( var horizonal = new EditorGUILayout.HorizontalScope()){
if( GUILayout.Button("select all material", EditorStyles.toolbarButton)){
Selection.objects = referencedByList
.Select( c => AssetDatabase.LoadAssetAtPath<Object>(c))
.ToArray();
}
}
using( var scroll = new EditorGUILayout.ScrollViewScope(scrollPos) ){
scrollPos = scroll.scrollPosition;
foreach( var filereference in referencedByList ){
using( var horizonal = new EditorGUILayout.HorizontalScope())
{
var icon = AssetDatabase.GetCachedIcon (filereference);
GUILayout.Label (icon, GUILayout.Width (20), GUILayout.Height (20));
if( GUILayout.Button(filereference, EditorStyles.label) ){
Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(filereference);
}
}
}
}
}
List<Reference> fileReferenceList = new List<Reference>();
string[] referencedByList = new string[0]{};
void Search(Object target)
{
var path = AssetDatabase.GetAssetPath(target);
var guid = AssetDatabase.AssetPathToGUID(path);
referencedByList = fileReferenceList
.Where(c=> c.from != path)
.Where(c=> c.to == guid)
.Select(c=>c.from).ToArray();
}
void CollectionFileReferenceList()
{
fileReferenceList.Clear();
var allMaterialFiles = Directory.GetFiles("Assets", "*.mat", SearchOption.AllDirectories);
foreach( var materialFile in allMaterialFiles ){
var referenceList = AssetDatabase.GetDependencies(new string[]{materialFile});
foreach( var reference in referenceList){
fileReferenceList.Add( new Reference(){
from = materialFile,
to = AssetDatabase.AssetPathToGUID(reference)
});
}
}
}
class Reference
{
public string from;
public string to;
}
}
@tsubaki
Copy link
Author

tsubaki commented Jul 31, 2015

how to use

Porjectビューでシェーダーを選択して
右クリック -> find -> Material use shader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment