2016-07-02 3 views
1

나는 게임 객체를 숨기고 그것을 정렬하는 데 이상한 문제가 있습니다. 실제로 하나의 gameobject가 첫 번째, 두 번째, 세 번째 중 어떤 것을 표시할지 정렬하기 위해 정렬합니다. 내 코드 아래Show GameObject 숨기기 Show All Game Show GameObject Unity 숨기기 C#

:

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 (I가 스토리지 슬롯에 추가해야 슬롯 저장 및 표시 항목을 생성)

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; 
       } 
      } 
     } 
    } 

} 

문제는 내가 실행했을 때입니다.

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

      HideAllSlot(); 
      ShowStorage1(); 

     } 
    } 

이 항목이 사라 표시되지 않은 : 그것은 모든 재고 스토리지 정렬 저장 3, 2,

1. 내가 넣어 표시합니다.

하지만 HideAllSlot() 및 ShowStorage1()을 삭제하면 항목이 표시되고 Storage 3, 2, 1에 따라 정렬 순서가됩니다. 그래서 내가 Klik 인벤토리를 보여줄 때 첫 번째 쇼는 스토리지 1이고 스토리지 2와 3은 숨기기를 원합니다. 그렇게하는 방법 ? 어떤 아이디어?

감사합니다.

답변

0

안녕하세요. 방금 해결책을 직접 찾았습니다.

이것은 내가 한 일입니다. 코드를 아래와 같이 변경합니다 : 게임 오브젝트 스토리지 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++; 
     } 

다음을, 3 정렬이 될이 것 하나

3, 2, 저장 한 활성 첫째 때 쇼 재고에 클릭.

감사합니다.

0

개체를 표시하거나 숨기려면 postion z index를 사용하면됩니다. 객체 변형 위치로 설정 새 벡터 및 z 인덱스 -1 또는 1 주 카메라가 0이라고 가정합니다.