如何在Unity C#上做库存Paging?
How to make an inventory Paging on Unity C#?
如何使用 3 个存储进行盘点。例如:
如果我按下存储 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);
}
}
"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."
1、点击添加canvas
2、点击添加Button..其实就是添加三个
3、Button下面是按钮的"text"。标记三个按钮"Storage 1"、"Storage 2"、"Storage 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、那三个例程,你想怎么调用就怎么调用。你似乎已经有一个脚本可以做......某事。只需拥有三个这样的脚本,它们具有不同的 "amounts" 或您所说的任何内容。只需从三个不同的按钮调用三个不同的脚本。
请注意,您只是将三个都设置为隐藏,然后显示所需的项目。就这么简单!
在您的代码编辑中注意,
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。)
就用那个数组吧!
最后,事实上你应该这样做:
将所有插槽分组在三个空游戏对象下,仅 disable/enable 那些。这样你就不需要循环了。
如何使用 3 个存储进行盘点。例如:
如果我按下存储 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);
}
}
"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."
1、点击添加canvas
2、点击添加Button..其实就是添加三个
3、Button下面是按钮的"text"。标记三个按钮"Storage 1"、"Storage 2"、"Storage 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、那三个例程,你想怎么调用就怎么调用。你似乎已经有一个脚本可以做......某事。只需拥有三个这样的脚本,它们具有不同的 "amounts" 或您所说的任何内容。只需从三个不同的按钮调用三个不同的脚本。
请注意,您只是将三个都设置为隐藏,然后显示所需的项目。就这么简单!
在您的代码编辑中注意,
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。)
就用那个数组吧!
最后,事实上你应该这样做:
将所有插槽分组在三个空游戏对象下,仅 disable/enable 那些。这样你就不需要循环了。