예제 #1
0
        private void UpdateView()
        {
            if (HistoryViewEntryUI == null || HistoryContentTransform == null)
            {
                return;
            }

            // Update existing UI objects to match the history order.
            // Add new items when we run out of existing items.
            // Update the layout at the end if required.
            int  nextViewChildIndex = (HistoryContentUI.content.childCount > 0) ? 0 : -1;
            bool updateLayout       = false;

            foreach (IPEndPoint connection in History)
            {
                GameObject    entryObj       = null;
                HistoryEntry  entry          = null;
                RectTransform entryTransform = null;
                // Try get an existing view entry for this item.
                if (nextViewChildIndex >= 0)
                {
                    while (entryObj == null && nextViewChildIndex < HistoryContentUI.content.childCount)
                    {
                        entryObj       = HistoryContentUI.content.GetChild(nextViewChildIndex).gameObject;
                        entry          = entryObj.GetComponent <HistoryEntry>();
                        entryTransform = entryObj.GetComponent <RectTransform>();
                        if (entry == null || entryTransform == null)
                        {
                            // Invalid/unexpected UI object. Remove and delete it.
                            entryObj.transform.parent = null;
                            GameObject.Destroy(entryObj);
                        }
                        else
                        {
                            ++nextViewChildIndex;
                        }
                    }

                    if (nextViewChildIndex >= HistoryContentUI.content.childCount)
                    {
                        nextViewChildIndex = -1;
                    }
                }
                else
                {
                    updateLayout   = true;
                    entryObj       = GameObject.Instantiate(HistoryViewEntryUI.gameObject);
                    entry          = entryObj.GetComponent <HistoryEntry>();
                    entryTransform = entryObj.GetComponent <RectTransform>();
                }

                if (entryObj == null || entry == null || entryTransform == null)
                {
                    if (entryObj)
                    {
                        GameObject.Destroy(entryObj);
                    }
                    continue;
                }

                entry.Connection  = connection;
                entry.Connections = this;
                entryTransform.SetParent(HistoryContentTransform, false);
            }

            if (updateLayout)
            {
                HistoryContentUI.LayoutContentV();
            }
        }