Skip to content

Instantly share code, notes, and snippets.

@JimmyCushnie
Last active April 30, 2024 18:17
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JimmyCushnie/e998cdec15394d6b68a4dbbf700f66ce to your computer and use it in GitHub Desktop.
Save JimmyCushnie/e998cdec15394d6b68a4dbbf700f66ce to your computer and use it in GitHub Desktop.
Exposes some Unity URP graphics settings that are (for some stupid fucking bullshit reason) private.
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using ShadowResolution = UnityEngine.Rendering.Universal.ShadowResolution;
/// <summary>
/// Enables getting/setting URP graphics settings properties that don't have built-in getters and setters.
/// </summary>
public static class UnityGraphicsBullshit
{
private static FieldInfo MainLightCastShadows_FieldInfo;
private static FieldInfo AdditionalLightCastShadows_FieldInfo;
private static FieldInfo MainLightShadowmapResolution_FieldInfo;
private static FieldInfo AdditionalLightShadowmapResolution_FieldInfo;
private static FieldInfo Cascade2Split_FieldInfo;
private static FieldInfo Cascade4Split_FieldInfo;
private static FieldInfo SoftShadowsEnabled_FieldInfo;
static UnityGraphicsBullshit()
{
var pipelineAssetType = typeof(UniversalRenderPipelineAsset);
var flags = BindingFlags.Instance | BindingFlags.NonPublic;
MainLightCastShadows_FieldInfo = pipelineAssetType.GetField("m_MainLightShadowsSupported", flags);
AdditionalLightCastShadows_FieldInfo = pipelineAssetType.GetField("m_AdditionalLightShadowsSupported", flags);
MainLightShadowmapResolution_FieldInfo = pipelineAssetType.GetField("m_MainLightShadowmapResolution", flags);
AdditionalLightShadowmapResolution_FieldInfo = pipelineAssetType.GetField("m_AdditionalLightsShadowmapResolution", flags);
Cascade2Split_FieldInfo = pipelineAssetType.GetField("m_Cascade2Split", flags);
Cascade4Split_FieldInfo = pipelineAssetType.GetField("m_Cascade4Split", flags);
SoftShadowsEnabled_FieldInfo = pipelineAssetType.GetField("m_SoftShadowsSupported", flags);
}
public static bool MainLightCastShadows
{
get => (bool)MainLightCastShadows_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => MainLightCastShadows_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static bool AdditionalLightCastShadows
{
get => (bool)AdditionalLightCastShadows_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => AdditionalLightCastShadows_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static ShadowResolution MainLightShadowResolution
{
get => (ShadowResolution)MainLightShadowmapResolution_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => MainLightShadowmapResolution_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static ShadowResolution AdditionalLightShadowResolution
{
get => (ShadowResolution)AdditionalLightShadowmapResolution_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => AdditionalLightShadowmapResolution_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static float Cascade2Split
{
get => (float)Cascade2Split_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => Cascade2Split_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static Vector3 Cascade4Split
{
get => (Vector3)Cascade4Split_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => Cascade4Split_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
public static bool SoftShadowsEnabled
{
get => (bool)SoftShadowsEnabled_FieldInfo.GetValue(GraphicsSettings.currentRenderPipeline);
set => SoftShadowsEnabled_FieldInfo.SetValue(GraphicsSettings.currentRenderPipeline, value);
}
}
@JimmyCushnie
Copy link
Author

JimmyCushnie commented Jun 28, 2020

I hereby release this code into the public domain. Do whatever you want to with it.

@victorlapin
Copy link

Dude, thank you very much for this solution =) and indeed, it's UnityGraphicsBullshit

@JimmyCushnie
Copy link
Author

Cheers, I'm glad it helped you.

@Shrimpey
Copy link

Thank you dude, awesome you fixed something Unity struggled to fix for eternity (and still struggles). Also love the naming convention.

@Twirl1511
Copy link

Thank you!

@coty-crg
Copy link

coty-crg commented Apr 1, 2023

Thanks for this! Saved me some time (shipped a game with this file haha)

@JimmyCushnie
Copy link
Author

Happy to be of service 🫡

@haplane10
Copy link

Thank you so much!!

@stuw-u
Copy link

stuw-u commented Jun 11, 2023

Coming back to this every month.
Shipped 2 projects with this, used it in 4 projects, even extended it a bit.
I am forever thankful but.... HOW IS THIS STILL NEEDED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@JimmyCushnie
Copy link
Author

It truly boggles the mind how the simplest one-line changes at Unity take a decade to be implemented.

@DanjelRicci
Copy link

Shipped two games with this and about to ship a third with it. Thank you so much!

@JimmyCushnie
Copy link
Author

💜💜💜 I'm very happy it's useful to you!

@AnthonyDemanueleFSG
Copy link

Thanks @JimmyCushnie!

Any ideas how I can extend this to access the GraphicSettings too?
Tried this so far without any luck.

var graphicsSettingsAssetType = typeof(GraphicsSettings); var graphicFlags = BindingFlags.Instance | BindingFlags.NonPublic; LightmapKeepStrippingFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapStripping", graphicFlags); //Lightmap stripping LightmapKeepPlainFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepPlain", graphicFlags); //Baked Non-Directional LightmapKeepDirCombinedFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepDirCombined", graphicFlags); //Baked Directional LightmapKeepDynamicPlainFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepDynamicPlain", graphicFlags); //Realtime Non-Directional LightmapKeepDynamicDirCombinedFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepDynamicDirCombined", graphicFlags); //Realtime Directional LightmapKeepShadowMaskFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepShadowMask", graphicFlags); //Baked Shadowmask LightmapKeepSubtractiveFieldInfo = graphicsSettingsAssetType.GetField("m_LightmapKeepSubtractive", graphicFlags); //Baked Subtractive

@Vivian-A
Copy link

Legendary. Love the name of it too!

@AnthonyDemanueleFSG
Copy link

If anyone else needs this, here's how we ended up changing graphic settings in our case:

public static void SetToHDSettings()
{
    var graphicSettingAssetDataPath = Application.dataPath.Replace("Assets", "ProjectSettings/GraphicsSettings.asset");
    var graphicSettingsFileData = File.ReadAllText(graphicSettingAssetDataPath);
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapStripping: 0", "m_LightmapStripping: 1");                              //Lightmap stripping - Always 1 - Custom
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepPlain: 1", "m_LightmapKeepPlain: 0");                              //Baked Non-Directional
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepDirCombined: 0", "m_LightmapKeepDirCombined: 1");                  //Baked Directional
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepDynamicPlain: 1", "m_LightmapKeepDynamicPlain: 0");                //Realtime Non-Directional 
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepDynamicDirCombined: 1", "m_LightmapKeepDynamicDirCombined: 0");    //Realtime Directional
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepShadowMask: 0", "m_LightmapKeepShadowMask: 1");                    //BakedShadowMask
    graphicSettingsFileData = graphicSettingsFileData.Replace("m_LightmapKeepSubtractive: 1", "m_LightmapKeepSubtractive: 0");                  //BakedSubtractive

    File.WriteAllText(graphicSettingAssetDataPath, graphicSettingsFileData);
    Debug.LogWarning($"Set shader stripping to HD settings.ShadowMask ON. Subtractive OFF");
}

@anomal3
Copy link

anomal3 commented Dec 30, 2023

I love you

@wonkee-kim
Copy link

Thank you!

@xiang-valcano
Copy link

does this really work in player build? I understand that shader variants will be stripped during the build process, and this script should not take any effect.

@MintUnreal
Copy link

Я ХОЧУ ОТ ТЕБЯ ДЕТЕЙ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment