45 lines
842 B
C#
45 lines
842 B
C#
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
[RequireComponent(typeof(RectTransform))]
|
|
[RequireComponent(typeof(BoxCollider2D))]
|
|
public class MatchRectToCollider : MonoBehaviour
|
|
{
|
|
RectTransform rt;
|
|
BoxCollider2D col;
|
|
|
|
void Awake()
|
|
{
|
|
rt = GetComponent<RectTransform>();
|
|
col = GetComponent<BoxCollider2D>();
|
|
}
|
|
|
|
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<RectTransform>();
|
|
col = GetComponent<BoxCollider2D>();
|
|
UpdateCollider();
|
|
}
|
|
} |