C# 如何使一个对象的GUI Rect子对象?
我有一个问题,我用GUI绘制了一个新的Rect:它现在只在屏幕上绘制。我想做的是将此图形设置为对象的子对象,以便使用C# 如何使一个对象的GUI Rect子对象?,c#,user-interface,unity3d,rect,unity2d,C#,User Interface,Unity3d,Rect,Unity2d,我有一个问题,我用GUI绘制了一个新的Rect:它现在只在屏幕上绘制。我想做的是将此图形设置为对象的子对象,以便使用SetActive(false)隐藏此图形。这将只在玩家暂停并打开库存时可用。这款游戏是针对Android的2D游戏 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Inventory : MonoBehaviour { public i
SetActive(false)
隐藏此图形。这将只在玩家暂停并打开库存时可用。这款游戏是针对Android的2D游戏
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"));
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类清单:单一行为
{
公共int SlotsX,SlotsY;
公众皮肤;
公共列表库存=新列表();
公共列表插槽=新列表();
私有项目数据库;
void Start()
{
对于(int i=0;i<(SlotsX*SlotsY);i++)
{
添加(新项());
}
database=GameObject.FindGameObjectWithTag(“项目数据库”).GetComponent();
inventory.Add(database.Items[0]);
inventory.Add(database.Items[1]);
inventory.Add(database.Items[3]);
inventory.Add(数据库项[4]);
存货.Add(数据库.Items[5]);
库存.添加(数据库.项目[6]);
}
void OnGUI()
{
GUI.skin=皮肤;
提取存货();
对于(int i=0;i
要以编程方式设置Unity中游戏对象的父对象,只需使用以下命令:
childGameObject.transform.SetParent(parentGameObject);
编辑:
事实证明,使用OnGUI并不是这么简单。按照Draco18s的建议使用新的GUI系统。不幸的是,这不适用于使用OnGui创建的GUI元素。停止使用OnGui,改用。