Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created October 30, 2024 17:57
Show Gist options
  • Save kurtdekker/36f3e863ff12675302a4e97a8867e099 to your computer and use it in GitHub Desktop.
Save kurtdekker/36f3e863ff12675302a4e97a8867e099 to your computer and use it in GitHub Desktop.
PostProcessingScene work - a way to temporarily modify scenes at build time
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections.Generic;
// @kurtdekker
//
// Makes temporary changes to scenes when building.
//
// Obviously this must live in an Editor/ folder.
public static class PostProcessingSceneUtilities
{
[PostProcessSceneAttribute (2)]
public static void OnPostprocessScene()
{
RemoveStaticFlagsHelper( complain: false);
}
[MenuItem( "Assets/Remove Static Flags")]
static void RemoveStaticFlags()
{
RemoveStaticFlagsHelper( complain: true);
}
static void RemoveStaticFlagsHelper( bool complain = false)
{
var ToChange = new List<GameObject>();
var trs = GameObject.FindObjectsOfType<Transform>();
if (complain)
{
Debug.Log( "trs.Length = " + trs.Length);
}
foreach( var tr in trs)
{
var go = tr.gameObject;
if (go.isStatic)
{
ToChange.Add( go);
}
}
var ToChangeArray = ToChange.ToArray();
if (ToChangeArray.Length > 0)
{
if (complain)
{
Debug.Log( System.String.Format(
"Removing {0} static flags.", ToChangeArray.Length));
}
}
else
{
if (complain)
{
Debug.LogWarning( "No objects marked static found!");
}
}
Undo.RecordObjects( ToChangeArray, "Remove Static");
// actual doing the work here...
foreach( var go in ToChangeArray)
{
// I used to do this, but this means it cannot be used in
// games that use Unity navigation:
// go.isStatic = false;
// the point of this whole thing is only to remove lightmap
// static flags, so let's just do that!
var flags = GameObjectUtility.GetStaticEditorFlags( go);
#if UNITY_2020_1_OR_NEWER
flags &= ~(StaticEditorFlags.ContributeGI);
#else
flags &= ~(StaticEditorFlags.LightmapStatic);
#endif
GameObjectUtility.SetStaticEditorFlags( go, flags);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment