UnityEditor编辑器扩展开发-自定义Shader入门

估计需要自定义Shader 的人不多

下面内容就看看

作为小白的我们,无从入手,当然首先看看 Amplify Shader Editor(ASE)是如何实现Shader定义,

从(ASE)的Shader代码,得知自定义原理(代码)

    //CustomEditor "ASEMaterialInspector"
    CustomEditor "SkinShaderGUI_URP_SRP"
    Fallback "Hidden/InternalErrorShader"
    
}
    public override void OnGUI( MaterialEditor materialEditor, MaterialProperty[] properties )
    {
        IOUtils.Init();//IOUtils还不算特别
        Material mat = materialEditor.target as Material;
//。。。zheli这里又一堆特别代码
//。。。
        Shader shader = mat.shader;
        int propertyCount = UnityEditor.ShaderUtil.GetPropertyCount( shader );
        for( int i = 0; i < properties.Length; i++ )
        {
            if( ( properties[ i ].flags & ( MaterialProperty.PropFlags.HideInInspector | MaterialProperty.PropFlags.PerRendererData ) ) == MaterialProperty.PropFlags.None )
            {
                // Removed no scale offset one line texture property for consistency :( sad face
                //if( ( properties[ i ].flags & MaterialProperty.PropFlags.NoScaleOffset ) == MaterialProperty.PropFlags.NoScaleOffset )
                //{
                //    object obj = MaterialPropertyHandlerEx.GetHandler( mat.shader, properties[ i ].name );
                //    if( obj != null )
                //    {
                //        float height = MaterialPropertyHandlerEx.GetPropertyHeight( obj, properties[ i ], properties[ i ].displayName, materialEditor );
                //        //Rect rect = (Rect)materialEditor.GetType().InvokeMember( "GetPropertyRect", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, materialEditor, new object[] { properties[ i ], properties[ i ].displayName, true } );
                //        Rect rect = EditorGUILayout.GetControlRect( true, height, EditorStyles.layerMaskField );
                //        MaterialPropertyHandlerEx.OnGUI( obj, ref rect, properties[ i ], new GUIContent( properties[ i ].displayName ), materialEditor );

                //        if( MaterialPropertyHandlerEx.PropertyDrawer( obj ) != null )
                //            continue;

                //        rect = EditorGUILayout.GetControlRect( true, height, EditorStyles.layerMaskField );
                //        materialEditor.TexturePropertyMiniThumbnail( rect, properties[ i ], properties[ i ].displayName, string.Empty );
                //    }
                //    else
                //    {
                //        materialEditor.TexturePropertySingleLine( new GUIContent( properties[ i ].displayName ), properties[ i ] );
                //    }
                //}
                //else
                //{
                    float propertyHeight = materialEditor.GetPropertyHeight( properties[ i ], properties[ i ].displayName );
                    Rect controlRect = EditorGUILayout.GetControlRect( true, propertyHeight, EditorStyles.layerMaskField, new GUILayoutOption[ 0 ] );
                    materialEditor.ShaderProperty( controlRect, properties[ i ], properties[ i ].displayName );
                //}
            }
        }
               
}

官方的 materialEditor 给我们准备了所有API,只要调用 materialEditor.ShaderProperty 即可

自定义Shader步骤:

(你需要又一点点Unity经验,和Editor的Window或者Inspector经验即可)

基础自定义步骤

  1. 创建一个类,继承ShaderGUI

  1. 重写OnGUI( MaterialEditor materialEditor, MaterialProperty[] properties )

  1. 获取 Material mat = materialEditor.target as Material;

  1. 获取mat.shader的属性

  1. 用materialEditor.ShaderProperty显示该方法

  1. 需要在你的shader代码写上 CustomEditor “”,在Fallback代码行之前(是的一个shader 对应一个ShaderGUI子类,不通用,一对一,但是再继承应该做通用应该也是可以)

普通属性上半部分

    void OnProptyCommonGUI(string key)
    {
        var specFloat = FindProperty(key, _properties);//先不管这个错误处理了。。。
        _materialEditor.ShaderProperty(specFloat, new GUIContent(specFloat.displayName, ""));
    }

这样我们就完成了属性部分

没了??就这??

    
        EditorGUILayout.Space();
        materialEditor.RenderQueueField();
        materialEditor.EnableInstancingField();
        materialEditor.DoubleSidedGIField();
        materialEditor.LightmapEmissionProperty();

再完成下半部分,

如果你想隐藏,则不需要了。。。

浅浅的自定义一个折叠Group

//关键在于代码新开始一个Button 样式:      Rect r_texturecolor = EditorGUILayout.BeginVertical("Button");
  _properties = properties;
  _materialEditor = materialEditor;
  //OnSpecularGUI(materialEditor,properties);
  OnSpecularFoldOutGUI();
 

 void OnSpecularFoldOutGUI()
    {
        Rect r_texturecolor = EditorGUILayout.BeginVertical("Button");
        //Rect r_outline = EditorGUILayout.BeginVertical("Button");
        showSpecular = EditorGUILayout.Foldout(showSpecular, "(Specular - )", true, EditorStyles.foldout);


        if (showSpecular)
        {

            OnProptyCommonGUI("_SpecularMapm");
            OnProptyCommonGUI("_Specular");
        }
        
        EditorGUILayout.EndVertical();
    }

    void OnProptyCommonGUI(string key)
    {
        var specFloat = FindProperty(key, _properties);//先不管这个错误处理了。。。
        _materialEditor.ShaderProperty(specFloat, new GUIContent(specFloat.displayName, ""));
    }

总结:

多说无益,Show me the Code

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity Shader是一种用于渲染图形的程序,它可以控制对象的表面颜色、纹理、透明度、反射等属性,从而实现特殊的视觉效果。对于游戏开发者来说,掌握Shader编写技巧是非常重要的。 以下是关于Unity Shader入门精要: 1. ShaderLab语言 ShaderLab是Unity中用于编写Shader的语言,它是一种基于标记的语言,类似于HTML。ShaderLab可以用于定义Shader的属性、子着色器、渲染状态等信息。 2. CG语言 CG语言是Unity中用于编写Shader的主要语言,它是一种类似于C语言的语言,可以进行数学运算、向量计算、流程控制等操作。CG语言可以在ShaderLab中嵌入,用于实现Shader的具体逻辑。 3. Unity的渲染管线 Unity的渲染管线包括顶点着色器、片元着色器、几何着色器等组件,每个组件都有不同的作用。顶点着色器用于对对象的顶点进行变换,片元着色器用于计算每个像素的颜色,几何着色器用于处理几何图形的变形和细节等。 4. 模板和纹理 在Shader中,我们可以使用纹理来给对象添加图案或者贴图,也可以使用模板来控制对象的透明度、反射等属性。纹理可以通过内置函数tex2D()来获取,模板可以通过内置函数clip()来实现裁剪。 5. Shader的实现 Shader的实现需要注意以下几点: - 在ShaderLab中定义Shader的属性、子着色器、渲染状态等信息。 - 在CG语言中实现Shader的具体逻辑,包括顶点着色器、片元着色器等内容。 - 使用纹理和模板来实现特定的视觉效果。 - 在对象上应用Shader,通过调整Shader的属性来达到不同的效果。 以上是关于Unity Shader入门精要,希望对你有所帮助。如果你想更深入地了解Shader的编写技巧,可以参考官方文档或者相关教程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值