文档

Unity Engine

Unity Engine

ShaderUtil

Utility methods to assist with working with shaders from the Editor.
阅读时间5 分钟最后更新于 15 天前

Definition

  • Type: Class
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule
public sealed class ShaderUtil

Remarks

This class contains methods you can use to do the following:

Examples

using UnityEditor;using UnityEngine;using UnityEngine.Rendering;// Adds a new Editor menu item called Example. Select a shader// asset in the Project window, then select// Example > PrintLightModeTagExample from the Editor menu. The value of all// "LightMode" tags present in any pass of the shader will be printed to// the console.public class PrintLightModeTagExample{ [MenuItem("Example/PrintLightModeTagExample")] static void MenuCallback() { // If the selected object is a shader... if (Selection.activeObject is Shader selectedShader) { // For each subshader... ShaderData selectedShaderData = ShaderUtil.GetShaderData(selectedShader); for (int subShaderIndex = 0; subShaderIndex < selectedShaderData.SubshaderCount; ++subShaderIndex) { // For each pass... ShaderData.Subshader subShaderData = selectedShaderData.GetSubshader(subShaderIndex); for (int passIndex = 0; passIndex < subShaderData.PassCount; ++passIndex) { ShaderData.Pass passData = subShaderData.GetPass(passIndex); // If the pass has a "LightMode" tag, print its value ShaderTagId lightModeValue = passData.FindTagValue(new ShaderTagId("LightMode")); if (lightModeValue != ShaderTagId.none) { Debug.Log($"Pass '{passData.Name}' in SubShader {subShaderIndex} of " + $"Shader '{selectedShader.name}' has LightMode '{lightModeValue.name}'."); } } } } }}

Static Properties

Property

Description

allowAsyncCompilationWhen true, asynchronous Shader compilation is allowed at the current call site.
anythingCompilingWhen true, the Editor is compiling some Shaders asynchronously at the point of query.
disableShaderOptimizationDisables shader optimization in the Editor.
hardwareSupportsRectRenderTextureDoes the current hardware support render textues.

Static Methods

Method

Description

ClearCachedDataClears all internally-cached data that was generated for the given shader, such as errors and compilation info.
ClearShaderMessagesClear compile time messages for the given shader.
CompilePassRequest the Editor to compile the Shader Variant needed for the specific pass of the given Material.
CreateComputeShaderAssetCreates a new ComputeShader object from the provided source code string. You can use this method alongside the ScriptedImporter to create custom compute shader generation tools in the Editor.
CreateRayTracingShaderAssetCreates a new RayTracingShader object from the provided source code string. You can use this method alongside the ScriptedImporter to create custom ray tracing shader generation tools in the Editor.
CreateShaderAssetCreates a new shader from source code and compiles its initial shader variants.
GetAllShaderInfoReturns an array of ShaderInfo of all available shaders. That includes built-in shaders.
GetAnyHitShaderNameReturns the name of a user-defined any hit shader from within a RayTracingShader.
GetAnyHitShaderRayPayloadSizeReturns the ray payload size of a user-defined any hit shader from within a RayTracingShader.
GetCallableShaderCountReturns the number of callable shaders defined whitin a given RayTracingShader.
GetCallableShaderNameReturns the name of a user-defined callable Shader from within a RayTracingShader.
GetCallableShaderParamSizeReturns the parameter size of a user-defined callable shader from within a RayTracingShader.
GetClosestHitShaderNameReturns the name of a user-defined closest hit shader from within a RayTracingShader.
GetClosestHitShaderRayPayloadSizeReturns the ray payload size of a user-defined closest hit shader from within a RayTracingShader.
GetComputeShaderMessageCountReturns the number of errors and warnings generated by the Unity Shader Compiler for the given ComputeShader.
GetComputeShaderMessagesReturns each error and warning generated by the Unity Shader Compiler for the given ComputeShader.
GetCurrentCustomEditorGets the current custom editor for the shader you pass in.
GetCustomEditorForRenderPipelineGets the class name of the custom editor that a shader uses for a render pipeline asset type, identified by its full type name.
GetIntersectionShaderNameReturns the name of a user-defined intersection shader from within a RayTracingShader.
GetMissShaderCountReturns the number of miss Shaders defined whitin a given RayTracingShader.
GetMissShaderNameReturns the name of a user-defined miss Shader from within a RayTracingShader.
GetMissShaderRayPayloadSizeReturns the ray payload size of a user-defined miss Shader from within a RayTracingShader.
GetPassKeywordsGets the local shader keywords that are valid for a pass within a particular shader.
GetRayGenerationShaderCountReturns the number of ray generation Shaders defined whitin a given RayTracingShader.
GetRayGenerationShaderNameReturns the name of a user-defined ray generation Shader from within a RayTracingShader.
GetRayTracingShaderMessageCountReturns the number of errors and warnings generated by the Shader Compiler for the given RayTracingShader.
GetRayTracingShaderMessagesReturns each error and warning generated by the Shader Compiler for the given RayTracingShader.
GetShaderDataGet the shader data for a specific shader.
GetShaderInfoGets ShaderInfo for the given shader.
GetShaderMessageCountReturns the number of errors and warnings generated by the Unity Shader Compiler for the given Shader.
GetShaderMessagesGets the errors and warnings that the shader compiler generated for a shader on all compiler platforms.
GetShaderPlatformKeywordsForBuildTargetGets the platform keywords needed to compile a shader for a shader compiler platform and build target, using graphics tier 1.
HasProceduralInstancingDetermines whether the specified Shader contains a valid Procedural Instancing variant.
IsGraphicsAPISupportedChecks whether the given compute shader supports the provided graphics API.
IsPassCompiledChecks if the shader variant for the given pass in the material has already been compiled.
PassHasKeywordChecks whether a local shader keyword is valid for a pass within a particular shader.
RegisterShaderRegister a user created shader.
RestoreAsyncCompilationRestores the previous Shader compilation mode in this CommandBuffer scope.
SetAsyncCompilationAdds shader compilation mode command in the CommandBuffer.
ShaderHasErrorChecks if a shader has any compilation errors. Ignores warnings.
ShaderHasWarningsChecks if a shader has any compilation warnings. Ignores errors.
UpdateShaderAssetReplaces the source code of a shader during an asset import, and registers its dependencies with the asset system.