英文:
How to set godot4 game screen resolution when launched
问题
因为我的全屏Godot游戏将在许多平台上可用(例如Mac、Windows),所以我必须确保每次游戏首次启动时,它都会检查所在的操作系统和设备,并为设备设置正确的分辨率。我应该如何做到这一点?我想要类似于以下的代码:
func _ready():
device = project.get_device()
project_settings.set_setting("window_width", device.width)
project_settings.set_setting("window_height", device.height)
project_settings.save()
请注意,我写的代码并不是实际的GDScript,只是我想要的示例。
英文:
Because my fullscreen godot game would be avaliable on many platforms (e.g. mac, windows) so I have to make sure that everytime my game launches on it's first time, it checks the OS and device it's on and sets the correct resolution for the device. How do I do this? I want something like this:
func _ready():
device = project.get_device()
project_settings.set_setting("window_width", device.width)
project_settings.set_setting("window_height", device.height)
project_settings.save()
note that the code I wrote is not actual GDscript, it's just an example of something I want.
答案1
得分: 1
你可以这样设置项目设置:
ProjectSettings.set("display/window/size/viewport_width", 320)
ProjectSettings.set("display/window/size/viewport_height", 240)
但是在你的代码运行时,这太晚了。你可以改为获取窗口并设置大小,像这样:
get_window().size = Vector2(320, 240)
或者是视口:
get_viewport().size = Vector2(320, 240)
要识别平台,你可以使用 OS.get_name()
:
prints(OS.get_name())
或者使用 OS.has_feature()
。
但更好的做法是,不要通过代码来做这个。相反,进入项目设置并为你想要的平台定义覆盖设置。
例如,如果我想要为Android覆盖宽度,我选择Viewport Width:
然后在顶部的下拉列表中选择特性"android":
然后点击添加:
这将添加一个新的"Viewport Width.android"属性,它将在具有特性"android"的目标上生效(即在Android平台上):
你可以将其设置为你想要的任何值。
英文:
You can set project settings like this:
ProjectSettings.set("display/window/size/viewport_width", 320)
ProjectSettings.set("display/window/size/viewport_height", 240)
But for the time your code runs, it is too late for that. You could instead get the window and set the size, like this:
get_window().size = Vector2(320, 240)
Or the viewport:
get_viewport().size = Vector2(320, 240)
To identify the platforms you can use OS.get_name()
:
prints(OS.get_name())
Or work with OS.has_feature()
But better yet, don't do this by code. Instead go to Project Settings and define overrides for the platforms you want.
For example, if I want to override the width for Android, I select Viewport Width:
Then select the feature "android" from the drop down list at the top:
Then click on add:
That adds a new "Viewport Width.android" property that will be effective on targets that has the feature "android" (i.e. on Android platforms):
Which you can set to whatever you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论