Exemplo n.º 1
0
        public void AddSmartButton(string rawButtonText, string rawCallbackCode)
        {
            // Setting the prefabs text
            // because setting the clones doesnt update the preferredWidth instantly
            buttonPrefab.text.text = rawButtonText;

            SmartButton clone = Instantiate(buttonPrefab);

            clone.transform.SetParent(container, false);

            clone.name = "[" + rawCallbackCode + "]";

            clone.button.onClick.AddListener(() =>
            {
                // All variables used in here will be excluded from the GC
                // They're kept in memory as long as the callback exists, which is as long as the button exists.
                PMWrapper.AddCode(rawCallbackCode, true);
            });

            float width  = clone.text.preferredWidth + padding * 2;            // x2 for left and right padding
            float height = clone.rect.rect.height;

            // Check if fits
            if (offset.x + width > container.rect.width)
            {
                // Move to next line
                offset = new Vector2(0, offset.y - height - margin);
            }

            // min.x = west
            // min.y = south
            // max.x = east
            // max.y = north
            clone.rect.offsetMin = new Vector2(offset.x, offset.y - height);
            clone.rect.offsetMax = new Vector2(offset.x + width, offset.y);

            offset.x           += width + margin;
            container.sizeDelta = new Vector2(container.sizeDelta.x, height + margin - offset.y);

            // Enable background if needs scrollbar
            background.enabled = container.sizeDelta.y > background.rectTransform.sizeDelta.y;

            buttons.Add(clone);
            UISingleton.instance.manusSelectables.Add(new UISingleton.ManusSelectable
            {
                selectable = clone.button, names = new List <string> {
                    "sb-" + rawCallbackCode
                }
            });
        }