Skip to content

Instantly share code, notes, and snippets.

@fguillen
Last active April 3, 2024 00:59
Show Gist options
  • Save fguillen/2327f281c7827f39f7873550273fbe63 to your computer and use it in GitHub Desktop.
Save fguillen/2327f281c7827f39f7873550273fbe63 to your computer and use it in GitHub Desktop.
Godot preload and cache Shader Materials to avoid freeze in play mode
# Create a Scene with a Node
# Add this script to it
# Add the Scene to the Autoloads
# Fulfill the material Array with all the Materials that are causing freeze
# It also works with Materials from ParticlesSystems
# preload_materials.gd
extends Node
# Load the array through Inspector
@export var materials: Array[Material]
func _ready():
_load_materials()
func _load_materials():
var sprites = []
for material in materials:
var sprite = Sprite2D.new()
sprite.texture = PlaceholderTexture2D.new()
sprite.material = material
add_child(sprite)
sprites.append(sprite)
# Remove the sprites after being rendered
await get_tree().create_timer(0.2).timeout
for sprite in sprites:
sprite.queue_free()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment