文档

Unity Engine

Unity Engine

CreateShaderAsset

Creates a new shader from source code during an asset import, and registers its dependencies with the asset system.
阅读时间2 分钟最后更新于 16 天前

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule

CreateShaderAsset(AssetImportContext, string, bool)

Creates a new shader from source code during an asset import, and registers its dependencies with the asset system.
public static Shader CreateShaderAsset(AssetImportContext context, string source, bool compileInitialShaderVariants)

Parameters

The import context that registers the shader dependencies with the asset system. Pass the context from a ScriptedImporter when you call this method during an import.

source

A string that contains a shader written in ShaderLab code.

compileInitialShaderVariants

Set to true to compile the initial shader variants from the source code. Set to false to skip compiling them.

Returns

Type

Description

ShaderThe new Shader object.

Remarks

Use this overload in a ScriptedImporter to create custom shader generation tools in the Unity Editor. The import context lets the asset system track the files that the shader depends on, so Unity reimports the shader when they change.

CreateShaderAsset(string)

Creates a new shader from source code and compiles its initial shader variants.
public static Shader CreateShaderAsset(string source)

Parameters

source

A string that contains a shader written in ShaderLab code.

Returns

Type

Description

ShaderThe new Shader object.

Remarks

Use this overload only to create a transient shader that isn't serialized or stored anywhere. Unity compiles the initial shader variants and doesn't register any shader dependencies with the asset system. To create a persistent shader, always use the overload that takes an AssetImportContext. To control compilation, use the overload that takes
compileInitialShaderVariants
.

CreateShaderAsset(string, bool)

Creates a new shader from source code, with the option to skip compiling the initial shader variants.
public static Shader CreateShaderAsset(string source, bool compileInitialShaderVariants)

Parameters

source

A string that contains a shader written in ShaderLab code.

compileInitialShaderVariants

Set to true to compile the initial shader variants from the source code. Set to false to skip compiling them.

Returns

Type

Description

ShaderThe new Shader object.

Remarks

Use this overload only to create a transient shader that isn't serialized or stored anywhere. Unity doesn't register any shader dependencies with the asset system. To create a persistent shader, always use the overload that takes an AssetImportContext.