/// <summary> /// Inserts a list item at the specified position in the list. /// </summary> /// <param name="item">Reference to the item to be inserted into the list.</param> /// <param name="position">0-based index of the position in the list where the item will be placed.</param> /// <param name="text">Text to display in the item (requires that the item has a TextMesh associated with it, preferably in a child GameObject).</param> public void InsertItem(IUIListObject item, int position, string text) { // Make sure Awake() has already run: if (!m_awake) Awake(); // Make sure Start() has already run: if (!m_started) Start(); // See if the item needs to be enabled: if (activateWhenAdding) { if (!((Component)item).gameObject.active) ((Component)item).gameObject.SetActiveRecursively(true); } // Now deactivate again if the list itself is deactivated: if(!gameObject.active) ((Component)item).gameObject.SetActiveRecursively(false); // Put the item in the correct layer: item.gameObject.layer = gameObject.layer; // Add the item to our container: if (container != null) container.AddChild(item.gameObject); //------------------------------------- // Position our item: //------------------------------------- item.transform.parent = mover.transform; #if AUTO_ORIENT_ITEMS item.transform.localRotation = Quaternion.identity; #endif #if AUTO_SCALE_ITEMS item.transform.localScale = Vector3.one; #endif // Go ahead and get the item in the mover's plane // on the local Z-axis. This must be done here // before anything that follows because if we are // using a perspective camera and these are newly // created items, their Start() will be called for // the first time when FindOuterEdges() is called // either by us, or by the Text property, and if // the item isn't already positioned relative to // the camera, then its size will be calculated // wrong. item.transform.localPosition = Vector3.zero; item.SetList(this); if (text != null) item.Text = text; // Clamp our position: position = Mathf.Clamp(position, 0, items.Count); // Hide the item by default: if (clipContents) { item.Hide(true); if (!item.Managed) item.gameObject.SetActiveRecursively(false); } item.Index = position; // Add the item: newItems.Add(item); if (position != items.Count) { itemsInserted = true; items.Insert(position, item); // Add to our visible items so that // it will be clipped in the next go: if (visibleItems.Count == 0) { visibleItems.Add(item); } else if (item.Index > 0) { int prevIdx = visibleItems.IndexOf(items[item.Index - 1]); if (prevIdx == -1) { // See if it should be added to the beginning or the end: if (visibleItems[0].Index >= item.Index) visibleItems.Insert(0, item); else visibleItems.Add(item); } else visibleItems.Insert(prevIdx + 1, item); } } else { items.Add(item); // Add to our visible items so that // it will be clipped in the next go: visibleItems.Add(item); } // See if we need to go ahead and position the item: if(positionItemsImmediately) { if (itemsInserted) RepositionItems(); else PositionNewItems(); } // Compute the edges of the item: /* item.FindOuterEdges(); item.UpdateCollider(); // See if we can just add it to the end: if(position == items.Count) { float x=0, y=0; bool addItemSpacing = false; if (orientation == ORIENTATION.HORIZONTAL) { // Find the X-coordinate: if (items.Count > 0) { addItemSpacing = true; // We will be adding itemSpacing lastItem = items[items.Count - 1]; if (direction == DIRECTION.TtoB_LtoR) x = lastItem.transform.localPosition.x + lastItem.BottomRightEdge.x + itemSpacing - item.TopLeftEdge.x; else x = lastItem.transform.localPosition.x - lastItem.BottomRightEdge.x - itemSpacing + item.TopLeftEdge.x; } else { if (spacingAtEnds) addItemSpacing = true; // We will be adding itemSpacing if (direction == DIRECTION.TtoB_LtoR) x = (viewableAreaActual.x * -0.5f) - item.TopLeftEdge.x + ((spacingAtEnds) ? (itemSpacing) : (0)) + extraEndSpacing; else x = (viewableAreaActual.x * 0.5f) - item.BottomRightEdge.x - ((spacingAtEnds) ? (itemSpacing) : (0)) - extraEndSpacing; } // Find the Y-coordinate: switch(alignment) { case ALIGNMENT.CENTER: y = 0; break; case ALIGNMENT.LEFT_TOP: y = (viewableAreaActual.y * 0.5f) - item.TopLeftEdge.y; break; case ALIGNMENT.RIGHT_BOTTOM: y = (viewableAreaActual.y * -0.5f) - item.BottomRightEdge.y; break; } contentDelta = item.BottomRightEdge.x - item.TopLeftEdge.x + ((addItemSpacing && lastItem != null) ? (itemSpacing) : (0)); } else { // Determine the Y-coordinate: if (items.Count > 0) { addItemSpacing = true; // We will be adding itemSpacing lastItem = items[items.Count - 1]; if(direction == DIRECTION.TtoB_LtoR) y = lastItem.transform.localPosition.y + lastItem.BottomRightEdge.y - itemSpacing - item.TopLeftEdge.y; else y = lastItem.transform.localPosition.y - lastItem.BottomRightEdge.y + itemSpacing + item.TopLeftEdge.y; } else { if (spacingAtEnds) addItemSpacing = true; // We will be adding itemSpacing if(direction == DIRECTION.TtoB_LtoR) y = (viewableAreaActual.y * 0.5f) - item.TopLeftEdge.y - ((spacingAtEnds) ? (itemSpacing) : (0)) - extraEndSpacing; else y = (viewableAreaActual.y * -0.5f) - item.BottomRightEdge.y + ((spacingAtEnds) ? (itemSpacing) : (0)) + extraEndSpacing; } // Determine the X-coordinate: switch (alignment) { case ALIGNMENT.CENTER: x = 0; break; case ALIGNMENT.LEFT_TOP: x = (viewableAreaActual.x * -0.5f) - item.TopLeftEdge.x; break; case ALIGNMENT.RIGHT_BOTTOM: x = (viewableAreaActual.x * 0.5f) - item.BottomRightEdge.x; break; } contentDelta = item.TopLeftEdge.y - item.BottomRightEdge.y + ((addItemSpacing && lastItem != null) ? (itemSpacing) : (0)); } // Position the new item: item.transform.localPosition = new Vector3(x, y); item.Index = items.Count; items.Add(item); UpdateContentExtents(contentDelta); ClipItems(); } else { // Else, insert the item in the midst of our list: items.Insert(position, item); PositionItems(); } */ }
/// <summary> /// Inserts a list item at the specified position in the list. /// </summary> /// <param name="item">Reference to the item to be inserted into the list.</param> /// <param name="position">0-based index of the position in the list where the item will be placed.</param> /// <param name="text">Text to display in the item (requires that the item has a TextMesh associated with it, preferably in a child GameObject).</param> public void InsertItem(IUIListObject item, int position, string text) { IUIListObject lastItem; float contentDelta; // Make sure Start() has already run: if (!m_started) Start(); // See if the item needs to be enabled: if(activateWhenAdding) if (!((Component)item).gameObject.active) ((Component)item).gameObject.SetActiveRecursively(true); // Put the item in the correct layer: item.gameObject.layer = gameObject.layer; // Add the item to our container: if (container != null) container.AddChild(item.gameObject); //------------------------------------- // Position our item: //------------------------------------- item.transform.parent = mover.transform; #if AUTO_ORIENT_ITEMS item.transform.localRotation = Quaternion.identity; #endif #if AUTO_SCALE_ITEMS item.transform.localScale = Vector3.one; #endif // Go ahead and get the item in the mover's plane // on the local Z-axis. This must be done here // before anything that follows because if we are // using a perspective camera and these are newly // created items, their Start() will be called for // the first time when FindOuterEdges() is called // either by us, or by the Text property, and if // the item isn't already positioned relative to // the camera, then its size will be calculated // wrong. item.transform.localPosition = Vector3.zero; if(text != null) item.Text = text; item.SetList(this); // Compute the edges of the item: item.FindOuterEdges(); item.UpdateCollider(); // Clamp our position: position = Mathf.Clamp(position, 0, items.Count); // Hide the item by default: item.Hide(true); if (!item.Managed) item.gameObject.SetActiveRecursively(false); // See if we can just add it to the end: if(position == items.Count) { if (orientation == ORIENTATION.HORIZONTAL) { if (items.Count > 0) { lastItem = items[items.Count - 1]; item.transform.localPosition = new Vector3(lastItem.transform.localPosition.x + lastItem.BottomRightEdge.x + itemSpacing - item.TopLeftEdge.x, 0); } else { item.transform.localPosition = new Vector3((viewableArea.x * -0.5f) - item.TopLeftEdge.x + itemSpacing, 0); } contentDelta = item.BottomRightEdge.x - item.TopLeftEdge.x + itemSpacing; } else { if (items.Count > 0) { lastItem = items[items.Count - 1]; item.transform.localPosition = new Vector3(0, lastItem.transform.localPosition.y + lastItem.BottomRightEdge.y - itemSpacing - item.TopLeftEdge.y); } else { item.transform.localPosition = new Vector3(0, (viewableArea.y * 0.5f) - item.TopLeftEdge.y - itemSpacing); } contentDelta = item.TopLeftEdge.y - item.BottomRightEdge.y + itemSpacing; } item.Index = items.Count; items.Add(item); UpdateContentExtents(contentDelta); ClipItems(); } else { // Else, insert the item in the midst of our list: items.Insert(position, item); PositionItems(); } }