Creating Custom Shaders - toydev/HC_VRTrial GitHub Wiki

Prerequisites

Preparation

The .NET project will contain the shader source and asset bundles (a collection of built shaders) for repository management. For this purpose, follow these steps:

  • Create an AssetBundles directory in the .NET project.
  • Place the following .gitignore in the AssetBundles to exclude unnecessary secondary artifacts from repository management:
AssetBundles
*.manifest
*.meta
  • Create a Unity project.
    • Editor Version: 2021.3.14f1
    • Template: 3D

The Unity project is used only as a temporary workspace and is excluded from repository management. It's crucial to match the version with the game itself. Failure to do so may lead to compatibility issues and prevent the shaders from loading properly.

Open Command Prompt with administrative rights and link the .NET project's AssetBundles directory to the Unity project's Asset/AssetBundles directory with the following command:

mklink /D [Path to Unity project's Asset/AssetBundles directory] [Path to .NET project's AssetBundles directory]

Creating Shaders and Asset Bundles

  • Select the AssetBundles directory and create a shader via the menu: CreateShader.
  • Select the shader file and choose an AssetBundle in the Inspector pane.
    • Use New to create one initially. Subsequently, you can select an already created one.
    • There are two input fields, but only the first one is necessary. The second one can be used for further division of asset bundles if needed.
  • Create an Editor directory in the project's Assets directory.
  • Select the Editor directory, then create a C# script via the menu: CreateC# Script.
  • Name the C# script BuildAssetBundles and include the following code:
    • This code adds an operation to build shaders and bundle them into asset bundles to the Unity dropdown menu.
using UnityEditor;

public class BuildAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}
  • Build the asset bundles via the menu: AssetsBuild AssetBundles.

If files named after the asset bundles appear in the Asset/AssetBundles directory, the creation was successful.

Integrating into the .NET Project

Change the build action of the asset bundle file to Embedded Resource.

The following code loads the shaders:

using System.IO;

using UnityEngine;

using HC_VRTrial.Logging;

namespace HC_VRTrial.VRUtils
{
    public class CustomAssetManager
    {
        private static AssetBundle Bundle { get; set; }
        public static AssetBundle GetBundle()
        {
            if (Bundle == null)
            {
                Bundle = AssetBundle.LoadFromMemory(ReadAllBytes($"{nameof(HC_VRTrial)}.AssetBundles.custom_asset_bundle"));
                foreach (var i in Bundle.GetAllAssetNames()) PluginLog.Debug($"Available custom_asset: {i}");
            }
            return Bundle;
        }

        public static Shader UiUnlitTransparentShader { get => GetBundle().LoadAsset("assets/assetbundles/ui-unlit-transparent.shader").Cast<Shader>(); }

        private static byte[] ReadAllBytes(string resourceName)
        {
            var assembly = typeof(CustomAssetManager).Assembly;
            using (var stream = assembly.GetManifestResourceStream(resourceName))
            using (var memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }
    }
}

Additional Notes

Built-in shaders for Unity can be downloaded from the "Built in shaders" link in the dropdown for each OS on the following page:

The shader "assets/assetbundles/ui-unlit-transparent.shader" mentioned in the shader loading example is the "UI-Unlit-Transparent.shader" built-in shader.

This is an example of incorporating it as a custom shader because it was not included in the game itself.

⚠️ **GitHub.com Fallback** ⚠️