如何在Unity C#上进行库存分页?

问题描述 投票:0回答:1

如何使用 3 Storage 进行盘点。例如:

如果我按下存储 1 按钮,它将在库存中显示 20 个插槽,

如果我按下存储 2 按钮,它将显示库存中的 21 个插槽到 40 个插槽,并且

如果我按存储 3 按钮,它将显示库存中的 41 个插槽到 60 个插槽。

所以总共有 60 个插槽。

下面是我的代码:

库存.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;

 public class inventory : MonoBehaviour {
     public List<GameObject> slotsx = new List<GameObject> ();
     public player Player;
     public List<item> itemx = new List<item> ();
     public GameObject slots;
     item itemxs;
     public int indexofdragitem;
     public Sprite icon;

     int sisa;
     itemDatabase database;
     int totalSlot = 60;
     int currentStorage = 1;
     int view = 20;

     // Use this for initialization
     void Start () {
         Player = new player();
         int slotAmount = 0;
         database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();


         //Generate the Slot and Slot Name;
         for(int i = 1; i <= 60; i++) {
                 GameObject Slot = (GameObject) Instantiate(slots);
                 Slot.GetComponent<slotScript>().slotNumber = slotAmount;

                 slotsx.Add(Slot);

                 Player.items.Add(new item());
                 addChilParent (this.gameObject,Slot);
                 //Slot.transform.parent = this.gameObject.transform;
                 Slot.name = "slot-" + i;
                 Slot.SetActive(false);
                 slotAmount++;

         }
      ShowStorage1();

     }

     //Add Slot Child To GridSlot Game Object
     public void addChilParent(GameObject parentx, GameObject childx) {
         childx.transform.SetParent (parentx.gameObject.transform);
     }
  }

谢谢

更新代码:

public void onClickStorage1() {
        HideAllSlot ();
        ShowStorage1 ();
    }

    public void onClickStorage2() {
        HideAllSlot ();
        ShowStorage2 ();
    }

    public void onClickStorage3() {
        HideAllSlot ();
        ShowStorage3 ();
    }

    public void HideAllSlot () {
        GameObject hslot;
        for(int i = 1; i <= 60; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(false);
        }
    }

    public void ShowStorage1 () {
        GameObject hslot;
        for(int i = 1; i <= 20; i++) {
            hslot = GameObject.Find("slot-"+i).SetActive(true);
            hslot.SetActive(true);
        }
    }

    public void ShowStorage2 () {
        GameObject hslot;
        for(int i = 21; i <= 40; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(true);
        }
    }

    public void ShowStorage3 () {
        GameObject hslot;
        for(int i = 41; i <= 60; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(true);
        }
    }
c# unity-game-engine pagination inventory
1个回答
1
投票

“如果我按下存储 1 按钮,它将显示库存中的 20 个插槽,如果我按下存储 2 按钮,它将显示库存中从 21 个插槽到 40 个插槽,如果我按下存储 3 按钮,它将显示从 41 个插槽到 60 个插槽在库存。”

1、点击添加画布

2、点击添加按钮..其实是添加三个

3、Button下有按钮的“文字”。 将三个按钮标记为“存储 1”、“存储 2”、“存储 3”

4,有一个类似这样的脚本...

public GameObject yourFIRSTPanel;
public GameObject yourSECONDPanel;
public GameObject yourTHIRDPanel;

private void HideAllThreePanels()
 {
 yourFIRSTPanel.setActive(false);
 yourSECONDPanel.setActive(false);
 yourTHIRDPanel.setActive(false);
 }



public void UserClickedS1()
 {
 Debug.Log("storage 1 needed!")
 HideAllThreePanels()
 yourFIRSTPanel.setActive(true);
 }
public void UserClickedS2()
 {
 Debug.Log("storage 2 needed!")
 HideAllThreePanels()
 yourSECONDPanel.setActive(true);
 }
public void UserClickedS3()
 {
 Debug.Log("storage 3 needed!")
 HideAllThreePanels()
 yourTHIRDPanel.setActive(true);
 }

5、在这三个例程中,想叫什么就叫什么。 你似乎已经有了一个可以做......某事的脚本。 只需有三个这样的脚本,它们具有不同的“金额”或您所说的任何内容。 只需从三个不同的按钮调用三个不同的脚本即可。

请注意,您只需将所有三个设置为隐藏,然后显示所需的项目即可。就这么简单!


在代码编辑中注意,

   for(int i = 1; i <= 20; i++) {
        hslot = GameObject.Find("slot-"+i).SetActive(true);
        hslot.SetActive(true);
    }

这是错误的:“hslot = GameObject.Find("slot-"+i).SetActive(true);”

整个循环也是错误的,应该是

  for(int i = 1; i <= 20; i++) {
       String theName = "slot-" + i.ToString();
       Debug.Log("Looking for: " +theName);
       GameObject gFound = GameObject.Find("slot-"+i);
       if (gFound == nil)
        {
        Debug.Log("COULD NOT FIND IT! " +theName);
        continue;
        }
       gFound.SetActive(true);
    }

但是请注意!

GameObject.Find 找不到不活动的对象!

请注意,您已经将它们保存在数组中! (我认为是slotsx。)

只需使用该数组即可!


最后事实上你应该这样做:

将所有插槽分组到三个空游戏对象下,然后禁用/启用它们。这样你就不需要循环了。

© www.soinside.com 2019 - 2024. All rights reserved.