Get all textures from a material?

I have an editor script that looks at the objects materials and extracts from them texture information. I'd like for the script to be able to get information on all 2D textures. Presently I've only been able to use sharedMaterial.GetTexture("_MainTex"), where I have to call each texture channel by name directly. Some shaders though specify custom channel names. Is there a way to do this without having to manually add each channel name to the script?

I'm thinking something like this:

var texs : Texture2D[] = obj.renderer.sharedMaterial.GetTextures();

Thanks for any input. Walker

UPDATE: What I'm doing is listing all of the textures from a selected set of objects. My goal is to retrieve all unique textures, whether they are _MainTex, _Bump, etc. With this list I'm displaying a reference to each texture with a concise report of the dimensions, mip level and import settings. I was hoping I could make this tool work generally for all future shaders that may have custom 2D properties.

List allTexture = new List();
Shader shader = obj.renderer.sharedMaterial.shader;
for(int i=0; i<ShaderUtil.GetPropertyCount(shader); i++) {
if(ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
Texture texture = obj.renderer.sharedMaterial.GetTexture(ShaderUtil.GetPropertyName(shader, i));
allTexture.Add(texture);
}
}

If you know the renderer that the material is attached to you could do this (C#) ...

IEnumerable<Texture> GetTextures(Renderer renderer)
{
    foreach (Object obj in EditorUtility.CollectDependencies(new UnityEngine.Object[] {renderer}))
    {
        if (obj is Texture)
        {
            yield return obj as Texture;
        }
    }
}

Not entirely sure why this has popped to the top again, but just in case others need the answer, unity contains this utility these days:

It allows you to get a list of all the parameters a shader contains, so you can:

  • Get the shader from the material
  • Get a list of texture parameter names from the shader
  • Read the value of each parameter from the material

Hope that helps others with the same question :slight_smile:

-Chris

No, nothing that suits your needs exists yet.

My recommendation is to only use your own shaders; then you'll always know all of the property names.

Not sure if this is what you want, but should get all textures on an object.

List<Texture2D> l = new List<Texter2D>();

foreach(var m in renderer.materials)
{
      l.add(m.mainTexture);
}

Sorry for c#.