/* * Populate components from element. */ private void PopulateComponents(Element awElement) { // Clean up old ones foreach (Transform child in componentLayout) { GameObject.Destroy(child.gameObject); } // Wrap the ComponentView back if (componentView.isUnwrapped) { componentView.OnClose(); } // Set new ones, if necessary componentParent.SetActive(awElement.components.Length > 0); if (awElement.components.Length > 0) { for (int i = 0; i < awElement.components.Length; i++) { GameObject compObj = GameObject.Instantiate(ComponentPrefab, componentLayout.transform); compObj.SetActive(true); // The component label Text label = compObj.transform.Find("Text").GetComponent <Text>(); label.text = awElement.components[i].realName; // The mask Sprite sprite = awElement.components[i].cover; RectTransform mask = compObj.transform.Find("Mask").GetComponent <RectTransform>(); mask.gameObject.SetActive(sprite != null); if (sprite != null) { // The component icon Image icon = mask.Find("Image").GetComponent <Image>(); icon.sprite = awElement.components[i].cover; icon.SetNativeSize(); if (sprite.texture.width > sprite.texture.height) { // Scale by height float scale = mask.rect.height / sprite.texture.height; icon.transform.localScale = Vector3.one * scale; } else { // Scale by width float scale = mask.rect.width / sprite.texture.width; icon.transform.localScale = Vector3.one * scale; } } // Bind an action to it, to show component description. AW.Component awComponent = awElement.components[i]; Button actionBtn = compObj.GetComponent <Button>(); actionBtn.onClick.RemoveAllListeners(); // Because I'm too lazy to clean up properly actionBtn.onClick.AddListener(() => { componentView.Show(awComponent); }); } } }
public void Show(AW.Component component) { if (component == null) { Debug.LogWarning("Cannot show null component..."); return; } // Activate gameObject.SetActive(true); // Set icon and compute size icon.sprite = component.image; if (component.image != null) { // Best fit of image into view size HandlePictureSize(); // Enable image slot iconParent.gameObject.SetActive(true); // Resize attribute list Vector2 offsetMax = scroll.offsetMax; offsetMax.y = -450.0f; scroll.offsetMax = offsetMax; content.padding.top = 35; } else { // Disable image slot iconParent.gameObject.SetActive(false); // Resize attribute list Vector2 offsetMax = scroll.offsetMax; offsetMax.y = -75.0f; scroll.offsetMax = offsetMax; content.padding.top = 0; } // Set title title.text = component.realName; // Populate attributes for (int i = 0; i < component.attributes.Count; i++) { Attribute attribute = component.attributes[i]; GameObject obj = GameObject.Instantiate(AttributePrefab, attributeParent); Text label = obj.transform.Find("Name").GetComponent <Text>(); Text value = obj.transform.Find("Value").GetComponent <Text>(); label.text = attribute.label.ToUpper(); value.text = attribute.content; // Set attribute size manually, because VerticalLayoutGroup f***s up const float spacing = 8; float height = label.preferredHeight + spacing + value.preferredHeight; RectTransform attributeRT = obj.GetComponent <RectTransform>(); Vector2 size = attributeRT.sizeDelta; size.y = height; attributeRT.sizeDelta = size; } // Do show animation showing = StartCoroutine(DoViewAnimation(true, null)); }