导出

概览

现在您已经有了一个可以运行的游戏,您可能想要和别人分享您的成果。然而,让您的朋友下载Godot而只是为了打开您的项目是不实际的。相反,您可以 导出 您的项目,将其转换为任何人都可以运行的 软件包

导出游戏的方式取决于您的目标平台。本教程中,您将学习如何将 Dodge the Creeps 游戏导出到各种平台。然而,首先我们需要对游戏的工作方式做一些改变。

注解

如果您还没有自己完成 Dodge the Creeps 游戏,请阅读 您的第一个游戏 后再继续本教程。

准备项目

Dodge the Creeps 中我们使用键盘控制 Player 角色的移动。如果您的游戏是在PC平台上玩的,这没问题,但在手机或平板上,您需要支持触屏输入。因为点击事件可以被视为触摸事件,我们将把游戏转换为点击移动输入风格。

默认情况下,Godot 从触摸输入模拟鼠标输入。这意味着为鼠标事件所写的代码也能被触摸事件触发。Godot 也可以从鼠标点击模拟触摸输入。如果我们将游戏转到触摸操纵, 要在电脑上继续玩就要用到这个功能。在”项目设置”下的*Input Devices*, Pointing 下, 设置*Emulate Touch From Mouse* 为 “启用”。

../../_images/export_touchsettings.png

我们也希望保证游戏在不同大小的屏幕上缩放正常。在项目设置中转到 显示(Display),然后点击 窗口(Window)。在 伸展(Stretch) 选项中,将 模式(Mode) 设置为 “2d”,将 纵横比(Aspect) 设置为”keep”。

由于我们已经在*窗口*设置中,我们还应该在*手持*的*方向*下设置为“纵向”。

../../_images/export_handheld_stretchsettings.png

接下来,我们需要修改 Player.gd 脚本来改变输入方式。我们将移除键盘输入并使 Player 移动到触摸(或点击)事件设置的 目标

这是 Player 的完整脚本,注释指出了我们做了哪些改变:

GDScript

