TooltipGroup FindClosestEntry() { List <TooltipGroup> groups = TooltipGroup.ActiveGroups.FindAll(e => e.groupId == groupId); int leastHops = int.MaxValue; TooltipGroup closestGroup = null; for (int i = 0; i < groups.Count; i++) { bool foundMatch = false; int x = 0; Transform myTrackedObj = transform; while (myTrackedObj != null && !foundMatch) { Transform theirTrackedObj = groups[i].transform; int y = 0; while (theirTrackedObj != null && !foundMatch) { if (myTrackedObj == theirTrackedObj) { if (leastHops > x + y) { leastHops = x + y; closestGroup = groups[i]; } foundMatch = true; } theirTrackedObj = theirTrackedObj.parent; y++; } myTrackedObj = myTrackedObj.parent; x++; } } return(closestGroup); }
void AttachToGroup() { AttachedTo = FindClosestEntry(); if (AttachedTo != null) { AttachedTo.CurrentEntry = this; } }
void RemoveFromGroup() { if (AttachedTo != null) { if (AttachedTo.CurrentEntry == this) { AttachedTo.CurrentEntry = null; } AttachedTo = null; } }