【问题标题】:Show Hide GameObject And Sort All Show Hide GameObject Unity C#显示隐藏游戏对象并排序所有显示隐藏游戏对象 Unity C#
【发布时间】:2016-11-04 12:59:39
【问题描述】:

我在节目中遇到了一个奇怪的问题,隐藏游戏对象并对其进行排序。实际上排序它只是用来排序显示哪个游戏对象是第一,第二,第三。

在我的代码下面:

menu.cs(显示带有 3 个存储空间的库存)

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

public class menu : MonoBehaviour {
    public GameObject showInventory;
    public GameObject showRawStorage1;
    public GameObject showRawStorage2;
    public GameObject showRawStorage3;

    public Button storage1;
    public Button storage2;
    public Button storage3;

    bool active = false;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void onClickshowPlant () {
        if (active == true) {
            showInventory.SetActive (false);
            active = false;
        } else if (active == false) {
            showInventory.SetActive(true);
            active = true;
        }
    }

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

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

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

    public void HideAllSlot () {
        showRawStorage1.SetActive (false);
        showRawStorage2.SetActive (false);
        showRawStorage3.SetActive (false);
    }

    public void ShowAllSlot () {
        showRawStorage3.SetActive (true);
        showRawStorage2.SetActive (true);
        showRawStorage1.SetActive (true);
    }

    public void ShowStorage1 () {
        showRawStorage1.SetActive (true);
    }

    public void ShowStorage2 () {
        showRawStorage2.SetActive (true);
    }

    public void ShowStorage3 () {
        showRawStorage3.SetActive (true);
    }
}

Inventory.cs(生成 Slot Storage 并显示我已将其添加到 slot Storage 的项目)

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;
    public GameObject toolTip;
    public GameObject dragitemicon;
    public bool draggingitem = false;
    public item getdragitem;
    item itemxs;
    public int indexofdragitem;
    public Sprite icon;
    int maxItemRaw = 20;
    int maxItemValuable = 5;
    int maxItemAdmirable = 3;
    int sisa;
    itemDatabase database;
    int totalSlot = 60;
    int currentStorage = 1;
    int view = 20;

    menu menux;


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

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

            slotsx.Add(Slot);

            Player.items.Add(new item());

            addChilParent (this.gameObject,Slot);
            Slot.name = "slot-" + i;

            Slot.SetActive (true);

            slotAmount++;
        }

        //Generate the Slot and Slot Name at Storage 2;
        for(int i = 21; i <= 40; i++) {
            GameObject Slot = (GameObject) Instantiate(slots);
            Slot.GetComponent<slotScript>().slotNumber = slotAmount;

            slotsx.Add(Slot);

            Player.items.Add(new item());

            GameObject Rawstorage2 = GameObject.Find("RawStorage 2");
            addChilParent (Rawstorage2,Slot);
            Slot.name = "slot-" + i;

            Slot.SetActive (true);

            slotAmount++;
        }

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

            slotsx.Add(Slot);

            Player.items.Add(new item());

            GameObject Rawstorage3 = GameObject.Find("RawStorage 3");
            addChilParent (Rawstorage3,Slot);
            Slot.name = "slot-" + i;

            Slot.SetActive (true);
            slotAmount++;
        }



        AddItem (1);
        AddItem (2);

    }


    // Update is called once per frame
    void Update () {
        if (draggingitem) {
            Vector3 post = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
            dragitemicon.GetComponent<RectTransform>().position = new Vector3(post.x + 25, post.y - 25, post.z);

        }

    }

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

    //Add Item Method
    void AddItem(int ID) {
        for (int i = 0; i < database.items.Count; i++) {
            if(database.items[i].itemID == ID) {
                itemxs = new item (database.items [i].itemName,
                                  database.items [i].itemID,
                                  database.items [i].itemDesc,
                                  database.items [i].harvest,
                                  database.items [i].itemTime,
                                  database.items [i].stdprice,
                                  database.items [i].hightprice,
                                  database.items [i].itemStock,
                                  database.items [i].Lvlunlock,
                                  database.items [i].rawTree,
                                  database.items [i].itemType,
                                  database.items [i].itemProd,
                                  database.items [i].itemIcon,
                                  database.items [i].itemLocation,
                                  database.items [i].itemExp);

                CheckInventoryExist(itemxs);
                break;
            }
        }
    }

    //Add Item In Empty Slot
    void AddItemEmptySlot (item items, int sisa) {
        items.itemStock = sisa;
        for (int i = 0; i < Player.items.Count; i++) {
            if(Player.items[i].itemName == null) {
                Player.items[i] = items;
                break;
            }
        }

    }

    //Check Inventory is Exist
    void CheckInventoryExist(item IdItem) {
        sisa = IdItem.harvest;
        for (int i = 0; i < Player.items.Count; i++) {
            if(IdItem.itemType == item.ItemType.Raw) {
                for (int j = 1; j <= IdItem.harvest; j++) {
                    if(IdItem.itemID == Player.items[i].itemID && Player.items[i].itemID != null && Player.items[i].itemStock < maxItemRaw) {
                        Player.items[i].itemStock = Player.items[i].itemStock + 1;
                        sisa = sisa - 1;
                    }
                }
                if(sisa > 0 && sisa < IdItem.harvest) {
                    AddItemEmptySlot(IdItem,sisa);
                    break;
                }
                if (i == Player.items.Count - 1 && sisa == IdItem.harvest) {

                    AddItemEmptySlot(IdItem, sisa);
                    break;
                }

            }
            if(IdItem.itemType == item.ItemType.Valuable) {
                for (int j = 1; j <= IdItem.harvest; j++) {
                    if(IdItem.itemID == Player.items[i].itemID && Player.items[i].itemID != null && Player.items[i].itemStock < maxItemValuable) {
                        Player.items[i].itemStock = Player.items[i].itemStock + 1;
                        sisa = sisa - 1;
                    }
                }
                if(sisa > 0 && sisa < IdItem.harvest) {
                    AddItemEmptySlot(IdItem,sisa);
                    break;
                }
                if (i == Player.items.Count - 1 && sisa == IdItem.harvest) {
                    AddItemEmptySlot(IdItem, sisa);
                    break;
                }
            }
            if(IdItem.itemType == item.ItemType.Admirable) {
                for (int j = 1; j <= IdItem.harvest; j++) {
                    if(IdItem.itemID == Player.items[i].itemID && Player.items[i].itemID != null && Player.items[i].itemStock < maxItemAdmirable) {
                        Player.items[i].itemStock = Player.items[i].itemStock + 1;
                        sisa = sisa - 1;
                    }
                }
                if(sisa > 0 && sisa < IdItem.harvest) {
                    AddItemEmptySlot(IdItem,sisa);
                    break;
                }
                if (i == Player.items.Count - 1 && sisa == IdItem.harvest) {
                    AddItemEmptySlot(IdItem, sisa);
                    break;
                }
            }
        }
    }

}

