SCTransformUtility.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class SCTransformUtility
  7. {
  8. public static bool ScreenPointToWorldPointInPlane(Transform transform, Vector2 screenPoint, Camera cam, out Vector3 worldPoint)
  9. {
  10. worldPoint = Vector2.zero;
  11. Ray ray = cam.ScreenPointToRay(screenPoint);
  12. var plane = new Plane(transform.rotation * Vector3.back, transform.position);
  13. float dist;
  14. if (!plane.Raycast(ray, out dist))
  15. return false;
  16. worldPoint = ray.GetPoint(dist);
  17. return true;
  18. }
  19. public static bool ScreenPointToLocalPointInPlane(Transform transform, Vector2 screenPoint, Camera cam, out Vector2 localPoint)
  20. {
  21. localPoint = Vector2.zero;
  22. Vector3 worldPoint;
  23. if (ScreenPointToWorldPointInPlane(transform, screenPoint, cam, out worldPoint))
  24. {
  25. localPoint = transform.InverseTransformPoint(worldPoint);
  26. return true;
  27. }
  28. return false;
  29. }
  30. }
  31. }