我们为自定义shader编写ShaderGUI可以很好的管理shader属性,拥有较好的交互体验。
但是狗蛋代价是……它并不通用,我们不会为零散的shader单独编写GUI,大部分时间都是使用Unity提供的内置UI效果。
那么当你想要低成本编写UI界面并且一劳永逸的话,MaterialPropertyDrawer会是不错的选择。
Properties
{
_MainTex("MainTex",2D) = ""{}
}
这是一个基本的RT界面,包含贴图,Tiling Offset信息。我们仿照URP Lit Shader的样子开始编写一个简单的Drawer脚本
//首先继承自MaterialPropertyDrawer类,类名不写Drawer也可以会自动添加,在shader[]中填写
internal class SingleLineTexDrawer : MaterialPropertyDrawer
{
private string Keyword;
//无参 [SingleLineTex]
public SingleLineTexDrawer() { }
//有参[SingleLineTex(keyword)]
public SingleLineTexDrawer(string Keyword)
{
this.Keyword = Keyword;
}
//控制UI高度
public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
if (prop.textureValue != null)
{
height = base.GetPropertyHeight(prop, label, editor);
}
else
{
height = 0;
}
return height;
}
//重写OnGUI方法
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
//当我们定义的属性是贴图时起作用
if (prop.type == MaterialProperty.PropType.Texture)
{
//绘制UI
editor.TexturePropertySingleLine(label, prop, subProp);
//如果没有贴图,不显示Tiling Offset,节省位置
if (prop.textureValue != null)
{
EditorGUI.indentLevel += 2;
editor.TextureScaleOffsetProperty(prop);
EditorGUI.indentLevel -= 2;
}
}
}
}
//TODO