继承自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