Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Camera2D
繼承: Node2D < CanvasItem < Node < Object
2D 場景的相機節點。
說明
Camera node for 2D scenes. It forces the screen (current layer) to scroll following this node. This makes it easier (and faster) to program scrollable scenes than manually changing the position of CanvasItem-based nodes.
Cameras register themselves in the nearest Viewport node (when ascending the tree). Only one camera can be active per viewport. If no viewport is available ascending the tree, the camera will register in the global viewport.
This node is intended to be a simple helper to get things going quickly, but more functionality may be desired to change how the camera works. To make your own custom camera node, inherit it from Node2D and change the transform of the canvas by setting Viewport.canvas_transform in Viewport (you can obtain the current Viewport by using Node.get_viewport()).
Note that the Camera2D node's Node2D.global_position doesn't represent the actual position of the screen, which may differ due to applied smoothing or limits. You can use get_screen_center_position() to get the real position. Same for the node's Node2D.global_rotation which may be different due to applied rotation smoothing. You can use get_screen_rotation() to get the current rotation of the screen.
教學
屬性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
方法
void |
align() |
void |
|
get_drag_margin(margin: Side) const |
|
get_screen_center_position() const |
|
get_screen_rotation() const |
|
get_target_position() const |
|
is_current() const |
|
void |
|
void |
|
void |
set_drag_margin(margin: Side, drag_margin: float) |
void |
列舉
enum AnchorMode: 🔗
AnchorMode ANCHOR_MODE_FIXED_TOP_LEFT = 0
相機的位置是固定的,所以左上角總是在原點。
AnchorMode ANCHOR_MODE_DRAG_CENTER = 1
相機的位置要考慮垂直/水平偏移和螢幕尺寸。
enum Camera2DProcessCallback: 🔗
Camera2DProcessCallback CAMERA2D_PROCESS_PHYSICS = 0
相機在物理影格期間更新(見 Node.NOTIFICATION_INTERNAL_PHYSICS_PROCESS)。
Camera2DProcessCallback CAMERA2D_PROCESS_IDLE = 1
相機在程序影格期間更新(見 Node.NOTIFICATION_INTERNAL_PROCESS)。
屬性說明
AnchorMode anchor_mode = 1 🔗
void set_anchor_mode(value: AnchorMode)
AnchorMode get_anchor_mode()
The Camera2D's anchor point.
連接到 Camera2D 的自訂 Viewport 節點。如果為 null 或者不是 Viewport,則使用預設的視口。
float drag_bottom_margin = 0.2 🔗
拖動相機所需的下邊距。值為 1 時,相機僅在到達螢幕底部邊緣時移動。
bool drag_horizontal_enabled = false 🔗
如果為 true,相機僅在到達水平(左或右)拖動邊距時移動。如果為 false,則相機水平移動時不考慮邊距。
float drag_horizontal_offset = 0.0 🔗
相機在右側(-1)和左側(1)拖動邊距之間的相對水平拖動偏移量。
注意:用於設定初始水平拖動偏移量;確定目前偏移量;或強制目前偏移量。當 drag_horizontal_enabled 為 true 或更改拖動邊距時,它不會自動更新。
float drag_left_margin = 0.2 🔗
拖動相機所需的左邊距。值為 1 時,相機僅在到達螢幕左側邊緣時移動。
float drag_right_margin = 0.2 🔗
拖動相機所需的右邊距。值為 1 時,相機僅在到達螢幕右側邊緣時移動。
拖動相機所需的上邊距。值為 1 時,相機僅在到達螢幕頂部邊緣時移動。
bool drag_vertical_enabled = false 🔗
如果為 true,相機僅在達到垂直(頂部及底部)拖動邊距時才移動。如果為 false,相機會垂直移動而不管邊距。
float drag_vertical_offset = 0.0 🔗
相機在底部(-1)和頂部(1)拖動邊距之間的相對垂直拖動偏移量。
注意:用於設定初始垂直拖動偏移量;確定目前偏移量;或強制目前偏移量。當 drag_vertical_enabled 為 true 或更改拖動邊距時,它不會自動更新。
bool editor_draw_drag_margin = false 🔗
如果為 true,在編輯器中繪製相機的拖動邊距矩形。
bool editor_draw_limits = false 🔗
如果為 true,在編輯器中繪製相機的極限矩形。
bool editor_draw_screen = true 🔗
如果為 true,在編輯器中繪製相機的畫面矩形。
控制該相機是否可以啟動。如果為 true,當該 Camera2D 進入場景樹並且目前沒有活動的相機時,它將成為主相機(參見 Viewport.get_camera_2d())。
當該相機目前處於活動狀態且 enabled 被設定為 false 時,則場景樹中下一個啟用的 Camera2D 將變為活動狀態。
如果為 true ,相機的算繪視圖不會受到其 Node2D.rotation 和 Node2D.global_rotation 的影響。
底部滾動極限,單位為圖元。相機會在抵達該值時停止移動,但是 offset 可以把視圖推過該極限。
If true, the limits will be enabled. Disabling this will allow the camera to focus anywhere, when the four limit_* properties will not work.
左側滾動極限,單位為圖元。相機會在抵達該值時停止移動,但是 offset 可以把視圖推過該極限。
右側滾動極限,單位為圖元。相機會在抵達該值時停止移動,但是 offset 可以把視圖推過該極限。
如果為 true,相機會在達到極限時平滑地停止。
如果 position_smoothing_enabled 為 false,則該屬性無效。
注意:要立即將相機的位置更新到限制範圍內而不進行平滑,即使啟用了該設置,也要呼叫 reset_smoothing()。
頂部滾動極限,單位為圖元。相機會在抵達該值時停止移動,但是 offset 可以把視圖推過該極限。
Vector2 offset = Vector2(0, 0) 🔗
相機的相對偏移量。用於環顧四周或相機抖動動畫。偏移後的相機可以超過 limit_top、limit_bottom、limit_left 和 limit_right 中定義的限制。
bool position_smoothing_enabled = false 🔗
如果為 true,相機的視圖會以 position_smoothing_speed 的速度,平滑地移向其目標位置。
float position_smoothing_speed = 5.0 🔗
當 position_smoothing_enabled 為 true 時,相機平滑效果的速度,單位為每秒圖元。
Camera2DProcessCallback process_callback = 1 🔗
void set_process_callback(value: Camera2DProcessCallback)
Camera2DProcessCallback get_process_callback()
The camera's process callback.
bool rotation_smoothing_enabled = false 🔗
如果為 true,相機的視圖會通過漸近平滑的方式平滑地旋轉,以 rotation_smoothing_speed 的速度與其目標旋轉對齊。
注意:如果 ignore_rotation 為 true,則該屬性無效。
float rotation_smoothing_speed = 5.0 🔗
當 rotation_smoothing_enabled 為 true 時,相機旋轉平滑效果的角度漸近速度。
Vector2 zoom = Vector2(1, 1) 🔗
The camera's zoom. Higher values are more zoomed in. For example, a zoom of Vector2(2.0, 2.0) will be twice as zoomed in on each axis (the view covers an area four times smaller). In contrast, a zoom of Vector2(0.5, 0.5) will be twice as zoomed out on each axis (the view covers an area four times larger). The X and Y components should generally always be set to the same value, unless you wish to stretch the camera view.
Note: FontFile.oversampling does not take Camera2D zoom into account. This means that zooming in/out will cause bitmap fonts and rasterized (non-MSDF) dynamic fonts to appear blurry or pixelated unless the font is part of a CanvasLayer that makes it ignore camera zoom. To ensure text remains crisp regardless of zoom, you can enable MSDF font rendering by enabling ProjectSettings.gui/theme/default_font_multichannel_signed_distance_field (applies to the default project font only), or enabling Multichannel Signed Distance Field in the import options of a DynamicFont for custom fonts. On system fonts, SystemFont.multichannel_signed_distance_field can be enabled in the inspector.
方法說明
void align() 🔗
Aligns the camera to the tracked node.
Note: Calling force_update_scroll() after this method is not required.
void force_update_scroll() 🔗
強制相機立即更新滾動。
float get_drag_margin(margin: Side) const 🔗
返回指定邊 Side 的邊距。另見 drag_bottom_margin、drag_top_margin、drag_left_margin 和 drag_right_margin。
int get_limit(margin: Side) const 🔗
返回指定邊 Side 的相機極限。另見 limit_bottom、limit_top、limit_left 和 limit_right。
Vector2 get_screen_center_position() const 🔗
返回該 Camera2D 視角下的螢幕中心位置,使用全域座標。
注意:相機實際的目標位置可能與此不同。見 get_target_position()。
float get_screen_rotation() const 🔗
Returns the current screen rotation from this camera's point of view.
Note: The screen rotation can be different from Node2D.global_rotation if the camera is rotating smoothly due to rotation_smoothing_enabled.
Vector2 get_target_position() const 🔗
返回該相機的目標位置,使用全域座標。
注意:返回值與 Node2D.global_position 不同,因為會受到拖動屬性的影響。如果 position_smoothing_enabled 為 true ,也不等同於目前位置(見 get_screen_center_position())。
如果該 Camera2D 為活動相機,則返回 true(見 Viewport.get_camera_2d())。
void make_current() 🔗
強制該 Camera2D 成為目前的活動相機。enabled 必須為 true。
void reset_smoothing() 🔗
將相機的位置立即設定為其目前平滑的目標位置。
當 position_smoothing_enabled 為 false 時,本方法無效。
void set_drag_margin(margin: Side, drag_margin: float) 🔗
設定指定邊 Side 的邊距。另見 drag_bottom_margin、drag_top_margin、drag_left_margin 和 drag_right_margin。
void set_limit(margin: Side, limit: int) 🔗
設定指定邊 Side 的相機極限。另見 limit_bottom、limit_top、limit_left 和 limit_right。