- Home /
How can I create complex tooltips
Hi,
Is there a way to create a complex tooltip for a box item? Meaning a single window containing both images and texts positioned as I need them.
I assume there ways like: -a mouse position check: if a box contains mouse and create a window -a lot of boxes in one place(?) each one containing a part of whole tooltip
But maybe there is a predefined instrument or something with less code writing?
Answer by Scribe · Feb 12, 2014 at 10:55 PM
Hello! as NickP suggested, it is often better to use GUIElements as they are only called when they are updated rather than OnGUI which is often called ~twice per frame although as he also mentioned they are much harder to work with on trickier problems and I think as you can only have 1 tooltip active at a time it may be better to use the OnGUI method for your problem.
The main part of this code is the custom class MyGUI.Tooltip. It requires a few arguments to be passed to it:
position : Vector2 => the position of the top, right corner of the tooltip
imageSize : int => the size that images should be displayed
spacer : int => a spacing value, to have some gaps between images
images : Texture2D[] => a list of the images you want shown
string : String => the string you want written
The is an example of how you can call it using a GUI.tooltip value in the OnGUI method.
Code
import System;
private var index : int;
private var imageList;
var imageList0 : Texture2D[];
var imageList1 : Texture2D[];
var imageList2 : Texture2D[];
var stringList : String[] = ["I am the first tooltip!", "im the second tooltip though my index is 1", "3"];
var imageSize : int = 32;
var spacerSize : int = 5;
public class MyGUI{
public static function Tooltip(position : Vector2, imageSize : int, spacer : int, images : Texture2D[], string : String){
var stringSize = GUI.skin.GetStyle("Box").CalcSize(GUIContent(string));
var imagePanel : Vector2 = Vector2(
imageSize*images.Length + spacer*Mathf.Max(0, images.Length-1) + spacer,
(imageSize+spacer)*Mathf.Min(1, images.Length) + spacer);
var tooltipSize = Vector2(
Mathf.Max(stringSize.x, imagePanel.x) + spacer,
imagePanel.y + stringSize.y + spacer);
GUI.BeginGroup(Rect(position.x, position.y, tooltipSize.x, tooltipSize.y));
GUI.Box(Rect(0, 0, tooltipSize.x, tooltipSize.y), "");
GUI.Label(
Rect(3 + (tooltipSize.x-stringSize.x)/2.0,
imagePanel.y,
stringSize.x,
stringSize.y),
string);
var imageSpacer = (tooltipSize.x - images.Length*imageSize)/(images.Length+1);
for(var i = 0; i < images.Length; i++){
GUI.DrawTexture(Rect(imageSpacer + i*(imageSize+imageSpacer), spacer, imageSize, imageSize), images[i]);
}
GUI.EndGroup();
}
}
function Start(){
imageList = [imageList0, imageList1, imageList2];
}
function OnGUI(){
GUI.Box (Rect (10, 10, 20, 20), GUIContent("", "0"));
GUI.Box (Rect (40, 10, 20, 20), GUIContent("", "1"));
GUI.Box (Rect (70, 10, 20, 20), GUIContent("", "2"));
if(GUI.tooltip != ""){
try{
index = Int32.Parse(GUI.tooltip);
MyGUI.Tooltip(Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y),
imageSize,
spacerSize,
imageList[index],
stringList[index]);
}catch(err : System.FormatException){
}
}
}
Scribe
Never seen your kind of method to create a GUI! Looks interesting, going to try it tonight!