显示隐藏GameObject并排序所有显示隐藏GameObject Unity C#

时间:2016-07-02 05:34:11

标签: c# hide unityscript gameobject

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

在我的代码下面:

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(生成插槽存储和显示我已将其添加到插槽存储的项目)

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(),则该项目将显示,并按存储3,2,1排序。因此,当我想显示库存时,我希望第一个节目是存储1,存储2和3是隐藏。怎么做 ?任何的想法 ?

感谢。

2 个答案:

答案 0 :(得分:0)

如果要显示或隐藏任何对象,可以使用transform postion z index。设置为对象变换postion new vector和z index -1或1 我假设主摄像头为零。

答案 1 :(得分:0)

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

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

//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首先处于活动状态。

由于