public bool Replace(ElementContainer _Container) { if (_Container == null) { return(false); } int sourceIndex = m_Containers.IndexOf(_Container); int targetIndex = sourceIndex; if (sourceIndex < 0) { return(false); } Rect source = _Container.GetWorldRect(); float position; switch (m_Direction) { case Direction.Horizontal: position = source.center.x; break; case Direction.Vertical: position = source.center.y; break; default: return(false); } float distance = float.MaxValue; int index = 0; foreach (ElementContainer container in m_Containers) { if (container == null || container == _Container) { continue; } Rect target = container.GetWorldRect(); float minDistance; float maxDistance; switch (m_Direction) { case Direction.Horizontal: minDistance = Mathf.Abs(position - target.xMin); maxDistance = Mathf.Abs(position - target.xMax); break; case Direction.Vertical: minDistance = Mathf.Abs(position - target.yMax); maxDistance = Mathf.Abs(position - target.yMin); break; default: return(false); } if (distance > minDistance) { distance = minDistance; targetIndex = index; } if (distance > maxDistance) { distance = maxDistance; targetIndex = index + 1; } index++; } if (sourceIndex == targetIndex) { return(false); } m_Containers.RemoveAt(sourceIndex); m_Containers.Insert(targetIndex, _Container); return(true); }