Question by
dennz_liu · Jul 03, 2016 at 12:21 PM ·
c#unity 5inventory systemslot
How to make an inventory Paging on Unity C# ?
Hi everyones,
How to make an inventory with 3 Storage. For example :
if i push storage 1 button it will show 20 Slots at inventory,
if i push storage 2 button it will show from 21 Slots until 40 Slots at inventory, and
if i push storage 3 button it will show from 41 Slots until 60 Slots at inventory.
so it have all 60 slots total.
Below its my code :
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;
slotAmount++;
}
}
//Add Slot Child To GridSlot Game Object
public void addChilParent(GameObject parentx, GameObject childx) {
childx.transform.SetParent (parentx.gameObject.transform);
}
}
Thanks
Comment
Answer by ManxJason86 · May 08, 2017 at 12:58 PM
Paging can easily be achieve with LINQ, for example:
int pageLimit = 20;
IEnumerable<item> items = new List<item>();
// then populate your items as you usually would.
IEnumerable<item> GetPagedItems(int pageNum)
{
return items.skip((pageNum - 1) * pageLimit).Take(pageLimit);
}
Your answer
Follow this Question
Related Questions
Inventory master no slotscript.cs? 1 Answer
Removing item from list makes it appear at the bottom ? 1 Answer
Loot table problems 1 Answer
Rotation script issue? 2 Answers
Game crashes on iPhone 11 0 Answers