때로는 셰이더에 빌트인 Unity 머티리얼 에디터를 사용하며 올바르게 표시할 수 없는 흥미로운 데이터 타입이 포함되어 있습니다. Unity는 셰이더 프로퍼티가 표시되는 디폴트 방법을 오버라이드하여 프로퍼티를 직접 정의할 수 있는 방법을 제공합니다. 이 기능을 사용하여 커스텀 컨트롤과 데이터 범위 확인을 정의할 수 있습니다.
셰이더의 gui를 위한 커스텀 에디터를 작성하려면 커스텀 에디터를 필요로 하는 셰이더를 초기에 정의합니다. 커스텀 에디터에 사용하는 이름은 Unity에서 머티리얼 에디터를 찾기 위해 조회하는 클래스입니다.
커스텀 에디터를 정의하려면 ShaderGUI 클래스에서 확장하고 에셋 디렉토리의 에디터 폴더 아래에 스크립트를 저장합니다.
using UnityEditor;
public class CustomShaderGUI : ShaderGUI
{
public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties)
{
base.OnGUI (materialEditor, properties);
}
}
커스텀 에디터가 정의된 셰이더(CustomEditor “CustomShaderGUI”)는 위에 나열된 셰이더 gui 클래스의 인스턴스를 인스턴스화하고 관련 코드를 실행합니다.
이제 스탠다드 디퓨즈 조명을 렌더링하거나 파란색 및 초록색 채널을 50%로 렌더링하는 두 가지 모드로 작동할 수 있는 셰이더가 있습니다.
Shader "Custom/Redify" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert addshadow
#pragma shader_feature REDIFY_ON
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
#if REDIFY_ON
o.Albedo.gb *= 0.5;
#endif
}
ENDCG
}
CustomEditor "CustomShaderGUI"
}
위에서 볼 수 있는 것처럼 셰이더에는 설정에 사용할 수 있는 키워드인 REDIFY_ON이 있습니다. 머티리얼의 shaderKeywords 프로퍼티를 사용하여 머티리얼별로 설정하도록 변경할 수 있습니다. 다음은 이 작업을 수행하는 ShaderGUI 인스턴스입니다.
using UnityEngine;
using UnityEditor;
using System;
public class CustomShaderGUI : ShaderGUI
{
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
// render the default gui
base.OnGUI(materialEditor, properties);
Material targetMat = materialEditor.target as Material;
// see if redify is set, and show a checkbox
bool redify = Array.IndexOf(targetMat.shaderKeywords, "REDIFY_ON") != -1;
EditorGUI.BeginChangeCheck();
redify = EditorGUILayout.Toggle("Redify material", redify);
if (EditorGUI.EndChangeCheck())
{
// enable or disable the keyword based on checkbox
if (redify)
targetMat.EnableKeyword("REDIFY_ON");
else
targetMat.DisableKeyword("REDIFY_ON");
}
}
}
더욱 이해하기 쉬운 ShaderGUI의 예는 StandardShaderGUI.cs 파일과 Unity 다운로드 아카이브에서 다운로드할 수 있는 ‘빌트인 셰이더’ 패키지에 포함된 Standard.shader를 함께 참조하십시오.
위의 간단한 예제는 MaterialPropertyDrawers를 사용하여 훨씬 간편하게 해결할 수도 있습니다. 커스텀/Redify 셰이더의 프로퍼티 섹션에 다음 줄을 추가합니다.
[Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
그리고 다음을 제거합니다.
CustomEditor "CustomShaderGUI"
참고 항목: MaterialPropertyDrawer
ShaderGUI는 예를 들어 머티리얼 프로퍼티가 서로에 종속되거나, 특수 레이아웃을 원하는 경우 더 복잡한 셰이더 GUI 솔루션에 사용할 수 있습니다. MaterialPropertyDrawers를 ShaderGUI 클래스와 함께 사용하여 결합할 수 있습니다. StandardShaderGUI.cs를 참조하십시오.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.