public void RaycastSendingUpdate(MouseMoveEventArgs e) { var sortedObjects = new List <GameObject>(Game.GetAllGameObjects()); sortedObjects.Sort(SortByDistanceToCamera); foreach (var gameObject in sortedObjects) { if (gameObject.HasComponent <RaycastTarget>() && gameObject.enabled) { var target = gameObject.GetComponent <RaycastTarget>(); float dist = Raycast.DistanceFromPoint(new Vector2(e.X, e.Y), new Vector3(0, 0, 0), gameObject.transform.TransformationMatrix() * mainCamera.GetViewMatrix(), mainCamera.GetProjectionMatrix()); if (dist <= target.hitRadius && !target.hovering && currentTarget == null && currentTargetUI == null) { if (target.onEnter != null) { target.onEnter(); } target.hovering = true; currentTarget = target; return; } else if (dist <= target.hitRadius && !target.hovering && currentTarget != null && currentTargetUI == null) { if (Vector3.Distance(currentTarget.gameObject.transform.position, mainCamera.gameObject.transform.position) > Vector3.Distance(target.gameObject.transform.position, mainCamera.gameObject.transform.position)) { if (currentTarget.onExit != null) { currentTarget.onExit(); } currentTarget.hovering = false; if (target.onEnter != null) { target.onEnter(); } target.hovering = true; currentTarget = target; return; } } else if (dist > target.hitRadius && target.hovering && currentTarget != null && currentTargetUI == null) { if (target.onExit != null) { target.onExit(); } target.hovering = false; currentTarget = null; return; } } } }
public void RaycastUISendingUpdate(MouseMoveEventArgs e) { var sortedObjects = new List <GameObject>(Game.GetAllGameObjects()); sortedObjects.Sort(SortByZ); foreach (var gameObject in sortedObjects) { if (gameObject.HasComponent <RaycastTargetUI>() && gameObject.enabled) { var target = gameObject.GetComponent <RaycastTargetUI>(); var inBox = false; if (target.gameObject.HasComponent <Text>()) { var text = target.gameObject.GetComponent <Text>(); var dimensions = text.GetUIDimensionsPixels(true, text); var minX = dimensions.X; var maxX = dimensions.Z; var minY = dimensions.Y; var maxY = dimensions.W; inBox = minX <= e.X && e.X <= maxX && minY <= e.Y && e.Y <= maxY; } else if (target.gameObject.HasComponent <Sprite>()) { var element = target.gameObject.GetComponent <Sprite>(); var dimensions = element.GetUIDimensionsPixels(false); var minX = dimensions.X; var maxX = dimensions.Z; var minY = dimensions.Y; var maxY = dimensions.W; inBox = minX <= e.X && e.X <= maxX && minY <= e.Y && e.Y <= maxY; } if (inBox && !target.hovering && currentTargetUI == null) { if (currentTarget != null) { currentTarget.onExit?.Invoke(); currentTarget.hovering = false; currentTarget = null; } target.onEnter?.Invoke(); target.hovering = true; currentTargetUI = target; return; } else if (!inBox && !target.hovering && currentTargetUI != null) { if (currentTargetUI.gameObject.transform.position.Z > target.gameObject.transform.position.Z) { currentTargetUI.onExit?.Invoke(); currentTargetUI.hovering = false; target.onEnter?.Invoke(); target.hovering = true; currentTargetUI = target; return; } } else if (!inBox && target.hovering && currentTargetUI != null) { target.onExit?.Invoke(); target.hovering = false; currentTargetUI = null; return; } } } }