コード例 #1
0
    /*
     * 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);
                });
            }
        }
    }