public override void SetInstance(RectTransform instanceTransform, XmlLayout xmlLayout) { base.SetInstance(instanceTransform, xmlLayout); if (instanceTransform != null) { currentListElement = instanceTransform.GetComponent <XmlLayoutList>(); } }
private void _RemoveListItem(XmlLayoutList list, XmlLayoutListItem item) { if (item.xmlElement != null) { list.listItems.Remove(item); list.listElement.RemoveChildElement(item.xmlElement); } if (Application.isPlaying) { GameObject.Destroy(item.gameObject); } else { GameObject.DestroyImmediate(item.gameObject); } XmlLayoutTimer.AtEndOfFrame(() => { UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(list.rectTransform); }, list); }
public override void ParseChildElements(System.Xml.XmlNode xmlNode) { if (String.IsNullOrEmpty(currentXmlElement.DataSource)) { // no data source; no rendering to do here // I've decided not to log anything here, as this could be desired behaviour (a data source could be set later, for example) return; } var dataSource = currentXmlElement.DataSource; var xmlLayoutController = currentXmlLayoutInstance.XmlLayoutController; if (xmlLayoutController == null) { return; } var viewModelProperty = xmlLayoutController.GetType().GetProperty("viewModel"); if (viewModelProperty == null) { Debug.LogWarning("[XmlLayout] Warning: Useage of the <List> element requires the XmlLayoutController to have a view model type defined."); return; } var viewModel = viewModelProperty.GetValue(xmlLayoutController, null); var listProperty = viewModel.GetType().GetProperty(dataSource); var listField = viewModel.GetType().GetField(dataSource); IList list = null; if (listProperty != null) { if (!(listProperty.PropertyType.IsGenericType && listProperty.PropertyType.GetGenericTypeDefinition() == typeof(ObservableList <>))) { Debug.LogWarning("[XmlLayout] Warning: Usage of the <List> element requires a property with a type of ObservableList."); return; } list = (IList)listProperty.GetValue(viewModel, XmlLayoutUtilities.BindingFlags, null, null, null); } else if (listField != null) { if (!(listField.FieldType.IsGenericType && listField.FieldType.GetGenericTypeDefinition() == typeof(ObservableList <>))) { Debug.LogWarning("[XmlLayout] Warning: Usage of the <List> element requires a property with a type of ObservableList."); return; } list = (IList)listField.GetValue(viewModel); } else { Debug.LogWarning("[XmlLayout] Warning: View Model does not contain a field or property for data source '" + dataSource + "'."); return; } var observableList = (IObservableList)list; if (list == null || observableList == null) { // no data yet return; } var parent = transformToAddChildrenTo.GetComponent <XmlElement>(); if (!parent.attributes.ContainsKey("id")) { XmlLayoutTimer.AtEndOfFrame(() => parent.SetAndApplyAttribute("id", observableList.guid), parent, true); } var itemTemplate = GetItemTemplate(xmlNode.InnerXml); var xmlLayoutListComponent = parent.GetComponent <XmlLayoutList>(); if (xmlLayoutListComponent == null) { xmlLayoutListComponent = parent.gameObject.AddComponent <XmlLayoutList>(); } xmlLayoutListComponent.itemTemplate = itemTemplate; xmlLayoutListComponent.DataSource = dataSource; xmlLayoutListComponent.baseSiblingIndex = currentXmlElement.transform.GetSiblingIndex(); xmlLayoutListComponent.list = observableList; xmlLayoutListComponent.isCalculatedList = !listProperty.IsAutoProperty(); xmlLayoutListComponent.itemAnimationDuration = currentXmlElement.attributes.ContainsKey("itemAnimationDuration") ? currentXmlElement.attributes.GetValue <float>("itemAnimationDuration") : 0.25f; xmlLayoutListComponent.itemShowAnimation = currentXmlElement.attributes.ContainsKey("itemShowAnimation") ? currentXmlElement.attributes.GetValue <string>("itemShowAnimation") : "None"; xmlLayoutListComponent.itemHideAnimation = currentXmlElement.attributes.ContainsKey("itemHideAnimation") ? currentXmlElement.attributes.GetValue <string>("itemHideAnimation") : "None"; currentListElement = xmlLayoutListComponent; if (ListElements.ContainsKey(observableList.guid)) { ListElements[observableList.guid] = xmlLayoutListComponent; } else { ListElements.Add(observableList.guid, xmlLayoutListComponent); } // Render the list items as per the view model for (var x = 0; x < list.Count; x++) { RenderListItem(list[x], dataSource, itemTemplate, observableList); } }