2016-07-02 30 views
1

Ich habe ein seltsames Problem bei der Show Verstecken Sie das Spielobjekt und sortieren Sie es. Eigentlich sortiere es einfach, um zu sortieren, welches Spielobjekt als erstes, zweites, drittes angezeigt wird.Anzeigen Ausblenden GameObject und Sortieren Alle Anzeigen Ausblenden GameObject Unity C#

Unter meinen Code:

menu.cs (Show Inventar Mit 3 Lagerung)

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 Lagerung und Show Item erzeugen, habe ich es hinzufügen, Storage-Slot)

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

} 

Das Problem ist, wenn ich es starte. Es zeigen die ganze Inventar Lagerung Art Lagerung 3, 2, 1.

Wenn ich gesagt:

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

      HideAllSlot(); 
      ShowStorage1(); 

     } 
    } 

Das Element wird nicht angezeigt verschwunden.

Aber wenn ich die HideAllSlot() und ShowStorage1() lösche, wird das Element angezeigt und ist sortiert nach Speicher 3, 2, 1. Also wenn ich klik zeige Inventar Ich möchte die erste Show ist Storage 1 und die Speicher 2 und 3 sind versteckt. Wie geht das ? Irgendeine Idee ?

Danke.

Antwort

0

Hey ich habe gerade die Lösung selbst gefunden.

Das habe ich gemacht. Ändern Sie den Code wie folgt:

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

und dann auf dem Gameobject Lagerung 1, 2 und 3 Sortieren werden es 3, 2, 1

Dies wird Speicher 1 aktiv zuerst, wenn klicken Sie auf Show Inventar machen.

Dank

0

Wenn Sie ein beliebiges Objekt ein- oder ausblenden möchten, können Sie die Transformationsposition z index verwenden. Setze auf Objekt transform Position neuen Vektor und z Index -1 oder 1 Ich nehme an, dass die Hauptkamera Null ist.