Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Steven-Walker · Feb 15, 2011 at 01:53 AM · shadermaterial

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
8

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);
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
4

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;
        }
    }
}
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 10, 2011 at 01:21 AM 0
Share

+1. I haven't tried this myself but nice approach on solving the problem!

avatar image yoyo · Mar 10, 2011 at 01:23 AM 0
Share

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.

avatar image gorbi_one · Aug 19, 2014 at 03:54 PM 0
Share

Seems not working if the renderer is part of a prefab : give me the whole list of textures used by the prefab..

avatar image HenryStrattonFW · Nov 20, 2014 at 05:12 PM 0
Share

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.

avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Phoera · Oct 19, 2016 at 12:21 AM 0
Share

But this does not actually get a texture, just its description, name, type

alt text

avatar image
-1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steven-Walker · Feb 16, 2011 at 04:03 PM 0
Share

See UPDATE above.

avatar image
-2

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#.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steven-Walker · Feb 16, 2011 at 04:03 PM 0
Share

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.

avatar image Tenebrous · Sep 03, 2012 at 02:31 PM 3
Share

Don't ever be sorry for c#.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges