File size: 4,721 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;
using UnityEngine.UI;
using UnityEngine.TestTools.Utils;
public class GraphicRaycasterTests
{
Camera m_Camera;
EventSystem m_EventSystem;
Canvas m_Canvas;
RectTransform m_CanvasRectTrans;
Text m_TextComponent;
[SetUp]
public void TestSetup()
{
m_Camera = new GameObject("GraphicRaycaster Camera").AddComponent<Camera>();
m_Camera.transform.position = Vector3.zero;
m_Camera.transform.LookAt(Vector3.forward);
m_Camera.farClipPlane = 10;
m_EventSystem = new GameObject("Event System").AddComponent<EventSystem>();
m_Canvas = new GameObject("Canvas").AddComponent<Canvas>();
m_Canvas.renderMode = RenderMode.WorldSpace;
m_Canvas.worldCamera = m_Camera;
m_Canvas.gameObject.AddComponent<GraphicRaycaster>();
m_CanvasRectTrans = m_Canvas.GetComponent<RectTransform>();
m_CanvasRectTrans.sizeDelta = new Vector2(100, 100);
var textGO = new GameObject("Text");
m_TextComponent = textGO.AddComponent<Text>();
var textRectTrans = m_TextComponent.rectTransform;
textRectTrans.SetParent(m_Canvas.transform);
textRectTrans.anchorMin = Vector2.zero;
textRectTrans.anchorMax = Vector2.one;
textRectTrans.offsetMin = Vector2.zero;
textRectTrans.offsetMax = Vector2.zero;
}
[UnityTest]
public IEnumerator GraphicRaycasterDoesNotHitGraphicBehindCameraFarClipPlane()
{
m_CanvasRectTrans.anchoredPosition3D = new Vector3(0, 0, 11);
yield return null;
var results = new List<RaycastResult>();
var pointerEvent = new PointerEventData(m_EventSystem)
{
position = new Vector2(Screen.width / 2f, Screen.height / 2f)
};
m_EventSystem.RaycastAll(pointerEvent, results);
Assert.IsEmpty(results, "Expected no results from a raycast against a graphic behind the camera's far clip plane.");
}
[UnityTest]
public IEnumerator GraphicRaycasterReturnsWorldPositionAndWorldNormal()
{
m_CanvasRectTrans.anchoredPosition3D = new Vector3(0, 0, 11);
m_Camera.farClipPlane = 12;
yield return null;
var results = new List<RaycastResult>();
var pointerEvent = new PointerEventData(m_EventSystem)
{
position = new Vector2(Screen.width / 2f, Screen.height / 2f)
};
m_EventSystem.RaycastAll(pointerEvent, results);
// on katana on 10.13 agents world position returned is 0, -0.00952, 11
// it does not reproduce for me localy, so we just tweak the comparison threshold
Assert.That(new Vector3(0, 0, 11), Is.EqualTo(results[0].worldPosition).Using(new Vector3EqualityComparer(0.01f)));
Assert.That(new Vector3(0, 0, -1), Is.EqualTo(results[0].worldNormal).Using(new Vector3EqualityComparer(0.001f)));
}
[UnityTest]
public IEnumerator GraphicRaycasterUsesGraphicPadding()
{
m_CanvasRectTrans.anchoredPosition3D = new Vector3(0, 0, 11);
m_TextComponent.raycastPadding = new Vector4(-50, -50, -50, -50);
m_Camera.farClipPlane = 12;
yield return null;
var results = new List<RaycastResult>();
var pointerEvent = new PointerEventData(m_EventSystem)
{
position = new Vector2((Screen.width / 2f) - 60, Screen.height / 2f)
};
m_EventSystem.RaycastAll(pointerEvent, results);
Assert.IsNotEmpty(results, "Expected at least 1 result from a raycast outside the graphics RectTransform whose padding would make the click hit.");
}
[UnityTest]
public IEnumerator GraphicOnTheSamePlaneAsTheCameraCanBeTargetedForEvents()
{
m_Canvas.renderMode = RenderMode.ScreenSpaceCamera;
m_Canvas.planeDistance = 0;
m_Camera.orthographic = true;
m_Camera.orthographicSize = 1;
m_Camera.nearClipPlane = 0;
m_Camera.farClipPlane = 12;
yield return null;
var results = new List<RaycastResult>();
var pointerEvent = new PointerEventData(m_EventSystem)
{
position = new Vector2((Screen.width / 2f), Screen.height / 2f)
};
m_EventSystem.RaycastAll(pointerEvent, results);
Assert.IsNotEmpty(results, "Expected at least 1 result from a raycast ");
}
[TearDown]
public void TearDown()
{
Object.DestroyImmediate(m_Camera.gameObject);
Object.DestroyImmediate(m_EventSystem.gameObject);
Object.DestroyImmediate(m_Canvas.gameObject);
}
}
|