C#

  1. extends Area2D
  2. signal hit
  3. export var speed = 400
  4. var screen_size
  5. # Add this variable to hold the clicked position.
  6. var target = Vector2()
  7. func _ready():
  8. hide()
  9. screen_size = get_viewport_rect().size
  10. func start(pos):
  11. position = pos
  12. # Initial target is the start position.
  13. target = pos
  14. show()
  15. $CollisionShape2D.disabled = false
  16. # Change the target whenever a touch event happens.
  17. func _input(event):
  18. if event is InputEventScreenTouch and event.pressed:
  19. target = event.position
  20. func _process(delta):
  21. var velocity = Vector2()
  22. # Move towards the target and stop when close.
  23. if position.distance_to(target) > 10:
  24. velocity = target - position
  25. # Remove keyboard controls.
  26. # if Input.is_action_pressed("ui_right"):
  27. # velocity.x += 1
  28. # if Input.is_action_pressed("ui_left"):
  29. # velocity.x -= 1
  30. # if Input.is_action_pressed("ui_down"):
  31. # velocity.y += 1
  32. # if Input.is_action_pressed("ui_up"):
  33. # velocity.y -= 1
  34. if velocity.length() > 0:
  35. velocity = velocity.normalized() * speed
  36. $AnimatedSprite.play()
  37. else:
  38. $AnimatedSprite.stop()
  39. position += velocity * delta
  40. # We still need to clamp the player's position here because on devices that don't
  41. # match your game's aspect ratio, Godot will try to maintain it as much as possible
  42. # by creating black borders, if necessary.
  43. # Without clamp(), the player would be able to move under those borders.
  44. position.x = clamp(position.x, 0, screen_size.x)
  45. position.y = clamp(position.y, 0, screen_size.y)
  46. if velocity.x != 0:
  47. $AnimatedSprite.animation = "walk"
  48. $AnimatedSprite.flip_v = false
  49. $AnimatedSprite.flip_h = velocity.x < 0
  50. elif velocity.y != 0:
  51. $AnimatedSprite.animation = "up"
  52. $AnimatedSprite.flip_v = velocity.y > 0
  53. func _on_Player_body_entered( body ):
  54. hide()
  55. emit_signal("hit")
  56. $CollisionShape2D.set_deferred("disabled", true)
  1. using Godot;
  2. using System;
  3. public class Player : Area2D
  4. {
  5. [Signal]
  6. public delegate void Hit();
  7. [Export]
  8. public int Speed = 400;
  9. private Vector2 _screenSize;
  10. // Add this variable to hold the clicked position.
  11. private Vector2 _target;
  12. public override void _Ready()
  13. {
  14. Hide();
  15. _screenSize = GetViewport().Size;
  16. }
  17. public void Start(Vector2 pos)
  18. {
  19. Position = pos;
  20. // Initial target us the start position.
  21. _target = pos;
  22. Show();
  23. GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
  24. }
  25. // Change the target whenever a touch event happens.
  26. public override void _Input(InputEvent @event)
  27. {
  28. if (@event is InputEventScreenTouch eventMouseButton && eventMouseButton.Pressed)
  29. {
  30. _target = (@event as InputEventScreenTouch).Position;
  31. }
  32. }
  33. public override void _Process(float delta)
  34. {
  35. var velocity = new Vector2();
  36. // Move towards the target and stop when close.
  37. if (Position.DistanceTo(_target) > 10)
  38. {
  39. velocity = _target - Position;
  40. }
  41. // Remove keyboard controls.
  42. //if (Input.IsActionPressed("ui_right"))
  43. //{
  44. // velocity.x += 1;
  45. //}
  46. //if (Input.IsActionPressed("ui_left"))
  47. //{
  48. // velocity.x -= 1;
  49. //}
  50. //if (Input.IsActionPressed("ui_down"))
  51. //{
  52. // velocity.y += 1;
  53. //}
  54. //if (Input.IsActionPressed("ui_up"))
  55. //{
  56. // velocity.y -= 1;
  57. //}
  58. var animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  59. if (velocity.Length() > 0)
  60. {
  61. velocity = velocity.Normalized() * Speed;
  62. animatedSprite.Play();
  63. }
  64. else
  65. {
  66. animatedSprite.Stop();
  67. }
  68. Position += velocity * delta;
  69. // We still need to clamp the player's position here because on devices that don't
  70. // match your game's aspect ratio, Godot will try to maintain it as much as possible
  71. // by creating black borders, if necessary.
  72. // Without clamp(), the player would be able to move under those borders.
  73. Position = new Vector2(
  74. x: Mathf.Clamp(Position.x, 0, _screenSize.x),
  75. y: Mathf.Clamp(Position.y, 0, _screenSize.y)
  76. );
  77. if (velocity.x != 0)
  78. {
  79. animatedSprite.Animation = "walk";
  80. animatedSprite.FlipV = false;
  81. animatedSprite.FlipH = velocity.x < 0;
  82. }
  83. else if(velocity.y != 0)
  84. {
  85. animatedSprite.Animation = "up";
  86. animatedSprite.FlipV = velocity.y > 0;
  87. }
  88. }
  89. public void OnPlayerBodyEntered(PhysicsBody2D body)
  90. {
  91. Hide(); // Player disappears after being hit.
  92. EmitSignal("Hit");
  93. GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
  94. }
  95. }

设置主场景

主场景是你的游戏会启动的场景。要更改任务栏图标,请转到 *项目→项目设置→Application→Run*中, 设置*Main Scene*为”Main.tscn” 。单击文件夹图标,然后选择Main.tscn即可。

导出模板

为了导出,您需要从 这里 下载 导出模板。这些模板是为每个平台预先编译的不带编辑器的引擎的优化版本。您还可以通过点击 编辑器->管理导出模板 在Godot中下载它们:

../../_images/export_template_menu.png

在出现的窗口中,您可以点击 下载 以获取与您的Godot版本匹配的模板版本。

../../_images/export_template_manager.png

注解

如果升级Godot,则必须下载与新版本匹配的模板,否则导出的项目可能无法正常工作。

导出预设

接下来,您可以通过点击 项目 -> 导出 来配置导出设置。

通过点击 添加... 并选择平台来创建新的导出预设。您可以使用不同的设置创建任意数量的预设。

../../_images/export_presets_window.png

窗口的底部有两个按钮。导出 PCK/ZIP 只创建项目数据的打包版本。它不包含可执行文件,因此该项目无法单独运行。

