ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Unity Physics for DOTS 样例项目详解:EntityComponentSystemSamples 中 PhysicsSamples 的场景指南、目录结构与实现剖析

Unity Physics for DOTS 样例项目详解:EntityComponentSystemSamples 中 PhysicsSamples 的场景指南、目录结构与实现剖析 Unity Physics for DOTS 样例项目详解EntityComponentSystemSamples 中 PhysicsSamples 的场景指南、目录结构与实现剖析【免费下载链接】EntityComponentSystemSamples项目地址: https://gitcode.com/GitHub_Trending/en/EntityComponentSystemSamples本文基于仓库中 PhysicsSamples/README.md 展开系统梳理 Unity PhysicsDOTS 版物理引擎官方样例工程覆盖 46 个演示场景的完整清单、Game/Scene 窗口的操作与调试显示方式、场景编号与仓库目录的对应关系并结合同仓库源码剖析碰撞体运行时修改、立即模式物理步进等关键实现帮助你在 Unity 6.2 环境下快速上手并深入理解 ECS 物理的 Authoring/System 工作流。项目定位与运行环境PhysicsSamples是 EntityComponentSystemSamples 仓库中专注于 Unity Physics for DOTS 的 Unity 工程。仓库主页 README.md 明确说明本仓库中的样例工程基于 Unity 6.2并使用Entities、Netcode、Physics、Entities.Graphics四个包的 1.4 系列版本。从 PhysicsSamples/Packages/manifest.json 可以看到具体的依赖锁定包版本作用com.unity.entities1.4.3ECS 框架核心com.unity.entities.graphics1.4.16ECS 渲染桥接Hybrid Renderercom.unity.physics1.4.3Unity Physics 物理引擎com.unity.render-pipelines.universal17.2.0URP 渲染管线com.unity.inputsystem1.14.2新输入系统同时 manifest 中声明了testables: [com.unity.physics]意味着该工程内置了针对 Unity Physics 包的 PlayMode 测试套件位于PhysicsSamples/Assets/Tests/目录包含 900 余个文件。从源码结构看工程用 UnityPhysicsSamples.ASMDEF 定义统一程序集并通过 csc.rsp 配置编译器参数保证样例代码在 Burst/AOT 环境下的一致编译行为。演示场景的通用操作方式README 中Controls一节给出了所有演示场景在Game 窗口下的统一交互约定鼠标弹簧Mouse spring按住左键拖动——把选中的物理实体像弹簧一样拉拽释放后由物理求解器产生回弹相机旋转Camera rotate按住右键拖动相机平移Camera moveW、A、S、D 键。这套约定由公共输入脚本驱动公共输入动作定义在 PhysicsSamples/Assets/Common/InputActions.inputactions输入采集逻辑见 DemoInputGatheringSystem.cs。调试显示Scene 视图与 Game 视图的差异这是 README 中一个容易被初学者忽略的要点许多演示如 Query 系列的 ray cast、distance cast 等会把额外信息以 debug gizmo 的形式绘制在 Editor 的Scene视图中而不是Game视图。因此调试时要同时关注两个窗口。README 原表对比如下Scene view调试 Gizmo 显示在这里Game view实际渲染画面这一行为与 ReleaseNotes 中记录的一处修复相呼应QueryTester.cs用于 3a/3b/3c 查询演示曾因调试绘制系统未正确调度导致调试线不显示修复说明强调任何使用PhysicsDebugDisplaySystem绘制能力的系统必须调度在PhysicsDebugDisplayGroup内。这解释了为什么调试显示依赖 Scene 视图的 Editor 绘制管线而不是 Game 视图的渲染输出。完整场景清单README 的核心是一张 46 行的场景总表按Hello World / Setup / Query / Joints / Modify / Use Case六大类组织每行标注场景名、描述、难度Introductory / Intermediate / Advanced与演示截图。以下为完整继承的清单图片路径已转换为本仓库相对路径CategorySceneDescriptionLevelDemoHello WorldHello World.unityIntroductory scene for rigid body setupIntroductoryHello WorldSphereAndBoxColliders.unityBasic collidersIntroductoryHello WorldGravityWell.unityIntroductory sceneIntroductorySetup2a1. Collider Parade - Basic.unityDemo showing various shapes for collision detectionIntroductorySetup2a2. Collider Parade - AdvancedDemo showing various shapes for more advanced collision detectionIntroductorySetup2b1. Motion Properties - Mass.unityDemo showing how to explicitly set mass properties using custom (yellow) and built-in (grey) authoring componentsIntroductorySetup2b2. Motion Properties - Velocity.unitySetting initial linear and angular velocitiesIntroductorySetup2b3. Motion Properties - Damping.unityDemo showing the effect of linear and angular dampingIntroductorySetup2b4. Motion Properties - Gravity Factor.unityDemo showing the effect of per body gravity multipliersIntroductorySetup2b5. Motion Properties - Center of Mass.unityDemo showing the effect of overriding center of massIntroductorySetup2b6. Motion Properties - Inertia Tensor.unityDemo showing the effect of overriding inertia tensorIntroductorySetup2b7. Motion Properties - Smoothing.unityDemo showing the effect of interpolation and extrapolationIntroductorySetup2c1. Material Properties - Friction.unityShowing effect of different friction material valuesIntroductorySetup2c2. Material Properties - Restitution.unityShowing effect of different restitution valuesIntroductorySetup2c3. Material Properties - Collision Filters.unityShowing effect of different collision filtersIntroductorySetup2d1. Events - Triggers.unityDemo demonstrating the usage of triggersIntroductorySetup2d2. Events - Contacts.unityShowing effect of different contactsIntroductoryQuery3a. All Hits Distance Test.unityDemo showing results of distance queries between multiple collidersIntroductoryQuery3b. Cast Test.unityDemo showing the results of collider casting and ray castingIntroductoryQuery3c. Closest Hit Distance Test.unityDemo showing results of distance queriesIntroductoryQuery3d. Custom Collector.unityDemonstration of raycastIntroductoryJoints4a. Joints Parade.unityDemo showing a range of joint typesIntroductoryJoints4b. Limit DOF.unityShowing effect of limiting degrees of freedomIntroductoryJoints4c1. All Motors Parade.unityDemo showing different motorsIntroductoryJoints4c2. Position Motor.unityDemo showing position motorIntroductoryJoints4c3. Linear Velocity Motor.unityShowing linear velocity motorIntroductoryJoints4c4. Angular Velocity Motor.unityDemonstrating angular velocity motorIntroductoryJoints4c5. Rotational Motor.unityDemonstrating rotational motorIntroductoryJoints4d. Ragdolls.unityObligatory stack of ragdolls demoIntroductoryJoints4e. Single Ragdoll.unityGameObject ragdoll for Unity Physics (left) and built-in physics (right), created using the Ragdoll WizardIntroductoryModify5a. Change Motion Type.unityDemo showing change of motion typeIntroductoryModify5b. Change Box Collider Size.unityDemonstrating runtime change of collider sizeIntroductoryModify5c. Change Collider Type.unityDemonstrating change of collider typeIntroductoryModify5d. Change Velocity.unityDemo showing change of velocityIntroductoryModify5e. Kinematic Motion.unityDemo showing kinematic motion in combination with dynamic objectsIntroductoryModify5f. Change Surface Velocity.unityDemo showing change of surface velocityIntroductoryModify5g1. Change Collider Material - Bouncy Boxes.unityDemo showing effect of unique prefab instantiation with collider material changesIntermediateModify5g2. Unique Collider Blob Sharing.unityDemo showing effect of instantiating prefabs during runtime, making collider blobs unique and sharing collider blob dataAdvancedModify5g3. Runtime Collider Creation.unityCreate mesh colliders during runtimeAdvancedModify5g4. Runtime Collision Filter Modification.unityModify collision filters during runtimeAdvancedModify5g5. Modify Collider Geometry.unityModify collider geometry during runtimeAdvancedModify5h. Change Scale.unityDemo showing scale change of entitiesIntroductoryModify5i. Apply Impulse.unityDemo showing application of impulsesIntroductoryModify5j. Modify Broadphase Pairs.unityFilter out collision by explicitly deleting pairs from broad phaseAdvancedModify5k. Modify Contact Jacobians.unityModify the results of contact generation to produce special effectsAdvancedModify5l. Modify Narrowphase Contacts.unityAdd new user contacts to simulation pipelineAdvancedUse Case6a. Character Controller.unityUse case demo showing a rudimentary FPS character controllerIntermediateUse Case6b. Pool.unityDemonstration of calling immediate mode physicsIntermediateUse Case6c. Planet Gravity.unityPerformance demo of asteroids around a planet using SP/HPIntroductoryUse Case6d. Raycast Car.unityUse case demo showing a set of vehicle behaviorsIntermediate场景编号与仓库目录的对应关系值得注意的一点README 表格中的编号1–6 大类是演示教学顺序而仓库目录是按 ReleaseNotes.md 中 1.2.0 版本提到的 Reorganized the samples, with cleanup of various samples code and scenes 重新组织后的主题目录两者并非一一对应。对照 PhysicsSamples/Assets 的实际结构映射关系大致如下README 分类实际场景所在目录说明Hello World1PhysicsSamples/Assets/1. Hello World/含HelloWorld.unity、SphereAndBoxColliders.unity刚体字母由Letter.prefab与Alphabet.fbx构成Hello World1GravityWellPhysicsSamples/Assets/2. Gravity Well/GravityWell.unity配套 GravityWellSystem.cs 演示自定义力场 SystemSetup - Collider Parade2aPhysicsSamples/Assets/3. Collision Geometry/基础/进阶碰撞形状演示Setup - Motion Properties2bPhysicsSamples/Assets/4. Motion Properties/Mass、Velocity、Damping、Gravity Factor、Center of Mass、Inertia Tensor、Smoothing 共 7 个子场景配套PhysicsMassBehaviourAuthoring.cs、MotionSmoothingAuthoring.cs等 Authoring 脚本Setup - Material Properties2cPhysicsSamples/Assets/5. Material Properties/摩擦、弹性、碰撞过滤器Setup - Events2dPhysicsSamples/Assets/6. Events/Triggers含 Change Material、Force Field、Gravity Factor、Portals 四个子场景与 Contacts/CollisionsQuery3PhysicsSamples/Assets/7. Queries/距离查询、Collider Cast、Custom Collector配套CastQueries.prefab、ClosestDistanceQueries.prefabJoints Motors4PhysicsSamples/Assets/8. Joints and Motors/关节类型巡礼、Limit DOF、五种 Motor、RagdollModify5PhysicsSamples/Assets/9. Modify/13 个运行时修改演示脚本集中于9. Modify/Scripts/Use Case610. Immediate Mode、11. Planet Gravity、12. Raycast Car、13. Character ControllerPool立即模式、行星重力性能演示、射线车、FPS 角色控制器各占一个独立编号目录另外每个演示目录下的实际.unity场景文件大多放在子目录如Subscenes/、Raycast Car/Raycast Car.unity主目录的.unity是外层编排场景——这是 DOTS 子场景Subscene的典型组织方式。源码级实现剖析运行时修改碰撞体Authoring Blob 数据的分工Modify系列是本工程难度跨度最大的部分Introductory 到 Advanced其脚本都集中在 PhysicsSamples/Assets/9. Modify/Scripts/其中能直接体现 Unity Physics 核心机制的几个例子ColliderBakeTransformAuthoring.cs公共基础设施见 Common/Scripts支撑5g5. Modify Collider Geometry演示。从源码可以看到其字段设计Translation、Rotation、Scale、ShearXY/XZ/YZ三组剪切向量、AnimationDuration动画周期以及防漂移机制DriftPrevention、DriftErrorThreshold 0.05f。Baker 把这些参数烘焙为 ECS 组件ColliderBakeTransform并保存BlobAssetReferenceUnity.Physics.Collider OriginalCollider与PostTransformMatrix OriginalPostTransformMatrix——这正是 README 表格中Modify collider geometry during runtime的底层手段通过Collider.BakeTransform对碰撞体几何施加仿射变换旋转、平移、缩放、剪切实现循环形变动画。组件里还定义了ICleanupComponentDataSaveColliderBlobForDisposal用于子场景销毁时正确释放碰撞体 Blob这一点在 ReleaseNotes.md 的 1.3.0 修复记录中得到了印证修复了ColliderBakeTransformSystem的内存泄漏。ChangeColliderBlobAuthoring.cs/ChangeColliderTypeAuthoring.cs/ChangeBoxColliderSizeAuthoring.cs分别对应5g1 Bouncy Boxes、5c. Change Collider Type、5b. Change Box Collider Size展示修改 BlobAssetReference 指向的数据与替换组件引用两种运行时改碰撞体的路径。ModifyBroadphasePairsBehaviour.cs、ModifyContactJacobiansBehaviour.cs、ModifyNarrowphaseContactsBehaviour.cs对应三个 Advanced 级演示展示在 broadphase 中显式删除碰撞对、修改接触雅可比、向仿真管线注入用户接触这几种干预物理管线的高级用法。立即模式物理步进Pool 演示的支撑工具6b. Pool演示Demonstration of calling immediate mode physics依赖 ImmediatePhysicsWorldStepper.cs。从源码看它是一个IDisposable结构体内部持有独立的SimulationContext及条件编译下的HavokSimulationContextCreate()工厂方法显式声明不适合在 ECS job 中作为字段使用——因为它在主线程上立即运行一次物理步进与正常 ECS 系统调度解耦。这类旁路世界用法在 ReleaseNotes.md 0.10.0-preview 条目中还有更完整的说明Tests/MultipleWorlds下的 Animation 场景用独立PhysicsWorld模拟非关键动画体马尾、剑鞘ClientServer 场景则演示了服务端/客户端双物理世界的 ghost body 驱动模式。公共基础设施目录PhysicsSamples/Assets/Common/ 汇集了所有演示共享的组件与工具值得单独浏览Scripts/CameraControl.cs、CameraSmoothTrack.cs实现 READMEControls一节的相机旋转/平移约定Mouse/目录鼠标弹簧选择Mouse PickReleaseNotes 中多次提到其演进如MouseHoverAuthoring支持渲染组件位于子实体的情况并在4d. Ragdolls场景中启用EntityTracker.cs、SceneCreationSystem.cs、GridSpawner.cs演示实体生命周期追踪、运行时场景创建与网格生成Tests/子目录演示工程内嵌的行为验证工具SimulationValidationAuthoring组件用于确认关节按预期工作、刚体能否静止。自定义 Authoring 体验的自动导入根据 ReleaseNotes.md 的 1.0.0 条目围绕PhysicsBodyAuthoring/PhysicsShapeAuthoring组件的Custom Physics Authoring样例已从包 API 中剥离改由 Unity Physics 包以 Sample 形式自动导入落在 PhysicsSamples/Assets/Samples/ 下的Unity Physics/包版本/Custom Physics Authoring。仓库中该目录当前对应1.4.3版本ReleaseNotes 同时提醒升级 Unity Physics 包后需在 Package Manager 的Samples页签重新导入新版本样例并手动清理旧版本目录。PlayMode 测试套件PhysicsSamples/Assets/Tests/ 是规模可观的验证层223 个.unity场景、95 个.cs脚本目录涵盖 JointTestHinge、Prismatic、LimitedHinge 等、Stacking、Character、CharacterController、MultipleWorlds、Performance、SchedulerTest、StaticOptimize、StreamingStressTest 等。ReleaseNotes 显示多个关节演示场景已通过SimulationValidationAuthoring接入 PlayMode 自动验证即演示与回归测试共用同一套验证基础设施。版本演进要点PhysicsSamples/ReleaseNotes.md 记录了从 2019 年0.0.1-preview到当前1.3.0的完整演进其中对使用者最有价值的几个节点1.3.0Joints Parade 的BreakJoint1MaxImpulse 从 (10,10,10) 调为 (2,2,2)新增Modify Collider Geometry演示与ColliderBakeTransformAuthoring组件自定义 Authoring 组件的 Motor 支持 spring/damping 字段。1.2.0样例整体重组织与代码清理即上文目录重排的依据。1.1.0新增4e. Single Ragdoll用内置 Ragdoll Wizard 创建的 GameObject 布娃娃左右对比 DOTS 物理与内置物理新增运行时创建 Mesh Collider 的三个MeshCollider.Create重载 API支持UnityEngine.Mesh、MeshData、MeshDataArray新增Force Unique Collider Authoring组件与5g2. Unique Collider Blob Sharing、5g4. Runtime Collision Filter Modification演示。1.0.0Custom Physics Authoring 样例改为包样例自动导入见上节。更早版本0.x 系列还记录了 Character Controller 的隧道检测修复、ImmediatePhysicsWorldStepper引入、MultiWorld 演示等内容对理解各演示的设计动机有帮助。上手路径建议环境准备用 Unity 6.2 打开PhysicsSamples目录manifest 锁定的包版本会由 Package Manager 解析确认com.unity.physics1.4.3 正常解析。按难度递进从1. Hello World刚体基础→3./4./5.的 Setup 类场景碰撞形状、运动属性、材质→7. Queries查询与调试显示注意 Scene 视图→8. Joints and Motors→9. Modify重点看 5g 系列的 Blob 修改→10.–13.的四个 Use Case。读源码对照每个演示目录下都有同名 Authoring/Behaviour 脚本按Authoring编辑器烘焙→ Baker → 组件 → System运行时修改的链路阅读可把 README 表格中每个演示对应到具体 API 调用。扩展学习仓库同时提供了 Dots101/Physics101/ 入门工程ActivationPlates、GravityWell、LaserSight、Pachinko 等小例子README.md 主页将其与 Unity Physics 101 文档配套推荐PhysicsSamples内Assets/Tests/的验证场景也可作为回归测试范例参考。参考路径索引核心文档PhysicsSamples/README.md、PhysicsSamples/ReleaseNotes.md、PhysicsSamples/TestRunnerOptions.json依赖与工程配置PhysicsSamples/Packages/manifest.json、PhysicsSamples/ProjectSettings/演示场景PhysicsSamples/Assets/1. Hello World/ 至 PhysicsSamples/Assets/13. Character Controller/公共实现PhysicsSamples/Assets/Common/Scripts/、PhysicsSamples/Assets/9. Modify/Scripts/测试场景PhysicsSamples/Assets/Tests/演示截图与动图PhysicsSamples/READMEimages/【免费下载链接】EntityComponentSystemSamples项目地址: https://gitcode.com/GitHub_Trending/en/EntityComponentSystemSamples创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表