编写GUI面板
1. 新建GUI子面板
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEditor.Rendering.Universal.ShaderGUI
{
internal class CP_XXXOutLineGUI
{
public static class Styles{}
public struct LitProperties
{
public LitProperties(MaterialProperty[] properties){}
}
public static void DoDetailArea(LitProperties properties, MaterialEditor materialEditor){}
public static void SetMaterialKeywords(Material material){}
}
}
2. 将在Shader关联的GUI面板中注册该子面板
internal class CP_XXXLitShader : BaseShaderGUI
{
//声明子面板属性
private CP_XXXOutLineGUI.LitProperties litXXXOutLineProperties;
public override void FillAdditionalFoldouts(MaterialHeaderScopeList materialScopesList)
{
}
public override void FindProperties(MaterialProperty[] properties)
{
base.FindProperties(properties);
// 新建子面板属性
litXXXOutLineProperties = new CP_XXXOutLineGUI.LitProperties(properties);
}
}
3. 注册列表头部展开区域
FillAdditionalFoldouts(MaterialHeaderScopeList)
会在继承BaseShaderGUI
的类中由系统自动调用,它传入当前材质GUI的列表实例,我们通过RegisterHeaderScope
可以添加新的列表项目。
public override void FillAdditionalFoldouts(MaterialHeaderScopeList materialScopesList)
{
materialScopesList.RegisterHeaderScope(LitDetailGUI.Styles.detailInputs, Expandable.Details, _ => LitDetailGUI.DoDetailArea(litDetailProperties, materialEditor));
}
注册函数:materialScopesList.RegisterHeaderScope<TEnum>(GUIContent, TEnum, Action<Material>)
(1)创建头部列表GUIContext对象
public GUIContent(...);
- string text 文本
- Texture image 图片
- string tooltip 鼠标指针当前悬停在其上或具有键盘焦点的控件的工具提示。
internal class CP_XXXOutLineGUI{
public static class Styles
{
public static readonly GUIContent OutLineInputs = new GUIContent("Outline Inputs",
"This Let You Open The OutLine Panel");
}
}
(2)设置掩码
掩码设置要求,掩码按照位来设置,每个掩码占一个位字节。
如:在URP内置代码BaseShaderGUI:ShaderGUI
类中已经设置了部分掩码
[URPHelpURL("shaders-in-universalrp")]
protected enum Expandable
{
SurfaceOptions = 1 << 0,
SurfaceInputs = 1 << 1,
Advanced = 1 << 2,
Details = 1 << 3,
}
其中SurfaceOptions、SurfaceInputs、Advanced 都已被系统注册,在URPShader的LitShader(其他Shader中可能也有)中,Details也在重写函数FillAdditionalFoldouts中被注册
materialScopesList.RegisterHeaderScope(
LitDetailGUI.Styles.detailInputs,
Expandable.Details,
_ => LitDetailGUI.DoDetailArea(litDetailProperties, materialEditor)
);
我们仿照URP标准库中的写法,添加自定义掩码的扩展
在CP_XXXLitShader中自定义
protected enum AdditionalExpandable
{
...其他掩码(16、32、64、128、256)
OutlineMask = 512,
}
(3) 注册列表头部区域
public override void FillAdditionalFoldouts(MaterialHeaderScopeList materialScopesList)
{
materialScopesList.RegisterHeaderScope(
CP_XXXOutLineGUI.Styles.OutLineInputs,
AdditionalExpandable.OutlineMask,
_ => CP_XXXOutLineGUI.DoDetailArea(litXXXOutLineProperties, materialEditor));
}
现在返回Unity,查看Shader列表。发现已经有了列表头显示。
4. 添加属性列表的显示
首先OutLine需要的参数有OutLineColor和OutLineSize。
首先在LitProperties中声明该属性,并绑定该属性的Shader名称。
public struct LitProperties
{
public MaterialProperty OutLineColor;
public MaterialProperty OutLineSize;
public LitProperties(MaterialProperty[] properties)
{
OutLineColor = BaseShaderGUI.FindProperty("_OutlineColor", properties, false);//false-->未找到不抛出异常
OutLineSize = BaseShaderGUI.FindProperty("_OutlineSize", properties, false);
}
}
当展开OutLine下拉框时:调用public static void DoDetailArea(LitProperties properties, MaterialEditor materialEditor)
函数
public static void DoDetailArea(LitProperties properties, MaterialEditor materialEditor)
{
materialEditor.ColorProperty(properties.OutLineColor, Styles.OutLineColorContent.text);
materialEditor.FloatProperty(properties.OutLineSize, Styles.OutLineSizeContent.text);
}
展开显示
修改属性,Shader中参数被修改。