暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhysicsRaycasterTests.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using NUnit.Framework;
  3. using System.Collections.Generic;
  4. using UnityEngine.EventSystems;
  5. public class PhysicsRaycasterTests
  6. {
  7. GameObject m_CamGO;
  8. GameObject m_Collider;
  9. [SetUp]
  10. public void TestSetup()
  11. {
  12. m_CamGO = new GameObject("PhysicsRaycaster Camera");
  13. m_Collider = GameObject.CreatePrimitive(PrimitiveType.Cube);
  14. }
  15. [Test]
  16. public void PhysicsRaycasterDoesNotCastOutsideCameraViewRect()
  17. {
  18. m_CamGO.transform.position = new Vector3(0, 0, -10);
  19. m_CamGO.transform.LookAt(Vector3.zero);
  20. var cam = m_CamGO.AddComponent<Camera>();
  21. cam.rect = new Rect(0.5f, 0, 0.5f, 1);
  22. m_CamGO.AddComponent<PhysicsRaycaster>();
  23. var eventSystem = m_CamGO.AddComponent<EventSystem>();
  24. // Create an object that will be hit if a raycast does occur.
  25. m_Collider.transform.localScale = new Vector3(100, 100, 1);
  26. List<RaycastResult> results = new List<RaycastResult>();
  27. var pointerEvent = new PointerEventData(eventSystem)
  28. {
  29. position = new Vector2(0, 0) // Raycast from the left side of the screen which is outside of the camera's view rect.
  30. };
  31. eventSystem.RaycastAll(pointerEvent, results);
  32. Assert.IsEmpty(results, "Expected no results from a raycast that is outside of the camera's viewport.");
  33. }
  34. [TearDown]
  35. public void TearDown()
  36. {
  37. GameObject.DestroyImmediate(m_CamGO);
  38. GameObject.DestroyImmediate(m_Collider);
  39. }
  40. }