- Home /
Loot GUI, chest, lists and gameobjects Problem.
Ok what want to make is to make a lootable chest or in my case a crate/box.
The problem I have is that I cannot get a good way to link the objects in the crate to the UI and then to the icon of the object.
I need an automatic way, so the UI when opening the crate will go looking for the icon of the related gameobject in the List of the crate.
Let me try and explain better in the code.
private Crate crateScript;
private BaseObjectClass objectScript;
private List<BaseObjectClass> crateItems;
private const string OBJECT_CAMEO_PATH = "UI/Cameos/";
// Use this for initialization
void Awake () {
crateScript = this.gameObject.GetComponent<Crate>();
// This is the crate items list.
crateItems = crateScript.baseObjeectList;
//This is the part I can't figure out... How to get the game objects in the list then get the name and load icon from resources?
//This is Pseudo retard code:
var item = crateItems.Find(The Items, (GameObject)Resources.Load("The Items Icon"));
//
}
//Now show the buttons.
void OnGUI(){
for(int cnt = 0; cnt < crateItems.Count; cnt++){
GUILayout.BeginHorizontal("box");
if(GUILayout.Button(item){
//Then get the object its linked to if the button is pressed.
//Pseudo Code:
item.transform.parent = player;
}
GUILayout.EndHorizontal();
// Hope you guys understand..
}
}
Answer by DeadKenny · Nov 14, 2013 at 01:39 AM
Case solved.
I think I just hit a wall. I took time off and after I came back, bam.
// _cameo is the texture I set in the Item and now its linked.
code:
for(int cnt = 0; cnt < crateItems.Count; cnt++){
GUILayout.BeginHorizontal("box");
if(GUILayout.Button(crateItems[cnt]._cameo){
Instatiate(bla bla bla);
}
GUILayout.EndHorizontal();
//
Only thing left is removing from list once clicked... and replacing button with texture.
Answer by undead-steve · Nov 12, 2013 at 12:38 AM
Have you tried storing the path to the correct icon as a field on each of the items in your crate?
class CrateItem : ScriptableObject
{
public string IconPath;
public CrateItem(icon)
{
IconPath = icon;
}
public Texture2D GetIcon()
{
return (Texture2D) Resources.load(IconPath) as Texture2D;
}
}
Yeah that is in the BaseObjectClass... but I replaced it with a public Texture2D that you have to set manually.
So now I just need to get the texture in the Gameobject in the list.
Still can't figure it out.
What about giving the crate a list of Texture2Ds
public Dictionary<crateItem, Texture2D> _ItemTextures;
Then in the awake you fill it out:
foreach (CrateItem c_item in crateItems)
{
_ItemTextures[c_item] = c_item.GetIcon();
}
and in the update code you use the list to draw the gui:
foreach (CrateItem c_item in crateItems)
{
Texture2D icon = _ItemTextures[c];
if GUILayout.Button(icon, GuiLayout.width(32))
{
// do something using the item
}
}
Your answer
Follow this Question
Related Questions
Real Simple Inventory and Vendor System Help 1 Answer
problem with gui 2 Answers
how make a for loop that makes GUI boxes ? 1 Answer
Changing the font size of a GUI.Button in the inspector 1 Answer
On Screen Button Controls 0 Answers