Skip to content

GameViewService ​

编辑器内嵌游戏视窗的窗口比例服务,通过 editor:GetService("GameViewService") 获取。

概览 ​

GameViewService 负责读取与切换编辑器内嵌游戏视窗的窗口比例。窗口比例档位由运行时可枚举的 Enums.GameViewRatio 决定:Free(free)、Ratio4X3(4:3)、Ratio16X9(16:9)、Ratio16X10(16:10)、Ratio19X9(19:9)、Ratio20X9(20:9),GetViewRatio 返回对应档位的字符串值。它只改变内嵌游戏视窗本身的比例,不改变 EUI 画布或外层编辑器窗口尺寸。

注意事项 ​

窗口比例档位的公开取值由运行时可枚举的 Enums.GameViewRatio 决定:Free、Ratio4X3、Ratio16X9、Ratio16X10、Ratio19X9、Ratio20X9;GetViewRatio 返回对应档位的字符串值(如 free、4:3、16:9)。当前 Editor Meta 索引尚未收录 GameViewRatio 的独立枚举页面(上游索引缺口),以运行时可枚举值为准。

Get by:

lua
-- @runtime editor
local service = editor:GetService("GameViewService")

函数 ​

GetViewRatio ​

读取当前游戏视窗比例,返回 GameViewRatio 值;当当前视窗比例无法匹配 free 或预设档位时,返回空值。

返回值

类型说明
GameViewRatiofree 或预设档位;无法匹配时为空

读取当前游戏视窗比例

lua
-- @runtime editor
local gameViewService = editor:GetService("GameViewService")
local ratio = gameViewService:GetViewRatio()
if ratio ~= nil then
    print("当前游戏视窗比例:", ratio)
else
    print("当前游戏视窗比例为自由模式或无法匹配预设档位")
end
print(tostring(gameViewService))

SetViewRatio ​

将游戏视窗切换为指定的游戏视窗比例,入参为 GameViewRatio 类型的比例值。

参数

参数类型说明
ratioGameViewRatio取值见 Enums.GameViewRatio;非法值报 GAME_VIEW_RATIO_UNKNOWN,不修改视窗。

按当前比例重新应用游戏视窗比例

lua
-- @runtime editor
local gameViewService = editor:GetService("GameViewService")
local currentRatio = gameViewService:GetViewRatio()
if currentRatio ~= nil then
    gameViewService:SetViewRatio(currentRatio)
    local appliedRatio = gameViewService:GetViewRatio()
    print("已重新应用游戏视窗比例:", appliedRatio)
else
    print("当前没有可用的游戏视窗比例,未执行切换")
end
print(tostring(gameViewService))