Skip to content

Instantly share code, notes, and snippets.

@Roland09
Created August 6, 2020 08:49
Show Gist options
  • Save Roland09/0b2adb6533e5097c6de34d598a8ae601 to your computer and use it in GitHub Desktop.
Save Roland09/0b2adb6533e5097c6de34d598a8ae601 to your computer and use it in GitHub Desktop.
Unity Material and Shader Logging
using System;
using UnityEditor;
using UnityEngine;
using static UnityEditor.ShaderUtil;
namespace DeveloperTools
{
public static class MaterialAnalysis
{
static readonly IFormatProvider Formatter = new System.Globalization.CultureInfo("en-US");
/// <summary>
/// Log material and shader data to the console.
/// </summary>
/// <param name="material"></param>
public static void LogMaterial(Material material)
{
Shader shader = material.shader;
string text = "";
#region Common
text += string.Format("Shader Name = {0}", shader.name);
text += "\n";
text += string.Format("Render Queue = {0}", shader.renderQueue);
text += "\n";
#endregion Common
#region Shader Keywords
text += "Shader Keywords";
text += "\n";
for (int i = 0; i < material.shaderKeywords.Length; i++)
{
text += string.Format("{0}: {1}", i, material.shaderKeywords[i]);
text += "\n";
}
#endregion Shader Keywords
#region Properties
text += "Properties";
text += "\n";
for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); i++)
{
string propertyName = ShaderUtil.GetPropertyName(shader, i);
ShaderPropertyType propertyType = (ShaderPropertyType)ShaderUtil.GetPropertyType(shader, i);
string propertyDescription = ShaderUtil.GetPropertyDescription(shader, i);
string value;
switch (propertyType)
{
case ShaderPropertyType.Color: // The property holds a Vector4 value representing a color.
value = string.Format(Formatter, "{0}", material.GetColor(propertyName));
break;
case ShaderPropertyType.Vector: // The property holds a Vector4 value.
value = string.Format(Formatter, "{0}", material.GetVector(propertyName));
break;
case ShaderPropertyType.Float: // The property holds a floating number value.
value = string.Format(Formatter, "{0}", material.GetFloat(propertyName));
break;
case ShaderPropertyType.Range: // The property holds a floating number value in a certain range.
value = string.Format(Formatter, "{0}", material.GetFloat(propertyName));
break;
case ShaderPropertyType.TexEnv: // The property holds a Texture object.
value = material.GetTexture(propertyName) == null ? "null" : string.Format(Formatter, "{0}", material.GetTexture(propertyName).dimension);
break;
default:
value = "<undefined>";
break;
}
text += string.Format("{0}: {1} = {2} ({3}, {4})", i, propertyName, value, propertyType, propertyDescription);
text += "\n";
}
#endregion Properties
#region Shader Passes
text += "Shader Passes";
text += "\n";
for (int i = 0; i < material.passCount; i++)
{
text += string.Format("{0}: {1} = {2}", i, material.GetPassName(i), material.GetShaderPassEnabled(material.GetPassName(i)));
text += "\n";
}
#endregion Shader Passes
Debug.Log(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment