ShaderGUI的学习

继承自ShaderGUI
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System;

public class ShaderGUITest : ShaderGUI
{
    private MaterialProperty[] _properties;
    private int defaultWidth = 2048;
    private int defaultHeight = 2048;
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        _properties = properties ;
        // render the default gui
        base.OnGUI(materialEditor, properties);
        MaterialProperty mytest = FindProperty("_MainTex",  _properties) ;
        
        Texture tex = mytest.textureValue;
        string texPath = AssetDatabase.GetAssetPath(mytest.textureValue.GetInstanceID());
        
        Material targetMat = materialEditor.target as Material;
        // see if redify is set, and show a checkbox
        bool CS_BOOL = Array.IndexOf(targetMat.shaderKeywords, "CS_BOOL") != -1;
        EditorGUI.BeginChangeCheck();

        //检查面板上cs_bool的开关变化
        CS_BOOL = EditorGUILayout.Toggle("CS_BOOL", CS_BOOL);
        if (EditorGUI.EndChangeCheck())
        {
            //设置全局着色器关键字。
            targetMat.EnableKeyword("CS_BOOL");
        }
        else
        {
            //取消设置全局着色器关键字。
            targetMat.DisableKeyword("CS_BOOL");
        }
        
        if (tex.width != defaultWidth)
        {
            string errorStr = string.Format("{0}: 大小不等于2048,宽为{1},格式为{0},请检查,地址{3}",tex.name,tex.width,texPath);
            EditorGUILayout.HelpBox(errorStr, MessageType.Error);
        }
    }
}

 

 ongui就是画在面板上的东西

MaterialEditor

可以获取材质的东西

MaterialProperty

材质的一些参数

MaterialProperty mytest = FindProperty("_MainTex",  _properties) ;

获取shader中_MainTex的属性

啥叫设置全局着色器关键字。

 #pragma shader_feature CS_BOOL

Unity shader 编辑器扩展类 ShaderGUI-腾讯游戏学堂

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值