└── loading ├── logo.png ├── loading.gd └── loading.tscn /loading/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeAmputer/godot-simple-scene-loader/HEAD/loading/logo.png -------------------------------------------------------------------------------- /loading/loading.gd: -------------------------------------------------------------------------------- 1 | extends CanvasLayer 2 | 3 | const _preload_animation_name := "Preload" 4 | const _postload_animation_name := "Postload" 5 | 6 | var _is_loading := false 7 | var _should_play_animation := false 8 | var _progress_completion := [] 9 | var _scene_path: String 10 | 11 | @onready var _animator: AnimationPlayer = %animator 12 | @onready var _progress_bar: ProgressBar = %progress_bar 13 | @onready var _background: ColorRect = %background 14 | 15 | func _ready(): 16 | _background.visible = false 17 | _background.modulate = Color(1, 1, 1, 0) 18 | 19 | func load_scene(path: String, use_transition_scene: bool = false) -> bool: 20 | if _is_loading: 21 | printerr("Scene loading is already in process") 22 | return false 23 | 24 | _is_loading = true 25 | _should_play_animation = use_transition_scene 26 | call_deferred("_deferred_load_scene", path) 27 | 28 | return true 29 | 30 | func _process(_delta: float) -> void: 31 | if _is_loading: 32 | var loading_status := ResourceLoader.load_threaded_get_status( 33 | _scene_path, 34 | _progress_completion) 35 | 36 | _progress_bar.value = _progress_completion[0] 37 | 38 | match loading_status: 39 | ResourceLoader.THREAD_LOAD_IN_PROGRESS: 40 | pass 41 | ResourceLoader.THREAD_LOAD_LOADED: 42 | _set_new_scene() 43 | ResourceLoader.THREAD_LOAD_FAILED: 44 | printerr("Scene loading failed") 45 | _is_loading = false 46 | 47 | func _deferred_load_scene(path: String) -> void: 48 | _scene_path = path 49 | ResourceLoader.load_threaded_request(_scene_path) 50 | _try_play_animation(_preload_animation_name) 51 | 52 | func _set_new_scene() -> void: 53 | get_tree().change_scene_to_packed( 54 | ResourceLoader.load_threaded_get(_scene_path)) 55 | 56 | _try_play_animation(_postload_animation_name) 57 | _is_loading = false 58 | _progress_completion[0] = 0.0 59 | 60 | func _try_play_animation(animation_name: String) -> void: 61 | if _should_play_animation: 62 | _animator.play(animation_name) 63 | -------------------------------------------------------------------------------- /loading/loading.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=6 format=3 uid="uid://ckypo4jsjuepe"] 2 | 3 | [ext_resource type="Script" path="loading.gd" id="1"] 4 | [ext_resource type="Texture2D" uid="uid://cs551cnrh6mag" path="logo.png" id="2_7j08w"] 5 | 6 | [sub_resource type="Animation" id="7"] 7 | resource_name = "Postload" 8 | length = 0.2 9 | tracks/0/type = "value" 10 | tracks/0/imported = false 11 | tracks/0/enabled = true 12 | tracks/0/path = NodePath("background:modulate") 13 | tracks/0/interp = 1 14 | tracks/0/loop_wrap = true 15 | tracks/0/keys = { 16 | "times": PackedFloat32Array(0, 0.2), 17 | "transitions": PackedFloat32Array(1, 1), 18 | "update": 0, 19 | "values": [Color(1, 1, 1, 1), Color(1, 1, 1, 0)] 20 | } 21 | tracks/1/type = "value" 22 | tracks/1/imported = false 23 | tracks/1/enabled = true 24 | tracks/1/path = NodePath("background:visible") 25 | tracks/1/interp = 1 26 | tracks/1/loop_wrap = true 27 | tracks/1/keys = { 28 | "times": PackedFloat32Array(0, 0.2), 29 | "transitions": PackedFloat32Array(1, 1), 30 | "update": 0, 31 | "values": [true, false] 32 | } 33 | 34 | [sub_resource type="Animation" id="6"] 35 | resource_name = "Preload" 36 | length = 0.2 37 | tracks/0/type = "value" 38 | tracks/0/imported = false 39 | tracks/0/enabled = true 40 | tracks/0/path = NodePath("background:visible") 41 | tracks/0/interp = 1 42 | tracks/0/loop_wrap = true 43 | tracks/0/keys = { 44 | "times": PackedFloat32Array(0), 45 | "transitions": PackedFloat32Array(1), 46 | "update": 0, 47 | "values": [true] 48 | } 49 | tracks/1/type = "value" 50 | tracks/1/imported = false 51 | tracks/1/enabled = true 52 | tracks/1/path = NodePath("background:modulate") 53 | tracks/1/interp = 1 54 | tracks/1/loop_wrap = true 55 | tracks/1/keys = { 56 | "times": PackedFloat32Array(0, 0.2), 57 | "transitions": PackedFloat32Array(1, 1), 58 | "update": 0, 59 | "values": [Color(1, 1, 1, 0), Color(1, 1, 1, 1)] 60 | } 61 | tracks/2/type = "value" 62 | tracks/2/imported = false 63 | tracks/2/enabled = true 64 | tracks/2/path = NodePath("background/container/message:visible") 65 | tracks/2/interp = 1 66 | tracks/2/loop_wrap = true 67 | tracks/2/keys = { 68 | "times": PackedFloat32Array(0), 69 | "transitions": PackedFloat32Array(1), 70 | "update": 0, 71 | "values": [true] 72 | } 73 | 74 | [sub_resource type="AnimationLibrary" id="AnimationLibrary_p12k3"] 75 | _data = { 76 | "Postload": SubResource("7"), 77 | "Preload": SubResource("6") 78 | } 79 | 80 | [node name="loading" type="CanvasLayer"] 81 | layer = 2 82 | script = ExtResource("1") 83 | 84 | [node name="background" type="ColorRect" parent="."] 85 | unique_name_in_owner = true 86 | anchors_preset = 15 87 | anchor_right = 1.0 88 | anchor_bottom = 1.0 89 | grow_horizontal = 2 90 | grow_vertical = 2 91 | color = Color(0.145098, 0.145098, 0.164706, 1) 92 | 93 | [node name="container" type="VBoxContainer" parent="background"] 94 | layout_mode = 1 95 | anchors_preset = 8 96 | anchor_left = 0.5 97 | anchor_top = 0.5 98 | anchor_right = 0.5 99 | anchor_bottom = 0.5 100 | offset_left = -272.0 101 | offset_top = -157.0 102 | offset_right = 272.0 103 | offset_bottom = 157.0 104 | grow_horizontal = 2 105 | grow_vertical = 2 106 | alignment = 1 107 | 108 | [node name="logo" type="TextureRect" parent="background/container"] 109 | custom_minimum_size = Vector2(300, 300) 110 | layout_mode = 2 111 | size_flags_horizontal = 4 112 | texture = ExtResource("2_7j08w") 113 | expand_mode = 3 114 | stretch_mode = 4 115 | 116 | [node name="indent" type="Control" parent="background/container"] 117 | custom_minimum_size = Vector2(0, 50) 118 | layout_mode = 2 119 | 120 | [node name="message" type="Label" parent="background/container"] 121 | layout_mode = 2 122 | theme_override_font_sizes/font_size = 35 123 | text = "Loading..." 124 | horizontal_alignment = 1 125 | vertical_alignment = 1 126 | 127 | [node name="indent2" type="Control" parent="background/container"] 128 | custom_minimum_size = Vector2(0, 10) 129 | layout_mode = 2 130 | 131 | [node name="progress_bar" type="ProgressBar" parent="background/container"] 132 | unique_name_in_owner = true 133 | custom_minimum_size = Vector2(600, 0) 134 | layout_mode = 2 135 | max_value = 1.0 136 | 137 | [node name="animator" type="AnimationPlayer" parent="."] 138 | unique_name_in_owner = true 139 | process_mode = 3 140 | libraries = { 141 | "": SubResource("AnimationLibrary_p12k3") 142 | } 143 | --------------------------------------------------------------------------------