簡體   English   中英

如何使GUI Rect對象的子級?

[英]How to make GUI Rect child of an object?

我有一個問題,我用GUI繪制了一個新的Rect:它現在只能在屏幕上繪制。 我想要做的是使此繪圖成為對象的子級,因此我可以使用SetActive(false)將其隱藏。 僅當玩家暫停並打開庫存時,此功能才可用。 該游戲是2D Android版。

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

要以編程方式在Unity中設置childGameObject.transform.SetParent(parentGameObject);的父代,只需使用以下代碼: childGameObject.transform.SetParent(parentGameObject);

編輯:事實證明,使用OnGUI並不是那么簡單。 遵循Draco18s的建議並使用新的GUI系統。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM