2017-09-17 42 views
0

我有這個問題,我用GUI畫了一個新的矩形:它現在只在屏幕上繪製。我想要做的就是讓這張圖畫成爲一個物體的孩子,所以我可以用SetActive(false)來隱藏它。這隻有在玩家暫停並打開庫存時纔可用。 Android的遊戲是2D的。如何製作對象的GUI Rect子對象?

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Inventory : MonoBehaviour 
{ 
    public int SlotsX, SlotsY; 

    public GUISkin Skin; 

    public List<Item> inventory = new List<Item>(); 
    public List<Item> slots = new List<Item>(); 

    private ItemDatabase database; 

    void Start() 
    { 
     for (int i = 0; i < (SlotsX * SlotsY); i++) 
     { 
      slots.Add(new Item()); 
     } 

     database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>(); 
     inventory.Add(database.Items[0]); 
     inventory.Add(database.Items[1]); 
     inventory.Add(database.Items[3]); 
     inventory.Add(database.Items[4]); 
     inventory.Add(database.Items[5]); 
     inventory.Add(database.Items[6]); 
    } 

    void OnGUI() 
    { 
     GUI.skin = Skin; 
     DrawInventory(); 
     for (int i = 0; i < inventory.Count; i++) 
     { 
      GUI.Label(new Rect(10, i * 40, 200, 50), inventory[i].itemName); 
     } 
    } 

    void DrawInventory() 
    { 
     for (int x = 0; x < SlotsX; x++) 
     { 
      for (int y = 0; y < SlotsY; y++) 
      { 
       GUI.Box(new Rect(x * 180, y * 180, 160, 160), "", Skin.GetStyle("Slot")); 
      } 
     } 
    } 
} 
+0

停止使用OnGui並改用[新的4.6 UI](https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui)。 – Draco18s

回答

0

要設置一個遊戲對象的父在Unity 編程,只需使用: childGameObject.transform.SetParent(parentGameObject);

編輯: 原來不是這個簡單的用OnGUI。遵循Draco18的建議並使用新的GUI系統。

+0

這......不幸的是不適用於使用OnGui創建的GUI元素。 – Draco18s