需求背景
Unity 提供了以下特性:
-
SceneManager.LoadScene允许指定LoadSceneMode为Additive,此时允许一个界面上同时运行多个 Scene。 -
Scene提供了 APIGetPhysicsScene,开发者可以获取到 PhysicScene 后调用PhysicsScene.Simulate手动更新物理引擎更新 - 由于上述 2 个特性,Unity 实际可以同时运行多个独立的
PhysicsScene,开发者可以自定义不同的更新帧率去更新它们。
这对于多人游戏场景下,实现 实现物理同步时的客户端预测 非常实用。
例子:https://github.com/spectre1989/unity_physics_csp
目前的问题
- Cocos 仅支持单 Scene 运行,并且 PhysicSystem 是全局单例
- 目前 PhysicSystem 的更新和视图层更新绑定(都由 director.tick 驱动)
这些导致想离屏单独运行一个物理场景,来单独完成物理的预测计算,变得困难
建议
-
PhysicSystem不再是一个单例,而是与Scene绑定,一个Scene有一个PhysicSystem(叫PhysicScene可能更贴切) - 支持多个 Scene 同时运行(以叠加方式 loadScene),某些 Scene 可能不跑渲染(没有 Camera)
- 增加
PhysicSystem.autoUpdate的选项,来支持手动更新物理系统,如 Physics-autoSimulation - Unity 脚本 API
例如:
// 以叠加方式 loadScene
let scene = await director.loadSceneAdditive('XXXScene');
// Physic 不再是全局单例,跟着场景走
let physicScene = scene1.getPhysicScene();
// 手动步进物理
physicScene.autoUpdate = false;
physicScene.update(dt);