一键导入
unity-physicscore2d-shapes-advanced
Advanced shape features including chains, compounds, rounded polygons, ellipses, and runtime geometry modification
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Advanced shape features including chains, compounds, rounded polygons, ellipses, and runtime geometry modification
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Batching techniques for queries, projectiles/shooters, and swarm/flocking behaviors (boids)
Collision detection, contact manifolds, collision responses, determinism, and character collision handling
Component authoring patterns and best practices for Unity PhysicsCore2D
High-level patterns and gameplay strategies for destructible objects in Unity PhysicsCore2D — slicing mechanics, fragmenting on impact, sprite-based destructible systems, debris cleanup, performance budgeting. Use for "how should I design destructible X?" questions. For the PhysicsDestructor type API and worked code examples see unity-physicscore2d-destructor; for raw member signatures see unity-physicscore2d-destructor-api.
Factory patterns for creating complex physics objects (ragdolls, soft bodies, vehicles, gears, mechanical systems)
Collision filtering, layer-based interactions, custom collision filters, and trigger areas
| name | unity-physicscore2d-shapes-advanced |
| description | Advanced shape features including chains, compounds, rounded polygons, ellipses, and runtime geometry modification |
You are now acting as a Unity PhysicsCore2D advanced shapes expert, specialized in complex shape types and runtime geometry manipulation.
Beyond basic circle and polygon shapes, PhysicsCore2D supports advanced shape features:
Reference examples from the PhysicsExamples2D repository:
Chain shapes connect multiple vertices to form edges:
Use cases:
PhysicsChain vs standalone ChainSegmentGeometryThere are two ways to put a connected chain of edges into the world. Pick deliberately — they have different ownership and mutation rules.
1. PhysicsChain.Create(body, ChainGeometry, PhysicsChainDefinition) — owned chain.
The chain owns its ShapeType.ChainSegment shapes; ghost vertices are stitched automatically from the vertex list and isLoop. Individual segments cannot be destroyed via PhysicsShape.Destroy — only via PhysicsChain.Destroy (which tears down all segments together). Use this when the chain is one logical entity (a piece of terrain, a track, a closed loop).
2. ChainSegmentGeometry.CreateSegments(vertices, transform, isLoop, allocator) + PhysicsShape.CreateShape(body, ChainSegmentGeometry, ...) — unowned segments.
CreateSegments returns a NativeArray<ChainSegmentGeometry> with ghosts already stitched from the vertex list (same rules as PhysicsChain — isLoop controls how the first/last segments' ghosts are filled). You then create one PhysicsShape per geometry yourself (or use CreateShapeBatch). The resulting shapes are regular ShapeType.ChainSegment shapes that you own — they can be destroyed individually with PhysicsShape.Destroy, mixed with segments built by hand, and renamed/replaced one-at-a-time without disturbing siblings.
The standalone path is what you want when:
Two new mutation paths avoid destroy/recreate:
PhysicsShape.chainSegmentGeometry { get; set; } — assign a new ChainSegmentGeometry to an existing chain-segment shape to update its endpoints and ghost vertices. Wakes the body. The shape's contact state is preserved (no destroy/recreate cost). Works on shapes you created via the standalone path and segments owned by a PhysicsChain.PhysicsChain.UpdateVertices(ReadOnlySpan<Vector2> vertices, bool isLoop) — bulk update of every segment in an owned chain. Vertex count and isLoop must match the original or you get a warning. Recalculates contacts; can produce overlaps/tunnelling if the new shape moves far from the old, so use carefully on dynamic/kinematic bodies.For mass per-segment edits across a non-PhysicsChain set, prefer setting chainSegmentGeometry on each shape over destroying and recreating — it skips contact-graph teardown and AABB-tree churn.
Multiple shapes attached to a single body:
Use cases:
Polygons with beveled or rounded corners:
Ellipses are approximated using many-sided polygons:
Shapes can be modified at runtime:
Important considerations:
PolygonGeometry mutationFor polygons specifically, prefer the manual write + Validate() pattern over the static PolygonGeometry.Create(span, radius) factory when:
Create logs a verticesHullIsValid error from inside the engine before it returns; checking poly.isValid after the fact does not suppress it. Validate() is silent and just sets isValid = false.stackalloc/span ceremony.PolygonGeometry poly = default; // or an existing polygon you're mutating
poly.count = 3; // 3..PhysicsConstants.MaxPolygonVertices
ref var pv = ref poly.vertices; // ref into the inline ShapeArray
pv[0] = new Vector2(x0, y0);
pv[1] = new Vector2(x1, y1);
pv[2] = new Vector2(x2, y2);
poly = poly.Validate(); // single engine call: refreshes centroid/normals, sets isValid
if (poly.isValid) shape.polygonGeometry = poly;
Same single-engine-call cost as Create.
⚠️ Validation discipline: any edit to vertices, count, or normals invalidates the polygon's snapshot (isValid, centroid, normals go stale). You must call poly = poly.Validate() and check isValid before assigning the polygon to a shape, passing it to a query, or storing it in a buffer that a later reader will trust. The one exception is radius — it's a Minkowski offset that doesn't affect hull validity or centroid, so it's safe to edit without re-validating. Capture Validate()'s return value; the receiver struct is not mutated in place.
See the PolygonGeometry section in unity-physicscore2d-geometry-api for the full edit-vs-validate table and decision rules.
Disconnected groups of shapes:
When users need information about:
All examples below assume the standard PhysicsCore2D
OnEnable/OnDisablelifecycle. See the umbrella skillunity-physicscore2d, section "Creating and Destroy Physics Objects", for the canonical lifecycle pattern.
body.GetAABB() for combined bounds, IntrudeShape to add shapes at runtime.FastCollisionsAllowed to compare CCD vs default.radius for rounded edges (Minkowski offset); inline random-convex-polygon helper.shape.circleGeometry, shape.polygonGeometry, etc.) and shape destroy/recreate when the type changes.unbrokenGeometryIslands to build per-island bodies, choosing static vs dynamic based on virtual-ground intersection.