SoFunction
Updated on 2025-03-07

Unity Shader Editor Tool Class ShaderUtil Common functions and usage examples

Unity's Shader Editor tool class ShaderUtil provides a series of functions for compiling, importing, and managing shaders. This article will introduce common functions and usages in the ShaderUtil class.

Compile and import functions

CompileShader

Function signature:public static bool CompileShader(string source, out string error);

CompileShader function is used to compile a shader. It accepts a string parameter source, indicating the shader code to be compiled, and returns a boolean, indicating whether the compilation is successful. If the compilation fails, the error message can be obtained through the out parameter error.

Here is an example using the CompileShader function:

string shaderCode = @"
    Shader ""CustomShader"" {
        // Shader code    }
";
string error;
bool success = (shaderCode, out error);
if (success)
{
    ("Shader compiled successfully!");
}
else
{
    ("Shader compilation failed: " + error);
}

OpenCompiledShader

Function signature:public static void OpenCompiledShader(string pathName, int line);

The OpenCompiledShader function is used to open a compiled shader file in an external program. It accepts two parameters, pathName represents the path to the shader file to be opened, and line represents the number of lines to be redirected to.

Here is an example using the OpenCompiledShader function:

string shaderPath = "Assets/Shaders/";
int line = 10;
(shaderPath, line);

Properties and keyword functions

GetPropertyCount

Function signature:public static int GetPropertyCount(Shader shader);

This function is used to get the number of properties in the specified shader. It takes a Shader object as an argument and returns the number of properties in that shader. Properties are variables defined in the Shader editor to control the appearance and behavior of a material.

Here is a sample code that demonstrates how to use the GetPropertyCount function to get the number of properties in the shader:

Shader shader = ("MyShader");
int propertyCount = (shader);
("Property count: " + propertyCount);

GetPropertyName

Function signature:public static string GetPropertyName(Shader shader, int propertyIndex);

This function is used to get the name of the property in the specified shader. It takes an index of a Shader object and property as a parameter and returns the name of the property.

Here is a sample code that demonstrates how to use the GetPropertyName function to get the name of a property in the shader:

Shader shader = ("MyShader");
int propertyIndex = 0;
string propertyName = (shader, propertyIndex);
("Property name: " + propertyName);

GetShaderKeywords

Function signature:public static string[] GetShaderKeywords(Shader shader);

This function is used to get the keywords for the specified shader. Keywords are identifiers defined in the Shader editor to control the different functions and effects of the shader.

Here is a sample code that demonstrates how to use the GetShaderKeywords function to get the keywords of a shader:

Shader shader = ("MyShader");
string[] keywords = (shader);
("Shader keywords: " + (", ", keywords));

SetShaderKeywords

Function signature:
public static void SetShaderKeywords(Shader shader, string[] keywords);

This function is used to set the keywords for the specified shader. It takes a Shader object and an array of keywords as parameters and applies these keywords to the shader.

Here is a sample code that demonstrates how to set the keywords of a shader using the SetShaderKeywords function:

Shader shader = ("MyShader");
string[] keywords = new string[] { "KEYWORD1", "KEYWORD2" };
(shader, keywords);

Render texture function

1. GetRenderTextureFormat

public static RenderTextureFormat GetRenderTextureFormat(string name);

This function is used to get the render texture format of the specified name. It accepts a string parameter name, which represents the name of the rendered texture and returns a RenderTextureFormat enumeration value, which represents the corresponding rendered texture format.

2. GetRenderTargetCount

public static int GetRenderTargetCount();

This function is used to get the number of the current rendering targets. It returns an integer value indicating the number of current render targets.

3. GetRenderTargetName

public static string GetRenderTargetName(int index);

This function is used to get the name of the rendering target of the specified index. It accepts an integer parameter index, which represents the index of the rendering target and returns a string value, which represents the name of the corresponding rendering target.

4. GetRenderTargetPropertyCount

public static int GetRenderTargetPropertyCount(int index);

This function is used to get the number of attributes for the specified rendering target. It accepts an integer parameter index, which represents the index of the rendering target and returns an integer value, which represents the number of attributes corresponding to the rendering target.

5. GetRenderTargetPropertyName

public static string GetRenderTargetPropertyName(int index, int propIndex);

This function is used to get the attribute name of the specified rendering target. It accepts two integer parameters index and propIndex, respectively representing the index of the rendering target and the index of the property, and returns a string value to represent the name of the corresponding property.

6. GetRenderTargetPropertyType

public static  GetRenderTargetPropertyType(int index, int propIndex);

This function is used to get the attribute type of the specified rendering target. It accepts two integer parameters index and propIndex, respectively representing the index of the rendering target and the index of the attribute, and returns an enumeration value to represent the type of the corresponding attribute.

7. GetRenderTargetPropertyValue

public static float GetRenderTargetPropertyValue(int index, int propIndex);

This function is used to get the attribute value of the specified rendering target. It accepts two integer parameters index and propIndex, respectively representing the index of the rendering target and the index of the property, and returns a floating point value to represent the value of the corresponding property.

8. SetRenderTargetPropertyCount

public static void SetRenderTargetPropertyCount(int index, int count);

This function is used to set the number of attributes for a specified rendering target. It accepts two integer parameters index and count, respectively, representing the number of indexes and attributes of the rendering target.

9. SetRenderTargetPropertyName

public static void SetRenderTargetPropertyName(int index, int propIndex, string name);

This function is used to set the attribute name of the specified rendering target. It accepts three parameters index, propIndex and name, which represent the index of the rendering target, the index of the attribute and the name of the attribute respectively.

10. SetRenderTargetPropertyType

public static void SetRenderTargetPropertyType(int index, int propIndex,  type);

This function is used to set the attribute type of the specified rendering target. It accepts three parameters index, propIndex and type, which represent the index of the rendering target, the index of the attribute and the type of the attribute respectively.

11. SetRenderTargetPropertyValue

public static void SetRenderTargetPropertyValue(int index, int propIndex, float value);

This function is used to set the property value of the specified rendering target. It accepts three parameters index, propIndex and value, respectively representing the index of the rendering target, the index of the attribute and the value of the attribute.

Sample code

using UnityEngine;
public class ShaderUtilExample : MonoBehaviour
{
    void Start()
    {
        // Get the render texture format        RenderTextureFormat format = ("MyRenderTexture");
        ("RenderTexture format: " + format);
        // Get the number of rendering targets        int targetCount = ();
        ("Render target count: " + targetCount);
        // Get the render target name        string targetName = (0);
        ("Render target name: " + targetName);
        // Get the number of render target attributes        int propertyCount = (0);
        ("Render target property count: " + propertyCount);
        // Get the render target attribute name        string propertyName = (0, 0);
        ("Render target property name: " + propertyName);
        // Get the render target attribute type         propertyType = (0, 0);
        ("Render target property type: " + propertyType);
        // Get the render target attribute value        float propertyValue = (0, 0);
        ("Render target property value: " + propertyValue);
        // Set the number of rendering target attributes        (0, 2);
        // Set the render target attribute name        (0, 0, "Property1");
        (0, 1, "Property2");
        // Set the render target attribute type        (0, 0, );
        (0, 1, );
        // Set the render target attribute value        (0, 0, 1.0f);
        (0, 1, );
    }
}

Other functions

GetMaterial

public static Material GetMaterial(Renderer renderer, int materialIndex);

This function is used to obtain the Material instance of the specified index on the specified renderer. In Unity, the renderer can contain multiple materials, each material corresponding to a sub-mesh (SubMesh). Here is an example of usage:

Renderer renderer = GetComponent<Renderer>();
Material material = (renderer, 0);

GetMaterialProperty

public static float GetMaterialProperty(Material material, int propertyIndex);
public static Vector4 GetMaterialProperty(Material material, int propertyIndex, bool isBatchable);

This function is used to get the value of the property of the specified index on the material. Properties can be of type floating point numbers, vectors, etc. Here is an example of usage:

Material material = GetComponent<Renderer>().material;
float floatValue = (material, 0);
Vector4 vectorValue = (material, 1);

GetMaterialPropertyIndex

public static int GetMaterialPropertyIndex(Material material, string propertyName);

This function is used to obtain the index (propertyIndex) of the specified property (propertyName) on the material. Here is an example of usage:

Material material = GetComponent<Renderer>().material;
int propertyIndex = (material, "_Color");

GetMaterialPropertyType

public static  GetMaterialPropertyType(Material material, int propertyIndex);

This function is used to obtain the type of the property (ShaderPropertyType) of the specified index on the material (Material). Here is an example of usage:

Material material = GetComponent<Renderer>().material;
 propertyType = (material, 0);

GetMaterialPropertyDefaultValue

public static float GetMaterialPropertyDefaultValue(Material material, int propertyIndex);
public static Vector4 GetMaterialPropertyDefaultValue(Material material, int propertyIndex, bool isBatchable);

This function is used to get the default value of the property of the specified index on the material. Here is an example of usage:

Material material = GetComponent<Renderer>().material;
float defaultValue = (material, 0);
Vector4 defaultVector = (material, 1);

GetMaterialPropertyTextureDimension

public static TextureDimension GetMaterialPropertyTextureDimension(Material material, int propertyIndex);

This function is used to obtain the dimensions of the Texture Property of the specified index on the Material. Here is an example of usage:

Material material = GetComponent<Renderer>().material;
TextureDimension dimension = (material, 0);

GetMaterialPropertyTextureDefaultName

public static string GetMaterialPropertyTextureDefaultName(Material material, int propertyIndex);

This function is used to get the default name of the Texture Property of the specified index on the Material. Here is an example of usage:

Material material = GetComponent<Renderer>().material;
string defaultName = (material, 0);

GetGlobalFloat

public static float GetGlobalFloat(string name);

This function is used to get the value of the Global Float Property. Here is an example of usage:

float globalFloat = ("_MyGlobalFloat");

GetGlobalInt

public static int GetGlobalInt(string name);

This function is used to get the value of the Global Int Property. Here is an example of usage:

int globalInt = ("_MyGlobalInt");

GetGlobalVector

public static Vector4 GetGlobalVector(string name);

This function is used to get the value of the Global Vector Property. Here is an example of usage:

Vector4 globalVector = ("_MyGlobalVector");

GetGlobalMatrix

public static Matrix4x4 GetGlobalMatrix(string name);

This function is used to get the value of the Global Matrix Property. Here is an example of usage:

Matrix4x4 globalMatrix = ("_MyGlobalMatrix");

GetGlobalTexture

public static Texture GetGlobalTexture(string name);

This function is used to get the value of the Global Texture Property. Here is an example of usage:

Texture globalTexture = ("_MyGlobalTexture");

SetGlobalFloat

public static void SetGlobalFloat(string name, float value);

This function is used to set the value of the Global Float Property. Here is an example of usage:

("_MyGlobalFloat", 1.5f);

This will set the global floating point property named "_MyGlobalFloat" to 1.5.

SetGlobalInt

public static void SetGlobalInt(string name, int value);

This function is used to set the value of the Global Int Property. Here is an example of usage:

("_MyGlobalInt", 10);

This will set the global integer property named "_MyGlobalInt" to 10.

SetGlobalVector

public static void SetGlobalVector(string name, Vector4 value);

This function is used to set the value of the Global Vector Property. Here is an example of usage:

("_MyGlobalVector", new Vector4(1, 2, 3, 4));

This will set the global vector property named "_MyGlobalVector" to (1, 2, 3, 4).

SetGlobalMatrix

public static void SetGlobalMatrix(string name, Matrix4x4 value);

This function is used to set the value of the Global Matrix Property. Here is an example of usage:

Matrix4x4 matrix = ;
("_MyGlobalMatrix", matrix);

This will set the global matrix property named "_MyGlobalMatrix" to the unit matrix.

SetGlobalTexture

public static void SetGlobalTexture(string name, Texture value);

This function is used to set the value of the Global Texture Property. Here is an example of usage:

Texture texture = <Texture>("MyTexture");
("_MyGlobalTexture", texture);

This will set the global texture property named "_MyGlobalTexture" to the resource texture named "MyTexture".

I sincerely apologize to you for possible errors in the technical articles. I strive to ensure accurate and reliable information is provided, but errors are inevitable due to constant changes in the technical field. If you find an error or have any questions, please contact me. I will do my best to correct errors and provide more accurate information.

This is the article about the common functions and usages of Unity Shader Editor Tool Class ShaderUtil. This is all about this. For more related contents of Unity Shader Editor Tool Class ShaderUtil, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!