TrnasformExtension.cs 877 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public static class TrnasformExtension
  5. {
  6. public static Bounds TransformBounds(this Transform self, Bounds bounds)
  7. {
  8. Vector3 center = self.TransformPoint(bounds.center);
  9. List<Vector3> corners = bounds.GetCorners(true);
  10. Bounds result = new Bounds(center, Vector3.zero);
  11. foreach (Vector3 position in corners)
  12. {
  13. result.Encapsulate(self.TransformPoint(position));
  14. }
  15. return result;
  16. }
  17. public static Bounds InverseTransformBounds(this Transform self, Bounds bounds)
  18. {
  19. Vector3 center = self.InverseTransformPoint(bounds.center);
  20. List<Vector3> corners = bounds.GetCorners(true);
  21. Bounds result = new Bounds(center, Vector3.zero);
  22. foreach (Vector3 position in corners)
  23. {
  24. result.Encapsulate(self.InverseTransformPoint(position));
  25. }
  26. return result;
  27. }
  28. }