主题
CameraService
Camera 相机服务。提供编辑相机的视角操控,以及运行时相机(地图默认相机)的参数读写与配置。
概览
编辑器相机服务,获取时使用 Camera(CameraService 是仅为兼容旧脚本保留的别名,新代码统一使用 Camera)。编辑器涉及两类相机:编辑相机用于观察、操作场景,支持切换视角模式、聚焦单位、重置视角与读取实时状态,状态不随地图保存;运行时相机即地图默认相机,通过 GetCurrentCamera 取得后可读写其属性调整玩家看到的画面,参数随地图保存。
Get by:
lua
-- @runtime editor
local service = editor:GetService("CameraService")函数
GetCurrentCamera
获取地图默认相机,返回一个相机对象,供脚本读取当前地图所使用的默认相机。该函数没有参数。
返回值
| 类型 | 说明 |
|---|---|
Camera | 地图默认相机对象;相机不可用时返回 nil |
读取默认相机
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
local camera = cameraService:GetCurrentCamera()
if camera ~= nil then
print("默认相机:", camera)
endGetEditorCameraMode
获取编辑相机当前的视角模式,返回 0=上帝视角、1=第一人称、2=引擎模式;编辑相机接口不可用时返回 nil。
返回值
| 类型 | 说明 |
|---|---|
Int | 编辑相机视角模式:0=上帝视角、1=第一人称、2=引擎模式;编辑相机接口不可用时返回 nil。 |
读取编辑相机视角模式
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
local mode = cameraService:GetEditorCameraMode()
if mode == nil then
print("编辑相机当前不可用")
else
print("当前编辑相机视角模式:", mode)
endSetEditorCameraMode
设置编辑相机的视角模式,编辑器视口即时生效。mode 支持 0=上帝视角、1=第一人称、2=引擎模式与 4=脚本模式(脚本接管相机控制);该接口无返回值。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
mode | Int | 0=上帝视角 1=第一人称 2=引擎模式 4=脚本模式 |
保持当前编辑器相机模式
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
local currentMode = cameraService:GetEditorCameraMode()
if currentMode ~= nil then
cameraService:SetEditorCameraMode(currentMode)
endResetCamera
重置编辑相机的视角,使编辑相机恢复到重置后的视角状态。该函数没有参数,也没有返回值。
重置编辑相机视角
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
cameraService:ResetCamera()FocusToUnit
将编辑相机聚焦到指定单位上,参数为单位对象,函数本身没有返回值。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
unit | Unit | 要聚焦的场景单位,相机将移动到该单位所在位置 |
聚焦当前选中的单位
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
local selection = editor:GetService("Selection")
local units = selection:Get()
local unit = units[1]
if unit ~= nil then
cameraService:FocusToUnit(unit)
endGetEditorProperties
获取编辑相机的实时状态快照,返回键包括 cur_mode_type(视角模式)、focus_pos(焦点)、camera_distance(焦距)、camera_pitch(俯仰)、camera_yaw(偏航)、camera_roll(翻滚)、camera_up(上方向)、camera_pos(机位)、camera_fov(视场角)与 camera_forward(朝向),向量类值为 {x, y, z} 数组;编辑相机接口不可用时返回 nil。
返回值
| 类型 | 说明 |
|---|---|
Table | 编辑相机实时状态键值表:cur_mode_type、focus_pos、camera_distance、camera_pitch、camera_yaw、camera_roll、camera_up、camera_pos、camera_fov、camera_forward;编辑相机接口不可用时返回 nil。 |
读取编辑相机状态中的常用键
lua
-- @runtime editor
local cameraService = editor:GetService("Camera")
local properties = cameraService:GetEditorProperties()
if properties ~= nil then
print("视角模式:", properties.cur_mode_type)
print("视场角:", properties.camera_fov)
print("机位:", properties.camera_pos)
end