- Home /
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
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()Material doesn't have a texture property '_MainTex'
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));
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.
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613095244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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