- Home /
GUI.tooltip is not set when hovering dynamic created buttons
Like many other people before and after me i'm working on a RPG inventory and i got some problems with getting a tooltip when hovering over the different Items.
Everything works fine but the tooltip...
I tried nearly any of the ideas i found in unityAnswers and inside some unity community forums.
I'm coding in c# so for now i use a new GUIContent(string, string) to set the tooltip and yes, that works perfectly on few buttons i specified exactly, each by its own rect and throws it out on Debug, boxes, labels aso.
But in my inventory i don't want to define every single button so i'm using a for loop running through lines and rows, creating all buttons. I guess somehow the tooltip information is not set or changes to fast to be shown inside this loop.
Anyone got any other idea on how to get this done?
/// <summary>
/// Dos the bag window.
/// </summary>
/// <param name='_id'>
/// _id.
/// </param>
void DoBagWindow (int _id) {
int _fieldCount = 0;
string _itemDescr = string.Empty;
Rect _fieldRect = new Rect(0,0,0,0);
Texture2D _tempTex = _defaultTexture;
BaseItem _tempItem = null;
if(GUI.Button(new Rect(_screenHeight-25, 5, 20,20), "X", healthtext)){
_selectedWindow = 100;
}
GUI.BeginGroup(new Rect(10, 10, 300, 300), string.Empty);
for(int _lines = 0; _lines < 7; _lines++){
for(int _rows = 0; _rows < 7; _rows++){
_fieldCount++;
_fieldRect = new Rect(_rows*40, 10+_lines*40, 40, 40);
if(_inventory.INVENTORYSIZE > _fieldCount){
_tempItem = _inventory.GetItemAtSlot(_fieldCount);
if(_tempItem != null){
_tempTex = _tempItem.APPEARENCE;
_itemDescr = _tempItem.DESCRIPTION;
}
else{
_tempTex = _defaultTexture;
}
}
if(GUI.Button(_fieldRect, new GUIContent(_tempTex, _itemDescr))){
if(_tempItem != null)
Debug.Log("Clicked " + _tempItem.NAME);
else
Debug.Log("Clicked an empty Slot");
}
}
}
GUI.EndGroup();
}
Answer by Mahtrok · Dec 17, 2012 at 02:13 PM
Ok, sometimes its really the best just to leave it alone for a few hours of sleep.
If i do set the tooltip during one loop, the program simply sets another bool true to remember that i found a button with event.current.mouseposition in it. if this bool is false i reset the tooltip, if true tooltip is displayed.
for(int _lines = 0; _lines < 7; _lines++){
for(int _rows = 0; _rows < 7; _rows++){
_fieldCount++;
_fieldRect = new Rect(_rows*40, 10+_lines*40, 40, 40);
if(_inventory.INVENTORYSIZE > _fieldCount){
_tempItem = _inventory.GetItemAtSlot(_fieldCount);
if(_tempItem != null){
_found = true;
_tempTex = _tempItem.APPEARENCE;
_itemDescr = _tempItem.DESCRIPTION;
}
}
if(GUI.Button(_fieldRect, new GUIContent(_tempTex, _itemDescr))){
if(_tempItem != null)
Debug.Log("Clicked " + _tempItem.NAME);
else
Debug.Log("Clicked an empty Slot");
}
}
}
if(!_found){
_tempTex = _defaultTexture;
_itemDescr = string.Empty;
}