public void LateUpdate() { cellTransform.anchoredPosition = DesiredAnchorPosition(); float bottomOfCell = (cellTransform.anchoredPosition.y) + tableContentTransform.anchoredPosition.y - (tableContentTransform.rect.height - tableTransform.rect.height); Vector2 otherPos = cellTransform.anchoredPosition; float distance = 99999.0f; // Get my sibling's top of cell, allow it to push me out of the way if (nextHeaderTransform != null) { PUTableHeaderScript otherScript = nextHeaderTransform.GetComponent <PUTableHeaderScript> (); otherPos = otherScript.DesiredAnchorPosition(); distance = cellTransform.anchoredPosition.y - otherPos.y; } if (distance < cellTransform.rect.height) { cellTransform.anchoredPosition = new Vector2(otherPos.x, otherPos.y + cellTransform.rect.height); tableCell.puGameObject.canvasGroup.alpha = LeanTween.easeInCubic(0, 1, distance / cellTransform.rect.height); } else if (bottomOfCell < 0) { tableCell.puGameObject.canvasGroup.alpha = LeanTween.easeInCubic(0, 1, (bottomOfCell + cellTransform.rect.height) / cellTransform.rect.height); } else { if (tableCell.puGameObject.canvasGroup.alpha.Equals(1.0f) == false) { tableCell.puGameObject.canvasGroup.alpha = 1.0f; } } }
public void ReloadTable(bool reuseCells = true) { RectTransform contentRectTransform = contentObject.transform as RectTransform; if (gameObject.GetComponent <PUTableUpdateScript>() == null) { PUTableUpdateScript script = (PUTableUpdateScript)gameObject.AddComponent(typeof(PUTableUpdateScript)); script.table = this; } // This one will set the content size of the scrolling axis CalculateContentSize(); // -2) Calculate the old height, so we can subtract it from the new height and adjust the scrolling appropriately float oldHeight = contentRectTransform.rect.height; // -1) Save previous cells for reuse if we can... List <PUTableCell> savedCells = new List <PUTableCell>(allCells); allCells.Clear(); if (allObjects == null || allObjects.Count == 0) { return; } // 0) Remove all of the cells from the list transform temporarily foreach (PUTableCell cell in savedCells) { cell.puGameObject.rectTransform.SetParent(null, false); } // 1) Run through allObjects; instantiate a cell object based on said object class float currentContentHeight = 0; for (int i = 0; i < allObjects.Count; i++) { object myCellData = allObjects [i]; // Can we reuse an existing cell? PUTableCell savedCell = null; if (reuseCells) { foreach (PUTableCell otherCell in savedCells) { if (otherCell.IsHeader() == false && otherCell.cellData.Equals(myCellData)) { savedCell = otherCell; savedCells.Remove(otherCell); break; } } } PUTableCell newCell = LoadCellForData(myCellData, savedCell, currentContentHeight); currentContentHeight += newCell.puGameObject.rectTransform.rect.height; } for (int i = 0; i < allObjects.Count; i++) { PUTableCell cell = allCells [i]; if (cell.IsHeader()) { // TODO: Move me to the end of the stuff cell.puGameObject.parent = this; cell.puGameObject.gameObject.transform.SetParent(cell.puGameObject.gameObject.transform.parent, false); PUTableHeaderScript headerScript = cell.puGameObject.rectTransform.GetComponent <PUTableHeaderScript> (); headerScript.Start(); } } // This one will set the content size of the scrolling axis CalculateContentSize(); // This will layout our cells LateUpdate(); // This will get the actual content size after the cells are successfully laid out CalculateContentSize(); // 3) offset the scroll based upon the change in table height float newHeight = contentRectTransform.rect.height; if (oldHeight > 1) { Vector2 scroll = contentRectTransform.anchoredPosition; scroll.y += newHeight - oldHeight; contentRectTransform.anchoredPosition = scroll; } // 2) Remove all previous content which have not reused foreach (PUTableCell cell in savedCells) { cell.unload(); } }
public virtual void LoadIntoPUGameObject(PUScrollRect parent, object data) { scrollRect = parent; cellData = data; string xmlPath = XmlPath(); if (xmlPath != null) { puGameObject = (PUGameObject)PlanetUnity2.loadXML(PlanetUnityOverride.xmlFromPath(xmlPath), parent.contentObject, null); // Attach all of the PlanetUnity objects try { FieldInfo field = this.GetType().GetField("scene"); if (field != null) { field.SetValue(this, puGameObject); } puGameObject.PerformOnChildren(val => { PUGameObject oo = val as PUGameObject; if (oo != null && oo.title != null) { field = this.GetType().GetField(oo.title); if (field != null) { field.SetValue(this, oo); } } return(true); }); } catch (Exception e) { UnityEngine.Debug.Log("TableCell error: " + e); } try { // Attach all of the named GameObjects FieldInfo[] fields = this.GetType().GetFields(); foreach (FieldInfo field in fields) { if (field.FieldType == typeof(GameObject)) { GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); foreach (GameObject pObject in pAllObjects) { if (pObject.name.Equals(field.Name)) { field.SetValue(this, pObject); } } } } } catch (Exception e) { UnityEngine.Debug.Log("TableCell error: " + e); } } else { puGameObject = new PUGameObject(); puGameObject.SetFrame(0, 0, 0, 60, 0, 0, "bottom,stretch"); puGameObject.LoadIntoPUGameObject(parent); puGameObject.gameObject.transform.SetParent(parent.contentObject.transform, false); } if (IsHeader() && this is PUSimpleTableCell == false) { PUTableHeaderScript script = (PUTableHeaderScript)puGameObject.gameObject.AddComponent(typeof(PUTableHeaderScript)); script.table = table; script.tableCell = this; } puGameObject.parent = table; // We want to bridge all notifications to my scope; this allows developers to handle notifications // at the table cell level, or at the scene controller level, with ease NotificationCenter.addObserver(this, "*", puGameObject, (args, name) => { NotificationCenter.postNotification(scrollRect.Scope(), name, args); }); cellGameObject = puGameObject.gameObject; cellTransform = cellGameObject.transform as RectTransform; tableTransform = scrollRect.rectTransform; tableContentTransform = scrollRect.contentObject.transform as RectTransform; UpdateContents(); }