Unity MaterialPropertyDrawer

我们为自定义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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值