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

时间:2016-07-01 11:41:18

标签: c# unity3d paging unityscript inventory

如何使用3 Storage制作库存。例如:

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

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

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

所以它总共有60个插槽。

在我的代码之下:

inventory.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);
        }
    }

1 个答案:

答案 0 :(得分:1)

  

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

1,点击添加画布

2,点击添加按钮..实际上添加三个

3,在按钮下面有按钮的“文本”。标记三个按钮“存储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不会找到非活动对象!

请注意,您已经在阵列中保留了它们! (slotx我想。)

只需使用该阵列!

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

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