Skip to content

EUIManager ​

概览 ​

字段值
KindData · 数据 Data
Realmcommon

EUIManager 是当前玩家 PlayerGui 上的 UI 管理器,用于读取设备分辨率、获取 UI 根节点、查询 EUI 节点,以及创建包 UI 模块和场景 UI 节点。

适用场景 ​

客户端 UI 脚本需要根据屏幕尺寸布局界面、管理当前玩家 UI 树,或创建挂接到场景单位的 UI 标记时使用。

使用要点 ​

在客户端通过 Players.LocalPlayer.PlayerGui.EuiManager 获取实例;再调用 GetDeviceResolution、GetRootNode、GetNodesByType 或场景 UI 创建接口。

注意事项 ​

EUIManager 是玩家 UI 树上的 UI 管理器,需从当前玩家的 PlayerGui.EuiManager 获取,不能通过 GetService 获取 EUIManager,也不能直接构造。普通 EUI 子节点通过 World:CreateUnit 创建时,必须把 Parent 设置为 EUIManager:GetRootNode();已存在节点可通过 GetRootNode、GetNodesByType、FindFirstChild 查询,场景 UI 节点则使用 EUIManager 的公开创建接口。

代码示例 ​

获取当前玩家的 EUIManager ​

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager

local resolution = euiManager:GetDeviceResolution()
print('屏幕尺寸:', resolution.x, resolution.y)

函数 (9) ​

GetDeviceResolution ​

签名:GetDeviceResolution() -> Vector2 (屏幕分辨率)

获取当前设备的屏幕分辨率,返回一个 Vector2。

返回值 Vector2 (屏幕分辨率)

示例代码

读取屏幕分辨率

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local resolution = euiManager:GetDeviceResolution()
print(resolution.x, resolution.y)

GetRootNode ​

签名:GetRootNode() -> EUIRootNode (根节点)

获取 RootNode,返回类型为 EUIRootNode。该方法无参数。

返回值 EUIRootNode (根节点)

示例代码

获取 UI 根节点

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local rootNode = euiManager:GetRootNode()
print(rootNode.Name)

GetNodesByType ​

签名:GetNodesByType(unitType: String) -> Array<EUINodeBase> (匹配节点列表)

按节点类型(unitType)获取所有匹配的节点,返回匹配节点列表(元素为 EUINodeBase)。

参数类型说明
unitTypeString单元类型(如 "EUIButton")

返回值 Array<EUINodeBase> (匹配节点列表)

示例代码

