FitBoxSizeToRect.cs 636 B

123456789101112131415161718192021222324
  1. using UnityEngine;
  2. namespace Rokid.UXR.Utility
  3. {
  4. [ExecuteAlways]
  5. [RequireComponent(typeof(BoxCollider))]
  6. [RequireComponent(typeof(RectTransform))]
  7. public class FitBoxSizeToRect : MonoBehaviour
  8. {
  9. private BoxCollider boxCollider;
  10. private RectTransform targetRect;
  11. private void Start()
  12. {
  13. boxCollider = transform.GetComponent<BoxCollider>();
  14. targetRect = transform.GetComponent<RectTransform>();
  15. }
  16. private void Update()
  17. {
  18. boxCollider.size = new Vector3(targetRect.rect.width, targetRect.rect.height, 0.2f);
  19. }
  20. }
  21. }