Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Material Collector?

Discussion in 'Scripting' started by mcbauer, May 27, 2016.

  1. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    495
    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.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you should be able to get a complete list of renderers using FindObjectsOfType<Renderer>()... from there you should be able to access the materials of each, can't think of anything else that would be using a material...
     
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Could always make a custom window with a list of all the SharedMaterials in the scene.

    Something like:
    Code (CSharp):
    1. private Material SharedMaterials;
    2.  
    3. for(materials in scene)
    4. if(material not in list)
    5.      Add material to list();
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    I'm not in front of my unity dev machine, but couldn't you just do 'FindObjectsOfType<Material>()'?

    I'll test when I get home...
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    Wouldn't you still need to get all the Renderers first, since that is the sharedMaterials of that Renderer, not all Renderers.
     
  6. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    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.
     
  7. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    495
    Yes you are correct mafiadude1.

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

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    495
    Code (CSharp):
    1. private Material SharedMaterials;
    2. for(materials in scene)
    3. if(material not in list)
    4.      Add material to list();
    I get this concept, but not the implementation
     
  9. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Basically, you first want to create a custom window that holds your materials:
    Code (csharp):
    1.  
    2. public class Example : EditorWindow
    3. {
    4.      List<Material> SharedMaterials;
    5. }
    6.  
    from there, you can add/remove materials to the list as the heirarchy is changed:

    Code (csharp):
    1.  
    2. static void Example ()
    3. {
    4.      EditorApplication.hierarchyWindowChanged += ExampleCallback;
    5. }
    6. static void ExampleCallback ()
    7. {
    8.      Object[] all = Resources.FindObjectsOfTypeAll (typeof (Object));
    9.      Debug.Log ("There are " + all.Length + " objects at the moment.");
    10. }
    11.  
    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.
     
    Last edited: May 27, 2016
  10. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    495
    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.
     
  11. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    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.