UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Component Module / Collider AutoResize Component Completed

Component Module — UGTK

Collider Auto Resize Component

Summary

Automatically resizes a BoxCollider2D or CapsuleCollider2D so it matches the size of a RectTransform every frame, keeping the physics shape aligned with the UI/reference rect (also in Edit Mode).


Content


Modules Dependencies


Setup


How To Use

  1. Put a BoxCollider2D and/or a CapsuleCollider2D on the owner GameObject (the parent). The owner may also hold a RectTransform.
  2. Add Mn_ColliderAutoResizeComponent to a child (the reference image). It inherits Owner from Mn_UGTKComponent.
  3. At runtime (and in Edit Mode, thanks to [ExecuteInEditMode]) the collider size is recomputed each frame from the reference RectTransform.rect.size:
  4. The reference rect is the owner's RectTransform if present, otherwise the RectTransform on this GameObject.
  5. BoxCollider2D: size is set directly to the rect size.
  6. CapsuleCollider2D: the axis matching its direction follows the rect, the other axis is clamped so the capsule stays fully inside the rect (Mathf.Min).
  7. Every axis is clamped to a minimum of 0.01, and the collider is only written when the value actually changes.

OnValidate re-fetches the components and re-adjusts, so inspector edits update the collider immediately.


Testing Scene

Open ColliderAutoreSizeScene in the module's Debug/ folder.

What to do, in order:

  1. Select the child carrying the component and look at the inspector: it names the collider and the RectTransform it resolved.
  2. Resize the parent's RectTransform in the Scene view and watch the collider gizmo follow.
  3. Remove the collider from the parent and press ForceResize().

Step 3 is the point. The collider is expected on the parent, not on the object carrying the component, and when it is missing the component simply has nothing to resize — no error, no movement. That is the most common report of "it does nothing", and the inspector now says so outright.

The recording is the whole point of the component in one panel: the parent's Right offset goes from 0 to -80 to -200 to -350, and the Box Collider 2D size underneath reads 100, 180, 300, 450 without anyone touching it. Put the offset back and the collider comes back with it.


Technical Info


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 1 module in total.