-1

I want to create an inventory system and i want to create my buttons with using GUI.Button.I create them like this.

void OnGUI()
        {
            
            GUI.Button(new Rect(0, 220, 100, 20), "Top-left");
            GUI.Button(new Rect(120,220, 100, 20), "Top-right");
            GUI.Button(new Rect(0, 280, 100, 20), "Bottom-left");
            GUI.Button(new Rect(120, 280, 100, 20), "Bottom-right");
    
          
            
        }
    

My problem is i cant move them. I mean i want to add a scrollrect to my inventory. But even if i add it my buttons are not moving. How to i move it dynamically like normal buttons or how to i scroll them with using script ? Thanks for your help.

badcoder
  • 33
  • 6
  • Isn't this old UI? In new UI system GUI class only used for editor. Why don't you use [ScrollView](https://docs.unity3d.com/ScriptReference/UIElements.ScrollView.html) component? It automatically setup everything needed for scrolling. – SeLeCtRa Mar 15 '21 at 14:04
  • Because im gonna use this buttons like inventory element and they must be dynamic.I mean the button numbers must be depent on the user.So that i need to create them with using script.And if i use script ,even if i use ScrollView they re not moving – badcoder Mar 15 '21 at 14:36
  • I used scroll view in alot of things and dynamically (by script) created elements are supported by scroll view. Scroll view can be used for inventory systems and you can move it from script. In which part did you have problem? Because if you use GUI, unless you handle it very well, on different screen sizes your app will look weird. If you post your all code related to this problem we can help you more clearly. – SeLeCtRa Mar 15 '21 at 15:17
  • this is my inventory system.I have three section in this inventory.Food,drink and health tab.When the user create a profile this inventory will be null.But if he buy something from market my inventory system must be create a button and it must put the image of food inside the button .After then if he buy another food from market ,it must be create a button again .and as you can they must be scroll down with script . And this is my inventory picture i upload it because i want you to imagine easily. https://hizliresim.com/0GJG3K – badcoder Mar 15 '21 at 17:42
  • by the way if i have an another option to create dynamic buttons you can tell me too :) – badcoder Mar 15 '21 at 17:43

1 Answers1

0

I think code is self explenatory but I will explain generally. Create a scroll view in UI. And create a prefab for slot with frame and an image as child object. For layout use GridLayout Group or from code create a horizontal layout then change the positions when needed. Then use this code:

public class InventoryManager : MonoBehaviour
{
    [SerializeField] Transform content; //Where all panels will be hold. Scroll rect is parent of this object. This is content of scroll view.
    Transform foodPanel, healthPanel, drinkPanel; // These are just child of *content* add them at awake

    [SerializeField] GameObject slotPrefab; //Slot is a UI element with frame and as child have empty image.

    Transform activePanel; // Hold the active page value (Ex: food panel, drink panel etc.)

    public void CreateItem(Transform panelToInstantiate, Item itemToİnstantiate)
    {
        GameObject slot = Instantiate(slotPrefab, panelToInstantiate);
        slot.transform.GetChild(0).GetComponent<Image>().sprite = itemToİnstantiate.icon;
        slot.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = itemToİnstantiate.itemName; // If you want a label (optioanal)
        slot.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = itemToİnstantiate.itemCost.ToString(); // This is optional if you want a label
        if (itemToİnstantiate as Food != null) // This is food
        {
            Character.energyAmount += (itemToİnstantiate as Food).energyAmount;
        }
        else if (itemToİnstantiate as Drink != null) // This is drink
            Character.energy++;
    }

    public void ChangePage(Transform transform)
    {
        // This is just place holder if you have more complicated system change code according to it. Currently we have only 3 panel
        foodPanel.gameObject.SetActive(false);
        drinkPanel.gameObject.SetActive(false);
        healthPanel.gameObject.SetActive(false);

        if (transform == foodPanel)
            foodPanel.gameObject.SetActive(true);
        else if (transform == drinkPanel)
            foodPanel.gameObject.SetActive(true);
        else if (transform == healthPanel)
            foodPanel.gameObject.SetActive(true);
    }

    void ScrollToItem(GameObject slot)
    {
        content.parent.GetComponent<ScrollView>().ScrollTo(slot.GetComponent<VisualElement>()); // I am not sure if that will work. Didn't try.
    }

    // Got this from stackoverflow. @maksymuik https://stackoverflow.com/a/30769550/9969193
    public void SnapTo(RectTransform target)
    {
        Canvas.ForceUpdateCanvases();

        contentPanel.anchoredPosition =
            (Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)
            - (Vector2)scrollRect.transform.InverseTransformPoint(target.position);
    }
}

public class Item : ScriptableObject // Test item class. Scriptable object
{
    public string itemName;
    public int itemCost;
    public Sprite icon;
}

public class Food : Item // Child of item class.
{
    public int energyAmount;
    public int deliciousness;
}

public class Drink : Item
{
    public bool isHot;
}
SeLeCtRa
  • 600
  • 1
  • 6
  • 14