浏览代码

Output shader errors to console when uber shader has error

/main
Peter Bay Bastian 7 年前
当前提交
1a6d34eb
共有 2 个文件被更改,包括 53 次插入5 次删除
  1. 46
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs
  2. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs

46
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs


using System;
using UnityEditor;
using UnityEditor.Graphing;
public class MaterialGraphAsset
static class MaterialGraphAsset
{
public static bool ShaderHasError(Shader shader)
{

}
public struct ShaderError
{
public string message;
public string messageDetails;
public string platform;
public string file;
public int line;
public int warning;
}
static MethodInfo s_GetErrorsCall = typeof(ShaderUtil).GetMethod("GetShaderErrors", BindingFlags.Static | BindingFlags.NonPublic);
static Type s_ShaderErrorType = typeof(ShaderUtil).Assembly.GetType("UnityEditor.ShaderError");
static FieldInfo s_ShaderErrorMessageField = s_ShaderErrorType.GetField("message", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorMessageDetailsField = s_ShaderErrorType.GetField("messageDetails", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorPlatformField = s_ShaderErrorType.GetField("platform", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorFileField = s_ShaderErrorType.GetField("file", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorLineField = s_ShaderErrorType.GetField("line", BindingFlags.Instance | BindingFlags.Public);
static FieldInfo s_ShaderErrorWarningField = s_ShaderErrorType.GetField("warning", BindingFlags.Instance | BindingFlags.Public);
public static ShaderError[] GetShaderErrors(Shader shader)
{
var invoke = s_GetErrorsCall.Invoke(null, new object[] { shader });
var objects = (Array)invoke;
var errors = new ShaderError[objects.Length];
for (var i = 0; i < objects.Length; i++)
{
var obj = objects.GetValue(i);
errors[i] = new ShaderError
{
message = (string)s_ShaderErrorMessageField.GetValue(obj),
messageDetails = (string)s_ShaderErrorMessageDetailsField.GetValue(obj),
platform = (string)s_ShaderErrorPlatformField.GetValue(obj),
file = (string)s_ShaderErrorFileField.GetValue(obj),
line = (int)s_ShaderErrorLineField.GetValue(obj),
warning = (int)s_ShaderErrorWarningField.GetValue(obj),
};
}
return errors;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using UnityEditor.Graphing.Util;
using UnityEngine;

m_UberShaderString = m_Graph.GetUberPreviewShader(m_UberShaderIds, out m_OutputIdProperty);
ShaderUtil.UpdateShaderAsset(m_UberShader, m_UberShaderString);
File.WriteAllText(Application.dataPath + "/../UberShader.shader", (m_UberShaderString ?? "null").Replace("UnityEngine.MaterialGraph", "Generated"));
var message = "RecreateUberShader: " + Environment.NewLine + m_UberShaderString;
Debug.LogWarning(message);
var errors = MaterialGraphAsset.GetShaderErrors(m_UberShader);
var message = new ShaderStringBuilder();
message.AppendLine("Uber shader has {0} error{1}:", errors.Length, errors.Length != 1 ? "s" : "");
foreach (var error in errors)
{
message.AppendLine("{0} at line {1} (on {2})", error.message, error.line, error.platform);
message.AppendLine(error.messageDetails);
}
Debug.LogWarning(message.ToString());
ShaderUtil.ClearShaderErrors(m_UberShader);
ShaderUtil.UpdateShaderAsset(m_UberShader, k_EmptyShader);
uberShaderHasError = true;

正在加载...
取消
保存