How do you programmatically extract the textures and other properties of an arbitrary Unity material?
Ideally runtime - I'm trying to upload the data to the server for remote access.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityHow do you programmatically extract the textures and other properties of an arbitrary Unity material?
Ideally runtime - I'm trying to upload the data to the server for remote access.
I think what you are looking for are the methods of Shader to get information about the properties and then as mentioned in the comments, use the methods of Material to get the data assigned to those properties (if any).
Below an example test where I check if the property is a texture, and log it if it is.
public class Example : MonoBehaviour
{
public Material material;
public void Start()
{
Shader shader = material.shader;
for (int i = 0; i < shader.GetPropertyCount(); i++)
{
string propertyName = shader.GetPropertyName(i);
var propertyType = shader.GetPropertyType(i);
if (propertyType == UnityEngine.Rendering.ShaderPropertyType.Texture)
{
var texture = material.GetTexture(propertyName);
Debug.Log(propertyName + " : " + texture?.name, texture);
}
}
}
}
texture == null
, texture?.name
will return null instead of throw an error complaining texture is null. It is the same as this code: (texture == null ? null : texture.name)
which would be similar to if(texture==null){return null;}else{return texture.name;}
.
\$\endgroup\$
– troien
Oct 28 '20 at 13:08