- Home /
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.
Answer by jobesu · Dec 22, 2015 at 10:42 AM
List<Texture> allTexture = new List<Texture>();
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);
}
}
Answer by yoyo · Mar 10, 2011 at 01:16 AM
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;
}
}
}
+1. I haven't tried this myself but nice approach on solving the problem!
I wish Unity had a built in "list all things that depend on this" feature, e.g. select an asset (like a texture) and see who's using it.
Seems not working if the renderer is part of a prefab : give me the whole list of textures used by the prefab..
Just used this little snippet in a simple editor tool to find all materials that reference a given texture, works a charm if you provide it a material ins$$anonymous$$d of a renderer.
Answer by wibble82 · Dec 22, 2015 at 10:26 AM
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:
http://docs.unity3d.com/ScriptReference/ShaderUtil.html
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 :)
-Chris
But this does not actually get a texture, just its description, name, type
Answer by Jessy · Feb 15, 2011 at 02:44 AM
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.
Answer by tertle · Feb 15, 2011 at 03:22 AM
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#.
This only finds the main texture. It needs to also account for any other 2D property in the shader. Certainly I can search for each type "_Bump", etc, but I was trying to find a more general way to get all 2D texture properties.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104436im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Using Color.Lerp with Lightweight Render Pipeline 1 Answer
Set unity_GUIZTestMode in UI Shaders? 0 Answers
Mobile\Vertex Colored shader 0 Answers
How can i change detail texture? 1 Answer