private void UpdateDropdown <T>(
            List <T> items,
            CustomDropdown dropdown,
            ItemNameDelegate <T> itemName,
            Sprite icon,
            OnSelectDelegate <T> onSelect,
            int selectedItemIndex)
        {
            List <CustomDropdown.Item> dropdownItems = new List <CustomDropdown.Item>(items.Count);

            for (int i = 0; i < items.Count; i++)
            {
                var currIndex = i;
                T   currItem  = (T)items[currIndex];

                var onSelectEvent = new UnityEvent();
                onSelectEvent.AddListener(() => { onSelect(currItem, currIndex); });
                CustomDropdown.Item newItem = new CustomDropdown.Item()
                {
                    itemName        = itemName(currItem, currIndex),
                    itemIcon        = icon,
                    OnItemSelection = onSelectEvent,
                };

                dropdownItems.Add(newItem);
            }

            dropdown.dropdownItems     = dropdownItems;
            dropdown.selectedItemIndex = selectedItemIndex < dropdownItems.Count ? selectedItemIndex : 0;

            dropdown.enabled = false;
            dropdown.enabled = true;
        }
Beispiel #2
0
        protected void Rebuild <TKey, TValue>(IDictionary <TKey, TValue> options, OnSelectDelegate <TKey> select,
                                              TKey current)
        {
            Toggles.Clear();
            selectionContent.DestroyAllChildren();
            selectionContent.sizeDelta =
                new Vector2(selectionContent.sizeDelta.x, selectionTemplate.rect.height * options.Count);

            var currentSelectionIndex = -1;
            var i = 0;

            foreach (var option in options)
            {
                var toggle = Instantiate(selectionTemplate.gameObject, selectionContent).GetOrAddComponent <Toggle>();
                toggle.gameObject.SetActive(true);
                toggle.transform.localScale = Vector3.one;
                toggle.GetComponentInChildren <Text>().text = option.Value.ToString();
                toggle.interactable = true;
                toggle.isOn         = option.Key.Equals(current);
                toggle.onValueChanged.AddListener(isOn => select(toggle, option.Key));
                Toggles.Add(toggle);
                if (toggle.isOn)
                {
                    currentSelectionIndex = i;
                }
                i++;
            }

            if (emptyText == null)
            {
                selectionTemplate.gameObject.SetActive(options.Count < 1);
                selectionTemplate.GetComponent <Toggle>().isOn = options.Count > 0;
            }
            else
            {
                selectionTemplate.gameObject.SetActive(false);
                emptyText.gameObject.SetActive(options.Count < 1);
            }

            if (currentSelectionIndex > 0 && currentSelectionIndex < options.Count && options.Count > 1)
            {
                scrollRect.verticalNormalizedPosition = 1f - (currentSelectionIndex / (options.Count - 1f));
            }
        }
Beispiel #3
0
    public void Rebuild(List <string> options, OnSelectDelegate changeValue, string currentValue = "")
    {
        if (options == null)
        {
            options = new List <string>();
        }
        if (changeValue == null)
        {
            changeValue = delegate { }
        }
        ;
        if (currentValue == null)
        {
            currentValue = string.Empty;
        }

        Toggles.Clear();
        selectionContent.DestroyAllChildren();
        selectionContent.sizeDelta = new Vector2(selectionContent.sizeDelta.x, selectionTemplate.rect.height * options.Count);

        foreach (string option in options)
        {
            Toggle toggle = Instantiate(selectionTemplate.gameObject, selectionContent).GetOrAddComponent <Toggle>();
            toggle.gameObject.SetActive(true);
            toggle.transform.localScale = Vector3.one;
            toggle.GetComponentInChildren <Text>().text = option;
            toggle.interactable = true;
            toggle.isOn         = option.Equals(currentValue);
            toggle.onValueChanged.AddListener(isOn => changeValue(toggle, option));
            Toggles.Add(toggle.gameObject);
        }

        selectionTemplate.gameObject.SetActive(options.Count < 1);
        selectionTemplate.GetComponent <Toggle>().isOn = options.Count > 0;

        float index = options.IndexOf(currentValue);

        if (index > 0 && index < options.Count && options.Count > 1)
        {
            scrollRect.verticalNormalizedPosition = 1f - (index / (options.Count - 1f));
        }
    }
Beispiel #4
0
    //---------------------------------------------------------------------------
    public void Init(CreatureInfo info, OnSelectDelegate _del = null)
    {
        CreatureInfo = info;
        if (CreatureInfo == null)
        {
            gameObject.SetActive(true);
            m_icon.spriteName           = "";
            m_type.spriteName           = "";
            character_border.spriteName = "";

            return;
        }
        gameObject.name = info.ID;

        string       sprite_name     = string.Format("cs_{0}", info.ID);
        string       new_sprite_name = "_cut_" + sprite_name;
        UISpriteData sp = m_icon.atlas.CloneCustomSprite(sprite_name, new_sprite_name);

        if (sp != null)
        {
            sp.height = sp.width;
        }

        m_icon.spriteName = new_sprite_name;

        m_type.spriteName = string.Format("New_hero_info_hero_type_{0}", info.ShowAttackType);
        gameObject.SetActive(true);

        character_border.SetSpriteActive(info.TeamSkill != null);

        OnSelect = _del;
        //if (OnSelect != null)
        //    m_tooltip.span_press_time = 0.2f;
        //else
        //    m_tooltip.span_press_time = 0f;
    }
Beispiel #5
0
 public void SetSelectionCallback(OnSelectDelegate callback)
 {
     selectCallback = callback;
 }