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 nicloay · Mar 28, 2013 at 11:44 AM · c#shadermaterial

How to catch Material doesn't have a texture property exception

Hi everyone, I have following code

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
 using System;   
 public class UFTMaterialUtil {    
     public static List<Texture> getTextures(Material material){
         Shader shader=material.shader;            
         List<Texture> textures=new List<Texture>();
         for (int i = 0; i < ShaderUtil.GetPropertyCount(shader) ; i++) {                        
             if (ShaderUtil.GetPropertyType(shader,i) == ShaderUtil.ShaderPropertyType.TexEnv){
                 string propertyName=ShaderUtil.GetPropertyName(shader,i);
                 try {                    
                     textures.Add( material.GetTexture(propertyName));                        
                 } catch (Exception ex) {
                     Debug.Log("EXCEPTION_________"+ex);
                 }                
             }            
         }
         return textures;
     }    
 }
 


the problem is here (row14) materials.add( material.GetTexture(propertyName)); if material has property, assume _MainTex, but non of texture is assigned to it, Unity instead of catch exception and show log, show this error

Material doesn't have a texture property '_MainTex'

UnityEngine.Material:GetTexture(String) UFTMaterialUtil:getTextures(Material) (at Assets/UFTAtlasEditor/Resources/Scripts/Utils/UFTMaterialUtil.cs:25) UFTAtlasMigrateWindow:getMaterialListByAtlasMetadata(UFTAtlasMetadata) (at Assets/UFTAtlasEditor/Editor/UFTAtlasMigrateWindow.cs:113) UFTAtlasMigrateWindow:displayUpdateLinksToTextureMaterial() (at Assets/UFTAtlasEditor/Editor/UFTAtlasMigrateWindow.cs:90) UFTAtlasMigrateWindow:OnGUI() (at Assets/UFTAtlasEditor/Editor/UFTAtlasMigrateWindow.cs:71) UnityEditor.DockArea:OnGUI()
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fffMalzbier · Mar 28, 2013 at 12:14 PM

You could use material.HasProperty()

  if (renderer.material.HasProperty(propertyName))
      textures.Add(material.GetTexture(propertyName));   

Comment
Add comment · Show 3 · 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 nicloay · Mar 28, 2013 at 02:11 PM 0
Share

I tried this method before, but unfortunately it won't help, because HasProperty() return every time.

this method is not the same as IsPropertySet which is missed.

avatar image nicloay · Mar 29, 2013 at 10:04 AM 0
Share

I found what was a problem I took all materials in the project by following code

 $$anonymous$$aterial[] all$$anonymous$$aterials=($$anonymous$$aterial[])Resources.FindObjectsOfTypeAll(typeof($$anonymous$$aterial));

And then i used my method to find information about textures.

But there was many different materials with unknown shaders. So i used following code to check that asset is $$anonymous$$e

         string assetPath=AssetDatabase.GetAssetPath(material);        
         if (String.IsNullOrEmpty(assetPath))
             return textures;

After that I havn't seen this error anymore. and material.GetTexture() return null in case if non of texture assigned to shader texture property

so. the final method body is this

     public static Dictionary<string,Texture> getTextures($$anonymous$$aterial material){        
         Shader shader=material.shader;            
         Dictionary<string,Texture> textures=new Dictionary<string, Texture>();
         string assetPath=AssetDatabase.GetAssetPath(material);        
         if (String.IsNullOrEmpty(assetPath))
             return textures;
         
         for (int i = 0; i < ShaderUtil.GetPropertyCount(shader) ; i++) {                        
             if (ShaderUtil.GetPropertyType(shader,i) == ShaderUtil.ShaderPropertyType.TexEnv){                
                 string propertyName=ShaderUtil.GetPropertyName(shader,i);                
                 textures.Add(propertyName, material.GetTexture(propertyName));                                                    
             }            
         }        
         return textures;
     }
avatar image fffMalzbier · Aug 28, 2013 at 01:35 PM 0
Share

Thanks for sharing :)

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity Normal Problem Between Two Seperated Model 1 Answer

Special shader question. How to draw half an object with one shader and the other half with another? 1 Answer

Can I access the Material of an animated object via code? 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