Material Collector?

I need to scan through the scene and populate my script component with each material found that uses a particular shader.

What I want to have is a way to easily access each material, for edits / tweaks, in the scene, without having to select on the object or digging through the asset directories to find the materials. A material collector if you will.

you should be able to get a complete list of renderers using FindObjectsOfType()… from there you should be able to access the materials of each, can’t think of anything else that would be using a material…

Could always make a custom window with a list of all the SharedMaterials in the scene.

Something like:

private Material SharedMaterials;

for(materials in scene)
if(material not in list)
     Add material to list();

I’m not in front of my unity dev machine, but couldn’t you just do ‘FindObjectsOfType()’?

I’ll test when I get home…

Wouldn’t you still need to get all the Renderers first, since that is the sharedMaterials of that Renderer, not all Renderers.

Yes. But as far as I understand, the OP wanted to not have to click through the inspector to access them. Instead, he/she wants them in one place. But maybe I misunderstood.

Yes you are correct mafiadude1.

I want to see and adjust each material on the object with the script attached

private Material SharedMaterials;
for(materials in scene)
if(material not in list)
     Add material to list();

I get this concept, but not the implementation

Basically, you first want to create a custom window that holds your materials:

public class Example : EditorWindow
{
     List<Material> SharedMaterials;
}

from there, you can add/remove materials to the list as the heirarchy is changed:

static void Example ()
{
     EditorApplication.hierarchyWindowChanged += ExampleCallback;
}
static void ExampleCallback ()
{
     Object[] all = Resources.FindObjectsOfTypeAll (typeof (Object));
     Debug.Log ("There are " + all.Length + " objects at the moment.");
}

then, you can just add PropertyFields to display/edit them.

I assume (depending on your implementation) it’ll get pretty laggy when the object count gets ridiculous.

Does this have to be in an editor window? Can this be attached to the script on the game object?

I have an empty game object which runs this material collector script, and when I select on this object inside the script panel is where I find all of the materials in the scene using my shader.

If the editor window is the only way to go, I think it will still be effective.

It’s completely up to you whether or not you want it to be an editor window. It all depends on where you want to see this info and when you want to collect the data. Editor windows and inspectors are a great choice because they allow you to use PropertyFields to interact with your materials.