繁体   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