Skip to content

Instantly share code, notes, and snippets.

@kraj0t
Last active March 21, 2025 21:07
Show Gist options
  • Save kraj0t/64fcab62d071c4c8b4150244e3da8469 to your computer and use it in GitHub Desktop.
Save kraj0t/64fcab62d071c4c8b4150244e3da8469 to your computer and use it in GitHub Desktop.
Asset that I like to use for quick debugging of shader values
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
/// <summary>
/// Convenience asset for testing different global shader values in the editor.
/// </summary>
[CreateAssetMenu(fileName = "DebugShaderGlobalsProfile", menuName = "__AURELIO/DebugShaderGlobalsProfile")]
public class DebugShaderGlobalsProfile : ScriptableObject
{
[Serializable, InlineProperty]
public class ShaderGlobal<T>
{
[HorizontalGroup(25f), HideLabel] public bool enabled = true;
[HorizontalGroup(0.33f), HideLabel, EnableIf(nameof(enabled))] public string name;
[HorizontalGroup, HideLabel, EnableIf(nameof(enabled))] public T value;
}
public bool isActive;
public List<ShaderGlobal<bool>> booleans = new();
public List<ShaderGlobal<float>> floats = new();
public List<ShaderGlobal<Color>> colors = new();
public List<ShaderGlobal<Vector4>> vectors = new();
public List<ShaderGlobal<Texture2D>> textures = new();
[PropertySpace(20)]
[ShowInInspector]
private static bool ApplyOnChange = false;
private static DebugShaderGlobalsProfile activeProfile;
private static HashSet<DebugShaderGlobalsProfile> allProfiles;
private void OnEnable()
{
if (allProfiles == null)
{
allProfiles = new HashSet<DebugShaderGlobalsProfile>();
}
allProfiles.Add(this);
if (isActive)
{
if (activeProfile && activeProfile != this)
{
Debug.LogError("Multiple active DebugShaderGlobalsProfiles detected. Only the first one will be left as the active one.");
isActive = false;
}
else
{
Apply();
}
}
}
private void OnDestroy()
{
allProfiles.Remove(this);
}
[Button]
public void Apply()
{
isActive = true;
activeProfile = this;
foreach (var shaderGlobal in booleans)
{
if (shaderGlobal.enabled && shaderGlobal.name != null)
{
Shader.SetGlobalInt(shaderGlobal.name, shaderGlobal.value ? 1 : 0);
}
}
foreach (var shaderGlobal in floats)
{
if (shaderGlobal.enabled && shaderGlobal.name != null)
{
Shader.SetGlobalFloat(shaderGlobal.name, shaderGlobal.value);
}
}
foreach (var shaderGlobal in colors)
{
if (shaderGlobal.enabled && shaderGlobal.name != null)
{
Shader.SetGlobalColor(shaderGlobal.name, shaderGlobal.value);
}
}
foreach (var shaderGlobal in vectors)
{
if (shaderGlobal.enabled && shaderGlobal.name != null)
{
Shader.SetGlobalVector(shaderGlobal.name, shaderGlobal.value);
}
}
foreach (var shaderGlobal in textures)
{
if (shaderGlobal.enabled && shaderGlobal.name != null)
{
Shader.SetGlobalTexture(shaderGlobal.name, shaderGlobal.value);
}
}
}
[Button]
private void CopyShaderCodeToClipboard()
{
var strBuilder = new System.Text.StringBuilder();
strBuilder.AppendLine("//// AUTO-GENERATED CODE - DebugShaderGlobalsProfile: " + name);
foreach (var shaderGlobal in booleans)
{
if (shaderGlobal.name != null)
{
strBuilder.AppendLine($"bool {shaderGlobal.name};");
}
}
foreach (var shaderGlobal in floats)
{
if (shaderGlobal.name != null)
{
strBuilder.AppendLine($"float {shaderGlobal.name};");
}
}
foreach (var shaderGlobal in colors)
{
if (shaderGlobal.name != null)
{
strBuilder.AppendLine($"float4 {shaderGlobal.name};");
}
}
foreach (var shaderGlobal in vectors)
{
if (shaderGlobal.name != null)
{
strBuilder.AppendLine($"float4 {shaderGlobal.name};");
}
}
foreach (var shaderGlobal in textures)
{
if (shaderGlobal.name != null)
{
strBuilder.AppendLine($"TEXTURE2D({shaderGlobal.name});");
}
}
strBuilder.AppendLine("//// END AUTO-GENERATED CODE");
GUIUtility.systemCopyBuffer = strBuilder.ToString();
#if UNITY_EDITOR
UnityEditor.EditorWindow.focusedWindow.ShowNotification(new GUIContent("Copied to clipboard!"));
#endif
}
private void OnValidate()
{
if (isActive == false && activeProfile == this)
{
activeProfile = null;
}
if (ApplyOnChange && activeProfile == this)
{
Apply();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment