【问题标题】:How to Scroll Page with script on Unity?如何在 Unity 上使用脚本滚动页面?
【发布时间】:2021-03-15 10:45:24
【问题描述】:

我想创建一个库存系统,我想使用 GUI.Button 创建我的按钮。我像这样创建它们。

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");
    
          
            
        }
    

我的问题是我无法移动它们。我的意思是我想在我的库存中添加一个滚动条。但即使我添加它,我的按钮也不会移动。如何像普通按钮一样动态移动它,或者如何使用脚本滚动它们?感谢您的帮助。

【问题讨论】:

  • 这不是老用户界面吗?在新的 UI 系统 GUI 类中仅用于编辑器。为什么不使用ScrollView 组件?它会自动设置滚动所需的一切。
  • 因为我要像库存元素一样使用这个按钮,它们必须是动态的。我的意思是按钮编号必须取决于用户。所以我需要使用脚本创建它们。如果我使用脚本,即使我使用 ScrollView 他们也不会移动
  • 我在很多事情中都使用了滚动视图,并且滚动视图支持动态(通过脚本)创建的元素。滚动视图可用于库存系统,您可以将其从脚本中移动。你在哪个部分有问题?因为如果你使用 GUI,除非你处理得很好,否则在不同的屏幕尺寸上,你的应用程序会看起来很奇怪。如果您发布与此问题相关的所有代码,我们可以更清楚地为您提供帮助。
  • 这是我的库存系统。我在此库存中有三个部分。食物、饮料和健康选项卡。当用户创建配置文件时,此库存将为空。但如果他从市场我的库存中购买东西系统必须创建一个按钮,并且必须将食物的图像放在按钮内。之后,如果他从市场购买另一种食物,则必须再次创建一个按钮。并且您必须使用脚本向下滚动。这是我上传的库存图片,因为我想让你轻松想象。 hizliresim.com/0GJG3K
  • 顺便说一句,如果我有另一个创建动态按钮的选项,你也可以告诉我:)

标签: c# unity3d mobile scroll uiscrollview


【解决方案1】:

我认为代码是不言自明的,但我会概括地解释一下。在 UI 中创建滚动视图。并为带有框架和图像的插槽创建一个预制件作为子对象。 对于布局,使用 GridLayout Group 或从代码创建水平布局,然后在需要时更改位置。然后使用此代码:

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;
}

【讨论】:

  • 哦,兄弟,你真是救命稻草。非常感谢,我会试试的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多