查询指定类型节点

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local labels = euiManager:GetNodesByType('EUITextLabel')
print('文本节点数量:', #labels)

CreatePackageUIModuleNode ​

签名:CreatePackageUIModuleNode(packageName: String, uiModule: String) -> EUINodeBase (根节点(模块未注册时返回 nil))

按包名(packageName)与 UI 模块名(uiModule)创建一个 package UI 模块节点,并返回该模块的根节点;成员说明中注明模块未注册时返回 nil。

参数类型说明
packageNameString包名
uiModuleStringUI 模块名

返回值 EUINodeBase (根节点(模块未注册时返回 nil))

示例代码

创建包 UI 模块节点

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local node = euiManager:CreatePackageUIModuleNode('MainPackage', 'InventoryPanel')
if node ~= nil then
    print('创建包 UI:', node.Name)
end

GetAllPackageNodes ​

签名:GetAllPackageNodes(packageName: String, uiModule: String) -> Array&lt;EUINodeBase&gt; (节点列表)

获取指定包名(packageName)下某个 UI 模块(uiModule)的全部节点,返回节点列表(元素为 EUINodeBase)。

参数类型说明
packageNameString包名
uiModuleStringUI 模块名

返回值 Array<EUINodeBase> (节点列表)

示例代码

获取包 UI 模块节点

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local nodes = euiManager:GetAllPackageNodes('MainPackage', 'InventoryPanel')
print('包 UI 节点数量:', nodes and #nodes or 0)

CreateSceneNodeAtPosition ​

签名:CreateSceneNodeAtPosition(position: Vector3, nodeInfo?: Any) -> EUISceneNode (场景 UI 节点)

在指定的世界坐标 position 处创建一个场景 UI 节点,并返回该场景 UI 节点。

参数类型说明
positionVector3世界坐标
nodeInfo?Any节点信息

返回值 EUISceneNode (场景 UI 节点)

示例代码

在世界坐标创建场景 UI

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local position = Vector3.New(0, 3, 0)
local nodeInfo = {}
local sceneNode = euiManager:CreateSceneNodeAtPosition(position, nodeInfo)
print(sceneNode)

CreateSceneUIByPrefabKeyAtPosition ​

签名:CreateSceneUIByPrefabKeyAtPosition(prefabKey: Int, position: Vector3) -> EUISceneNode (场景 UI 根节点)

根据预设 key(prefabKey)在世界坐标 position 处创建一整套场景 UI,并返回场景 UI 根节点。

参数类型说明
prefabKeyInt预设 key
positionVector3世界坐标

返回值 EUISceneNode (场景 UI 根节点)

示例代码

用预设创建场景 UI

lua
-- @runtime client
local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local prefabKey = 10001
local position = Vector3.New(0, 3, 0)
local sceneNode = euiManager:CreateSceneUIByPrefabKeyAtPosition(prefabKey, position)
print(sceneNode)

CreateSceneNodeAttachUnit ​

签名:CreateSceneNodeAttachUnit(Unit: Unit, Socket: String, Offset: Vector3, InheritVisible: Bool, nodeInfo?: Any) -> EUISceneNode (场景 UI 节点)

创建一个挂接到指定单位(Unit)某个插槽(Socket)上的场景 UI 节点,可设置相对插槽的偏移(Offset)以及是否继承单位可见性(InheritVisible)。

参数类型说明
UnitUnit挂接单位
SocketString挂点名
OffsetVector3偏移
InheritVisibleBool跟随单位可见性
nodeInfo?Any节点信息

返回值 EUISceneNode (场景 UI 节点)

示例代码

把场景 UI 挂接到已有单位

lua
-- @runtime client
local Players = game:GetService('Players')
local World = game:GetService('World')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local targetUnit = World:FindFirstChild('BillboardAnchor', true)
if targetUnit ~= nil then
    local nodeInfo = {}
    local sceneNode = euiManager:CreateSceneNodeAttachUnit(targetUnit, 'Head', Vector3.New(0, 1, 0), true, nodeInfo)
    print(sceneNode)
end

CreateSceneUIByPrefabKeyAttachUnit ​

签名:CreateSceneUIByPrefabKeyAttachUnit(prefabKey: Int, Unit: Unit, Socket: String, Offset: Vector3, InheritVisible: Bool) -> EUISceneNode (场景 UI 根节点)

根据预设 key(prefabKey)创建一整套场景 UI,并将其挂接到指定单位(Unit)的插槽(Socket)上,同时可设置偏移与是否继承可见性。

参数类型说明
prefabKeyInt预设 key
UnitUnit挂接单位
SocketString挂点名
OffsetVector3偏移
InheritVisibleBool跟随单位可见性

返回值 EUISceneNode (场景 UI 根节点)

示例代码

用预设创建并挂接场景 UI

lua
-- @runtime client
local Players = game:GetService('Players')
local World = game:GetService('World')
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local euiManager = localPlayer.PlayerGui.EuiManager
local targetUnit = World:FindFirstChild('BillboardAnchor', true)
if targetUnit ~= nil then
    local sceneNode = euiManager:CreateSceneUIByPrefabKeyAttachUnit(10001, targetUnit, 'Head', Vector3.New(0, 1, 0), true)
    print(sceneNode)
end