问题是当我运行它时。它显示所有库存存储排序存储3、2、1。

当我输入时:

public void onClickshowPlant () {
        if (active == true) {
            showInventory.SetActive (false);
            active = false;
        } else if (active == false) {
            showInventory.SetActive(true);
            active = true;

            HideAllSlot ();
            ShowStorage1();

        }
    }

该项目已消失不显示。

但是,如果我删除 HideAllSlot() 和 ShowStorage1(),则该项目会显示出来,并且按 Storage 3 、 2 、 1 排序。因此,当我单击显示库存时,我希望第一个显示是存储 1,而存储 2 和 3 是隐藏的。怎么做 ?任何想法 ?

谢谢。

【问题讨论】:

    标签: c# unity3d hide gameobject


    【解决方案1】:

    如果您想显示或隐藏任何对象,您可以使用变换位置 z 索引。设置为对象变换位置新向量和 z 索引 -1 或 1 我假设主摄像头为零。

    【讨论】:

      【解决方案2】:

      嘿,我自己找到了解决方案。

      这就是我所做的。修改如下代码:

      //Generate the Slot and Slot Name;
              for(int i = 1; i <= 20; i++) {
                  GameObject Slot = (GameObject) Instantiate(slots);
                  Slot.GetComponent<slotScript>().slotNumber = slotAmount;
      
                  slotsx.Add(Slot);
      
                  Player.items.Add(new item());
                  GameObject Rawstorage1 = GameObject.Find("RawStorage 1");
                  addChilParent (Rawstorage1,Slot);
                  Slot.name = "slot-" + i;
      
                  Slot.SetActive (true);
      
                  slotAmount++;
              }
      
              //Generate the Slot and Slot Name;
              for(int i = 21; i <= 40; i++) {
                  GameObject Slot = (GameObject) Instantiate(slots);
                  Slot.GetComponent<slotScript>().slotNumber = slotAmount;
      
                  slotsx.Add(Slot);
      
                  Player.items.Add(new item());
      
                  GameObject Rawstorage2 = GameObject.Find("RawStorage 2");
                  addChilParent (Rawstorage2,Slot);
                  Slot.name = "slot-" + i;
      
                  Slot.SetActive (true);
      
                  slotAmount++;
              }
      
              //Generate the Slot and Slot Name;
              for(int i = 41; 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.name = "slot-" + i;
      
                  Slot.SetActive (true);
                  slotAmount++;
              }
      

      然后在gameObject上存储1、2、3排序就变成3、2、1

      这将在点击显示库存时首先激活存储 1。

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多