第二个按钮 导出项目 创建游戏的完整可执行版本,例如Android的 .apk 或Windows的 .exe

资源(Resources)特性(Features) 选项卡中,您可以自定义如何为每个平台导出游戏。我们可以先不考虑这些设置。

按平台导出

在本节中,我们将逐步介绍每个平台的流程,包括您需要的任何其他软件或要求。

PC (Linux/macOS/Windows)

导出PC平台在三个受支持的操作系统上的工作原理相同。打开导出窗口,然后点击 添加.. 以创建要创建的预设。 然后点击 导出项目,然后选择一个名称和目标文件夹。选择项目文件夹的 外部 位置。

单击 保存,引擎将构建导出文件。

注解

为macOS导出时,如果在macOS计算机上导出,则最终会得到一个 .dmg 文件,而在使用Linux或Windows时会生成一个 .zip 文件。无论哪种情况,压缩文件都包含一个macOS .app,您可以双击并运行它。

注解

在Windows上,如果希望导出的可执行文件的图标与默认图标不同,则需要手动更改它。请参阅: 更改Windows的应用程序图标

Android

小技巧

移动设备具有各种各样的功能。在大多数情况下,Godot的默认设置是可以工作的,但是移动开发有时更多的是艺术而不是科学,您可能需要做一些试验和寻找帮助,以使一切工作正常。

必须先下载以下软件,然后才能导出Android项目:

首次运行Android Studio时,点击 配置-> SDK Manager 并安装 Android SDK平台工具。这将安装Godot用来与您的设备通信的 adb 命令行工具。

接下来,通过在系统的命令行上运行以下命令来创建调试密钥库:

  1. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999

单击Godot中的 编辑器 -> 编辑器设置,然后选择 导出/Android 部分。在这里,您需要设置系统上Android SDK应用程序的路径以及刚刚创建的密钥库的位置。

../../_images/export_editor_android_settings.png

现在您可以开始导出了。点击 项目 -> 导出,并添加Android的预设(请参见上文)。

点击 导出项目 按钮,Godot将生成一个APK,您可以下载该APK到设备上。为此,请在命令行中使用以下命令:

  1. adb install dodge.apk

注解

我们的设备可能需要处于 开发者模式。有关详细信息,请查阅设备的文档。

如果您的系统支持,则连接兼容的Android设备将在Godot的测试按钮区域中显示 一键部署 按钮:

../../_images/export_android_oneclick.png

点击此按钮可一步完成构建APK,并将其复制到您的设备上。

iOS

注解

为了构建适用于iOS的游戏,您必须有一台运行MacOS且安装了Xcode的计算机。

在导出之前,您 必须 完成一些设置才能成功导出项目。首先是 苹果商店团队Id,您可以登录到您的苹果开发者账户并在 会员 一栏中找到它。

您还必须提供图标和启动屏幕图像,如下所示:

../../_images/export_ios_settings.png

点击 导出项目 并选择目标文件夹。

成功导出项目后,将在所选位置找到以下文件夹和文件:

../../_images/export_xcode_project_folders.png

现在,您可以在Xcode中打开项目并为iOS构建项目。Xcode的构建过程超出了本教程的范围。有关更多信息,请参见 这里

HTML5 (网页)

点击HTML5预置中的 导出项目。我们不需要更改任何默认设置。

导出完成后,您将拥有一个包含以下文件的文件夹:

../../_images/export_web_files.png

在浏览器中查看 .html 文件可以让您玩游戏。但是,您无法直接打开文件,它需要由Web服务器提供。如果您的计算机上还没有设置一个,则可以在线搜索以查找有关特定操作系统的建议。

将浏览器指向放置html文件的URL。在载入游戏之前,您可能需要等待片刻,然后才能看到开始屏幕。

../../_images/export_web_example.png

游戏下方的控制台窗口会告诉您是否出了问题。您可以通过在导出项目时将 使用调试导出 关闭来禁用它。

../../_images/export_web_export_with_debug_disabled.png

注解

While WASM is supported in all major browsers, it is still an emerging technology and you may find some things that don’t work. Make sure you have updated your browser to the most recent version, and report any bugs you find at the Godot GitHub repository.