using UnityEngine; [ExecuteAlways] [RequireComponent(typeof(RectTransform))] [RequireComponent(typeof(BoxCollider2D))] public class MatchRectToCollider : MonoBehaviour { RectTransform rt; BoxCollider2D col; void Awake() { rt = GetComponent(); col = GetComponent(); } void UpdateCollider() { if (rt == null || col == null) return; col.size = rt.rect.size; col.offset = rt.rect.center; } void Start() { UpdateCollider(); } void Update() { if (rt.hasChanged) { UpdateCollider(); rt.hasChanged = false; } } void OnValidate() { rt = GetComponent(); col = GetComponent(); UpdateCollider(); } }