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..
}
}
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.
– DeadKennyWhat 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
– undead-stevec ; if GUILayout.Button(icon, GuiLayout.width(32)) { // do something using the item } }