Skip to content

Instantly share code, notes, and snippets.

@float3
Created February 13, 2022 16:58
Show Gist options
  • Save float3/d61a368a95ec1ee588453ee6d4196c93 to your computer and use it in GitHub Desktop.
Save float3/d61a368a95ec1ee588453ee6d4196c93 to your computer and use it in GitHub Desktop.
this will set a shader keyword and make sure it's only true in editor and never in build by disabling before the build and stripping and shader variants that have that keyword
// to use this define
// #pragma shader_feature UNITY_EDITOR
// in your shader
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Callbacks;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Rendering;
#if VRC_SDK_VRCSDK3
using VRC.SDKBase.Editor.BuildPipeline;
#endif
namespace Shaders.UNITY_EDITOR_TEST.Editor
{
[InitializeOnLoad]
public class SetKeyword
{
static SetKeyword()
{
Shader.EnableKeyword("UNITY_EDITOR");
}
[PostProcessBuild(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
Shader.EnableKeyword("UNITY_EDITOR");
}
}
#region Callbacks
#if VRC_SDK_VRCSDK3
public class OnBuildRequest : IVRCSDKBuildRequestedCallback
{
public int callbackOrder => 3;
public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType)
{
Shader.DisableKeyword("UNITY_EDITOR");
return true;
}
}
#if !UDON
public class OnAvatarBuild : IVRCSDKPreprocessAvatarCallback
{
public int callbackOrder => 3;
public bool OnPreprocessAvatar(GameObject avatarGameObject)
{
Shader.DisableKeyword("UNITY_EDITOR");
return true;
}
}
public class OnPostAvatarBuild : IVRCSDKPostprocessAvatarCallback
{
public int callbackOrder => 3;
public void OnPostprocessAvatar()
{
Shader.EnableKeyword("UNITY_EDITOR");
}
}
#endif
#else
public class OnBuild : IPreprocessBuildWithReport
{
public int callbackOrder => 3;
public void OnPreprocessBuild(BuildReport report)
{
Shader.DisableKeyword("UNITY_EDITOR");
}
}
#endif
#endregion
public class ShaderPreprocessor : IPreprocessShaders
{
public int callbackOrder => 3;
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
{
for (var i = data.Count - 1; i >= 0; --i)
if (data[i].shaderKeywordSet.IsEnabled(new ShaderKeyword("UNITY_EDITOR")))
data.RemoveAt(i);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment