Appearance
unison.gameobject — 游戏对象
ECS 风格的 Game Object / Component 系统。
使用方式
python
from unison import gameobject类层次
GameObject
├── Transform (Component)
└── ComponentGameObject
游戏对象是 ECS 架构中的实体。
python
obj = gameobject.GameObject("Player")属性
| 属性 | 类型 | 说明 |
|---|---|---|
name | str | 对象名称(读写) |
active | bool | 是否激活(读写) |
方法
| 方法 | 说明 |
|---|---|
get_name() | 获取名称 |
set_name(name) | 设置名称 |
is_active() | 是否激活 |
set_active(active) | 设置激活状态 |
get_transform() | 获取 Transform 组件 |
add_component_transform() | 添加 Transform 组件 |
get_component_transform() | 获取 Transform 组件 |
update() | 更新对象 |
Component
所有组件的基类。
python
class MyComponent(gameobject.Component):
def update(self): ...方法
| 方法 | 说明 |
|---|---|
get_game_object() | 获取所属 GameObject |
is_enabled() | 是否启用 |
set_enabled(enabled) | 设置启用状态 |
update() | 更新组件 |
start() | 启动组件 |
Transform
变换组件,管理位置、旋转、缩放和层级。
python
t = obj.get_transform()
t.position = math3d.vector(10, 0, 5)
t.rotation = math3d.euler_rotation(0, 90, 0)
t.scale = math3d.vector(2, 2, 2)属性
| 属性 | 类型 | 说明 |
|---|---|---|
position | Vector3 | 本地位置(读写) |
rotation | Quaternion | 本地旋转(读写) |
rotation_euler | Vector3 | 欧拉角旋转(读写) |
scale | Vector3 | 本地缩放(读写) |
方法
| 方法 | 说明 |
|---|---|
get_world_position() | 世界空间位置 |
get_world_rotation() | 世界空间旋转 |
get_world_scale() | 世界空间缩放 |
get_world_matrix() | 世界变换矩阵 |
set_parent(parent) | 设置父变换 |
get_parent() | 获取父变换 |
get_children() | 获取子变换列表 |
get_forward() | 前方向量 |
get_up() | 上方向量 |
get_right() | 右方向量 |
translate(v) | 平移 |
rotate(v) | 旋转 |
rotate_around(point, axis, angle) | 绕点旋转 |