SwiftGodotKick
是一个程序,用于创建 SwiftGodot + SwiftGodotKit 项目的初始框架。
只需运行它
$ swift run swift-godot-kick
并按照提示操作。
$ swift run swift-godot-kick
Please enter where you would like the project directory to be created: ../KickExampleProject
There is no directory at path: ../KickExampleProject, would you like to create it? (y/n): y
Please enter the name of the project: KickExampleProject
Project will be created at: /home/user/Documents/Code/Swift/KickExampleProject/Package.swift, would you like to proceed? (y/n): y
Created /home/user/Documents/Code/Swift/KickExampleProject/Package.swift
Created /home/user/Documents/Code/Swift/KickExampleProject/README.md
Created /home/user/Documents/Code/Swift/KickExampleProject/.gitignore
Created /home/user/Documents/Code/Swift/KickExampleProject/.env
Created /home/user/Documents/Code/Swift/KickExampleProject/Sources
Created /home/user/Documents/Code/Swift/KickExampleProject/Sources/KickExampleProject/KickExampleProject.swift
Created /home/user/Documents/Code/Swift/KickExampleProject/Sources/KickExampleProjectGame/main.swift
Created /home/user/Documents/Code/Swift/KickExampleProject/GDD.md
Created /home/user/Documents/Code/Swift/KickExampleProject/godot
Created /home/user/Documents/Code/Swift/KickExampleProject/godot/project.godot
Created /home/user/Documents/Code/Swift/KickExampleProject/godot/KickExampleProject.gdextension
Created /home/user/Documents/Code/Swift/KickExampleProject/godot/export_presets.cfg
Created /home/user/Documents/Code/Swift/KickExampleProject/Makefile
run the following command:
cd /home/user/Documents/Code/Swift/KickExampleProject && make all
这将创建一个具有以下结构的项目
├── .env
├── GDD.md
├── godot
│ ├── bin
│ │ └── .gdignore
│ ├── export_presets.cfg
│ ├── exports
│ │ └── .gdignore
│ ├── .gitignore
│ ├── icon.svg
│ ├── MySwiftGodotKickProject.gdextension
│ └── project.godot
├── Makefile
├── Package.resolved
├── Package.swift
├── README.md
└── Sources
├── MySwiftGodotKickProject
│ └── MySwiftGodotKickProject.swift
└── MySwiftGodotKickProjectGame
├── main.swift
└── Resources
└── MySwiftGodotKickProject.pck
只要我们将所有资源导入 Godot,我们就可以在 Swift 中使用与 GDScript 中相同的 res://
路径来引用它们,方法是导出 Resource .pck 文件,并在使用 SwiftGodotKit 运行 Godot 时将其传递给 Godot。
godot/export_presets.cfg
文件被 make pack
用来创建 Sources/MySwiftGodotKickProjectGame/Resources/MySwiftGodotKickProject.pck
资源包。
重要提示:您必须已下载导出模板。
最终,这允许 .gdscript
和 Swift 代码通过将 MySwiftGodotKickProject.pck
资源文件放置在 Swift Package 目标的 Resources/
文件夹中,并在 Package.swift
清单中将该资源声明为资源,来使用 res://icon.svg
访问诸如 icon.svg
之类的资源,如下所示:
let package = Package(
...
targets: [
.executableTarget(
name: "MySwiftGodotKickProjectGame",
dependencies: [
"MySwiftGodotKickProject",
.product(name: "SwiftGodotKit", package: "SwiftGodotKit")
],
resources: [
.copy("Resources") // <-- here, MySwiftGodotKickProject.pck lives in this folder
]),
我们可以访问资源包,并将其传递给 runGodot
// Sources/MySwiftGodotKickProjectGame/main.swift
let packPath = Bundle.module.path(forResource: "MySwiftGodotKickProject", ofType: "pck")! // <-- here
runGodot(
args: [
"--main-pack", packPath // <-- and here
],
initHook: registerTypes,
loadScene: loadScene,
loadProjectSettings: { settings in }
)
所以我们可以这样做
// Icon2D.swift
let image = GD.load(path: "res://icon.svg") as! Texture2D