/// <summary> /// Update collection is called from the editor button on the inspector. /// This function rebuilds / updates the layout. /// </summary> public void UpdateCollection() { // Check for empty nodes and remove them List <CollectionLayoutNode> emptyNodes = new List <CollectionLayoutNode>(); for (int i = 0; i < NodeList.Count; i++) { if (NodeList[i].transform == null || (!NodeList[i].transform.gameObject.activeSelf) || NodeList[i].transform.parent == null || !(NodeList[i].transform.parent.gameObject == this.gameObject)) { emptyNodes.Add(NodeList[i]); } } // Now delete the empty nodes for (int i = 0; i < emptyNodes.Count; i++) { NodeList.Remove(emptyNodes[i]); } emptyNodes.Clear(); // Check when children change and adjust for (int i = 0; i < this.transform.childCount; i++) { Transform child = this.transform.GetChild(i); if (!ContainsNode(child) && child.gameObject.activeSelf) { CollectionLayoutNode node = new CollectionLayoutNode(); node.Name = child.name; node.transform = child; NodeList.Add(node); } } if (LayoutType == CollectionLayoutType.Grid) { _columns = _rows = Mathf.CeilToInt(Mathf.Sqrt((float)NodeList.Count)); } else if (LayoutType == CollectionLayoutType.Stacked) { _columns = 1; _rows = NodeList.Count; } Width = _columns * CellWidth; Height = _rows * CellHeight; _halfCell = new Vector2(CellWidth * 0.5f, CellHeight * 0.5f); LayoutChildren(); if (OnCollectionUpdated != null) { OnCollectionUpdated.Invoke(this); } }
/// <summary> /// Update the facing of a node given the nodes new position for facing orign with node and orientation type /// </summary> /// <param name="node"></param> /// <param name="orientType"></param> /// <param name="newPos"></param> private void UpdateNodeFacing(CollectionLayoutNode node, Vector3 newPos = default(Vector3)) { Vector3 centerAxis; Vector3 pointOnAxisNearestNode; switch (NodeOrientation) { case NodeOrientationTypeEnum.FaceOrigin: node.transform.rotation = Quaternion.LookRotation(node.transform.position - this.transform.position, this.transform.up); break; case NodeOrientationTypeEnum.FaceOriginReversed: node.transform.rotation = Quaternion.LookRotation(this.transform.position - node.transform.position, this.transform.up); break; case NodeOrientationTypeEnum.FaceCenterAxis: centerAxis = Vector3.Project(node.transform.position - this.transform.position, this.transform.up); pointOnAxisNearestNode = this.transform.position + centerAxis; node.transform.rotation = Quaternion.LookRotation(node.transform.position - pointOnAxisNearestNode, this.transform.up); break; case NodeOrientationTypeEnum.FaceCenterAxisReversed: centerAxis = Vector3.Project(node.transform.position - this.transform.position, this.transform.up); pointOnAxisNearestNode = this.transform.position + centerAxis; node.transform.rotation = Quaternion.LookRotation(pointOnAxisNearestNode - node.transform.position, this.transform.up); break; case NodeOrientationTypeEnum.FaceFoward: node.transform.forward = transform.rotation * Vector3.forward; break; case NodeOrientationTypeEnum.FaceForwardReversed: node.transform.forward = transform.rotation * Vector3.back; break; case NodeOrientationTypeEnum.FaceParentUp: node.transform.forward = transform.rotation * Vector3.up; break; case NodeOrientationTypeEnum.FaceParentDown: node.transform.forward = transform.rotation * Vector3.down; break; case NodeOrientationTypeEnum.None: break; default: throw new ArgumentOutOfRangeException(); } }