Hi guys, so I have this code:
public List<Texture> GetTextures(Material mat) {
List<Texture> list = new List<Texture>();
int count = ShaderUtil.GetPropertyCount(mat.shader);
for(var i = 0; i < count; i++) {
if(ShaderUtil.GetPropertyType(mat.shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
//if the texture is not null then works, but if its null, how do I know if its a Texture2D this property type?
if(mat.GetTexture(ShaderUtil.GetPropertyName(mat.shader, i)) is Texture2D)
Debug.Log("Tex2D!");
list.Add(mat.GetTexture(ShaderUtil.GetPropertyName(mat.shader, i)));
}
}
Debug.Log("Textures:" + list.Count + " total properties: " + count);
return list;
}
And Im wondering how do I know if a property is a texture2D or a cubemap if the texture assigned is null :S. Any idea? :S
really appreciated!
I’ve come up with this solution (that I dont like), anyone knows a better solution?
public List<Texture> GetTextures(Material mat) {
List<Texture> list = new List<Texture>();
int count = ShaderUtil.GetPropertyCount(mat.shader);
for(var i = 0; i < count; i++) {
if(ShaderUtil.GetPropertyType(mat.shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
string propertyName = ShaderUtil.GetPropertyName(mat.shader, i);
if(propertyName != "_Cube") {
list.Add(mat.GetTexture(propertyName));
}
}
}
Debug.Log("Textures:" + list.Count + " total properties: " + count);
return list;
}
UPDATE:
Her’s how to do it!:
public List<Texture> GetTextures2D(Material mat) {
List<Texture> list = new List<Texture>();
int count = ShaderUtil.GetPropertyCount(mat.shader);
for(int i = 0; i < count; i++) {
if(ShaderUtil.GetPropertyType(mat.shader, i) == ShaderUtil.ShaderPropertyType.TexEnv) {
if(ShaderUtil.GetTexDim(mat.shader, i) == ShaderUtil.ShaderPropertyTexDim.TexDim2D) {
list.Add(mat.GetTexture(ShaderUtil.GetPropertyName(mat.shader, i)));
}
}
}
Debug.Log("Textures:" + list.Count + " total properties: " + count);
return list;
}
I don’t know if it helps at all, but there are a few very basic C# method to work with the Types.
Type type = typeof(GameObject); //For when you need an easy way to get a Type, for calling functions, for example.
Type type2 = "This is my string object".GetType(); // gives you the type used by an instance of that type.
if (randomObject is int){ //Do stuff, my object is an int
double myIntAsDouble = randomObject as double; // An other way to cast types, with other constraints, use only if you understand it well, as it doesn't throw exceptions when it fails.
}
I hope it helps a bit anyway, with all the Type-centered questions, because it’s a bit unknown and hidden, but can help a lot.
As a note, my int to double, I have no clue if it workd or not, and you won’t either until you try to use that “myIntAsDouble” variable ;).
indeed, the issue here is that these types reside in shaders lang, so basically you need a class that interfaces with shaderlab (ShaderUtil) and lets you know what type the var is because when you lets say Get a texture (Material.GetTexture) it will return you a texture (if there is a texture assigned in the inspector in the shader), now if there is no texture it will return null, but I need to know if that null returned is a cubemap or a texture2D!.
The 3rd post is a solution to this :).
Being said that, still thanks a lot!
at least someone replied me 