制作库存清单

时间:2018-05-03 11:47:30

标签: c# unity3d

我正在尝试为我的枪创建库存,但我的库存 ui 中的列表存在问题。这是我用于我的列表的代码:

public void updateInventory()
{
    int index = 0;

    if (Buttonlist != null)
    {
        foreach (GameObject ob in Buttonlist)
        {
            DestroyImmediate(ob, false);
        }
        Buttonlist.Clear();
        ButtonId = 0;
        Position = new Vector3(0, 0, 0);
    }

    Canvas.ForceUpdateCanvases();
    foreach (Gun item in gunScript.secInventory)
    {
        GameObject temp = Instantiate(getButton) as GameObject;
        Buttonlist.Add(temp);

        Buttonlist[index].transform.position = getButton.transform.position + Position;
        Buttonlist[index].GetComponentInChildren<Text>().text = item.Name;
        Buttonlist[index].name = ButtonId.ToString();
        Buttonlist[index].AddComponent<Button_Clicked>();
        Buttonlist[index].transform.parent = Content;
        Buttonlist[index].SetActive(true);

        Position.y -= 49;
        ButtonId++;
        index++;
    }

}

是否有修复或击球方式将项目添加到列表中?

1 个答案:

答案 0 :(得分:2)

您可以使用GameObject temp = Instantiate(getButton) as GameObject;代替Buttonlist[index]

使用Extract MethodupdateInventory方法更清晰

public void updateInventory()
{
    int index = 0;

    if (Buttonlist != null)
    {
        foreach (GameObject ob in Buttonlist)
        {
            DestroyImmediate(ob, false);
        }
        Buttonlist.Clear();
        ButtonId = 0;
        Position = new Vector3(0, 0, 0);
    }

    Canvas.ForceUpdateCanvases();
    foreach (Gun item in gunScript.secInventory)
    {
        GameObject buttonObj = SetButtonItem(ButtonId.ToString(),item.Name,Position);
        Buttonlist.Add(buttonObj);
        Position.y -= 49;
        ButtonId++;
    }
}

private GameObject SetButtonItem(string ButtonId,string itemName,Vector3 pos)
{
    GameObject temp = Instantiate(getButton) as GameObject;

    temp.transform.position = getButton.transform.position + pos;
    temp.GetComponentInChildren<Text>().text = itemName;
    temp.name = ButtonId;
    temp.AddComponent<Button_Clicked>();
    temp.transform.parent = Content;
    temp.SetActive(true);

    return temp;
}

PositionButtonId似乎是班级中的字段,所以你可以这样使用。

public void updateInventory()
{
    int index = 0;

    if (Buttonlist != null)
    {
        foreach (GameObject ob in Buttonlist)
        {
            DestroyImmediate(ob, false);
        }
        Buttonlist.Clear();
        ButtonId = 0;
        Position = new Vector3(0, 0, 0);
    }

    Canvas.ForceUpdateCanvases();
    foreach (Gun item in gunScript.secInventory)
    {
        GameObject buttonObj = SetButtonItem(item.Name);
        Buttonlist.Add(buttonObj);
        Position.y -= 49;
        ButtonId++;
    }
}

private GameObject SetButtonItem(string itemName)
{
    GameObject temp = Instantiate(getButton) as GameObject;

    temp.transform.position = getButton.transform.position + Position;
    temp.GetComponentInChildren<Text>().text = itemName;
    temp.name = ButtonId.ToString();
    temp.AddComponent<Button_Clicked>();
    temp.transform.parent = Content;
    temp.SetActive(true);

    return temp;
}