/// <summary>
        /// Removes the given matter from this UI.
        /// </summary>
        /// <param name="matter">The matter to remove.</param>
        /// <param name="quantity">The amount of that matter to remove (optional, defaults to 1).</param>
        public void RemoveMatter(Matter matter, int quantity = 1)
        {
            if (matter != null && this.displayedMatter.ContainsKey(matter))
            {
                MatterIcon matterIcon = this.displayedMatter[matter];

                if (matterIcon.DisplayedQuantity - quantity <= 0)
                {
                    GameObject.Destroy(matterIcon.gameObject);
                    this.displayedMatter.Remove(matter);
                }
                else
                {
                    matterIcon.SetDisplay(matterIcon.DisplayedMatter, matterIcon.DisplayedQuantity - quantity);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sets the matter to display on this UI element.
        /// </summary>
        /// <param name="matter">The matter to display. `null` will hide the icon.</param>
        /// <param name="showRequiredComponents">Whether to display the required components for the given <paramref name="matter"/>.</param>
        public void SetMatter(Matter matter, bool showRequiredComponents = true)
        {
            if (matter != null)
            {
                this.iconUI.sprite  = matter.GetIcon();
                this.iconUI.enabled = true;

                if (showRequiredComponents && matter is MatterCompound)
                {
                    this.ClearRequiredComponents();
                    this.requiredMatterContainer.SetActive(true);

                    Dictionary <Matter, int> requiredMatters = new Dictionary <Matter, int>();
                    foreach (Matter component in ((MatterCompound)matter).Components)
                    {
                        if (!requiredMatters.ContainsKey(component))
                        {
                            requiredMatters.Add(component, 1);
                        }
                        else
                        {
                            requiredMatters[component]++;
                        }
                    }

                    var enumerator = requiredMatters.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        GameObject uiElement  = GameObject.Instantiate(this.matterIconPrefab, Vector3.zero, Quaternion.identity, this.requiredMatterContainer.transform);
                        MatterIcon matterIcon = uiElement.GetComponent <MatterIcon>();
                        matterIcon?.SetDisplay(enumerator.Current.Key, enumerator.Current.Value);
                    }
                }
                else
                {
                    this.requiredMatterContainer.SetActive(false);
                }
            }
            else
            {
                this.iconUI.enabled = false;
            }
        }
        /// <summary>
        /// Adds the given matter to this UI.
        /// </summary>
        /// <param name="matter">The matter to add.</param>
        /// <param name="quantity">The amount of that matter to add (optional, defaults to 1).</param>
        public void AddMatter(Matter matter, int quantity = 1)
        {
            if (matter != null)
            {
                MatterIcon matterIcon  = null;
                int        newQuantity = quantity;

                if (!this.displayedMatter.ContainsKey(matter))
                {
                    GameObject matterIconObject = GameObject.Instantiate(this.matterIconPrefab.gameObject, this.transform);
                    matterIcon = matterIconObject.GetComponent <MatterIcon>();
                    this.displayedMatter.Add(matter, matterIcon);
                }
                else
                {
                    matterIcon   = this.displayedMatter[matter];
                    newQuantity += matterIcon.DisplayedQuantity;
                }

                matterIcon.SetDisplay(matter, newQuantity);
            }
        }