Skip to content

Instantly share code, notes, and snippets.

@AG-Dan
Created August 30, 2020 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AG-Dan/63acf648e385ccaef37f71a08d045134 to your computer and use it in GitHub Desktop.
Save AG-Dan/63acf648e385ccaef37f71a08d045134 to your computer and use it in GitHub Desktop.
using UnityEngine;
class GlobalDungeonObject : MonoBehaviour
{
}
using DunGen;
using UnityEngine;
/// <summary>
/// Takes objects marked as global dungeon objects (can move freely throughout the dungeon layout)
/// and parents them to the dungeon root instead of the room the object was spawned in.
/// Used to prevent global objects from being culled when using the built-in culling scripts
/// NOTE: Script execution order might need to be changed to ensure this script runs before the culling
/// caches all of the renderers in each tile.
/// </summary>
class UnparentGlobalDungeonObjects : MonoBehaviour
{
public RuntimeDungeon RuntimeDungeon = null;
private void Awake()
{
RuntimeDungeon.Generator.OnGenerationStatusChanged += OnGenerationStatusChanged;
}
private void OnDestroy()
{
RuntimeDungeon.Generator.OnGenerationStatusChanged -= OnGenerationStatusChanged;
}
private void OnGenerationStatusChanged(DungeonGenerator generator, GenerationStatus status)
{
if (status != GenerationStatus.Complete)
return;
// GlobalDungeonObject is just an empty MonoBehaviour used to tag objects
// other methods can be used instead (tags, name, layer, etc)
foreach (var obj in generator.Root.GetComponentsInChildren<GlobalDungeonObject>())
obj.transform.parent = generator.Root.transform;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment