Partage de technologie

[Godot3.3.3] - Animation de transitions

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Animation de transitionScreenTransitionAnimation

Structure du projet

Ajoutez une scène, le nœud racine est CanvasLayer2D et renommé enTransition d'écran:

image-20240712002547221

Ajouter un nœud enfant CouleurRect etLecteur d'animation,exister CouleurRect Lieutenant-général Couleur (Couleur) mis en noir :

image-20240712002657042

venez Matériel, nouveau Shader(Ignorez-le pour l'instant)Shader Paramètre milieuEssuyer Image, sera ajouté plus tard, voici à quoi cela ressemblera une fois terminé) :

image-20240712002743876

Code de shader

exister Shader Tapez le code dans :

shader_type canvas_item;
// 申明 shader 的类型,因为是 2d 游戏,所以是 canvas_item 类型的

uniform sampler2D wipeImage;	// 全局变量 wipeImage
uniform float percent : hint_range(0, 1);	// 限制在 01 之间的百分比范围

void fragment() {
	float texVal = texture(wipeImage, UV).r;
	// 对图像的 r 通道进行采样
	
	if (texVal < percent) {
		// 在 Animation 中进行动态改变 percent 并且让 百分比不断变大,就可以有一个动态变透明的效果(虽然没有完全弄懂,但大致是这样没错的)
		COLOR.a = 0.0;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Lecteur d'animation

Ajoutez d'abord un script pour le nœud racine :

extends CanvasLayer

signal screen_covered

func emit_screen_covered():
	emit_signal("screen_covered")
    // 这个函数用来发送信号

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

puis ouvre Lecteur d'animation Ajouter àdéfaut Animation:

dans la première piste CouleurRect > matériel:Shader doit y avoirShader Cliquez sur la petite touche dans l'inspecteur pour ajouter des images d'animation.

image-20240712003234496

Gestionnaire de transition d'écran

Créer une nouvelle scène Gestionnaire de transition d'écran Et définissez-le comme scène chargée automatiquement dans les paramètres du projet, et ajoutez un script au nœud racine :

extends Node

var screenTransitionScene = preload("res://scenes/UI/ScreenTransition.tscn")

func transition_to_scene(scenePath):
	var screenTransition = screenTransitionScene.instance()
	add_child(screenTransition)
	yield(screenTransition, "screen_covered")
	get_tree().change_scene(scenePath)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

méthode d'appel

dans le projetLe menu DémarrerÀ titre d'exemple, le processus consistant à cliquer sur le menu Démarrer pour appeler la fonction est le suivant :

func on_playButton_pressed():
	$"/root/LevelManager".change_level
  • 1
  • 2

changer_de_niveau OuiGestionnaire de niveaufonction dans , modifiez-le simplement comme ceci :

func change_level(levelIndex):
	currentLevelIndex = levelIndex
	if (currentLevelIndex >= levelScenes.size()):
		currentLevelIndex = 0
	$"/root/SceenTransitionManager".transition_to_scene(levelScenes[currentLevelIndex].resource_path)	// 获取路径并通过动画过渡脚本来延迟调用场景的切换,达到淡入淡出的效果
  • 1
  • 2
  • 3
  • 4
  • 5