// Convenience function -- change the selection to the specified object if it's not null and happens to be active. void Navigate(AxisEventData eventData, UserSelectable sel) { if (sel != null && sel.IsActive()) { eventData.selectedObject = sel.gameObject; } }
// Selection logic // Find the next UserSelectable object in the specified world-space direction. public UserSelectable FindUserSelectable(Vector3 dir) { dir = dir.normalized; Vector3 localDir = Quaternion.Inverse(transform.rotation) * dir; Vector3 pos = transform.TransformPoint(GetPointOnRectEdge(transform as RectTransform, localDir)); float maxScore = Mathf.NegativeInfinity; UserSelectable bestPick = null; for (int i = 0; i < s_List.Count; ++i) { UserSelectable sel = s_List[i]; if (sel == this || sel == null) { continue; } if (!sel.IsInteractable() || sel.UserNavigation.mode == UserNavigation.Mode.None) { continue; } var selRect = sel.transform as RectTransform; Vector3 selCenter = selRect != null ? (Vector3)selRect.rect.center : Vector3.zero; Vector3 myVector = sel.transform.TransformPoint(selCenter) - pos; // Value that is the distance out along the direction. float dot = Vector3.Dot(dir, myVector); // Skip elements that are in the wrong direction or which have zero distance. // This also ensures that the scoring formula below will not have a division by zero error. if (dot <= 0) { continue; } // This scoring function has two priorities: // - Score higher for positions that are closer. // - Score higher for positions that are located in the right direction. // This scoring function combines both of these criteria. // It can be seen as this: // Dot (dir, myVector.normalized) / myVector.magnitude // The first part equals 1 if the direction of myVector is the same as dir, and 0 if it's orthogonal. // The second part scores lower the greater the distance is by dividing by the distance. // The formula below is equivalent but more optimized. // // If a given score is chosen, the positions that evaluate to that score will form a circle // that touches pos and whose center is located along dir. A way to visualize the resulting functionality is this: // From the position pos, blow up a circular balloon so it grows in the direction of dir. // The first UserSelectable whose center the circular balloon touches is the one that's chosen. float score = dot / myVector.sqrMagnitude; if (score > maxScore) { maxScore = score; bestPick = sel; } } return(bestPick); }