- Home /
tooltip on GUI layout ?
Hi All I am using GUILayout. But my problem is that I want to implament a tooltip into every button created how would I do that ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//============================================================================
// TowerManager
//----------------------------------------------------------------------------
//
//============================================================================
public class TowerManager : MonoBehaviour
{
//----------------------------------
// Public
//----------------------------------
public List<Tower> towers;
//----------------------------------
// Private
//----------------------------------
private int? activeButton = null;
private GUILayoutOption[] button = {GUILayout.Height(30), GUILayout.Width(30) };
//----------------------------------
// SelectedTower
//----------------------------------
public Tower selected
{
get
{
if( activeButton.HasValue )
{
return towers[(int)activeButton];
}
return null;
}
}
//----------------------------------
// Ghost
//----------------------------------
public Tower ghost;
//----------------------------------
// AWAKE
//----------------------------------
void Awake()
{
}
//----------------------------------
// ONGUI
//----------------------------------
void OnGUI()
{
GUILayout.BeginArea( new Rect( Screen.width - 50, 25, 50, Screen.height ) );
GUILayout.BeginVertical();
foreach( Tower tower in towers )
{
if( activeButton != towers.IndexOf(tower) &&
GUILayout.Button(tower.icon, button ) )
{
Griddy.Terrain.Clear();
activeButton = towers.IndexOf(tower);
Griddy.Cursor.state = CursorState.PlaceBuilding;
SetGhost( selected );
}
else if( activeButton == towers.IndexOf(tower) )
{
float a = Mathf.PingPong( Time.time, 1 );
GUI.color = new Color( 1, 1, 1, a );
GUILayout.Label(tower.icon, button );
GUI.color = Color.white;
}
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.EndArea();
}
// UPDATE
void Update()
{
if( Input.GetKeyDown( KeyCode.Space ) )
{
Clear();
Griddy.Cursor.Clear();
}
if( ghost != null)
{
if( Griddy.Grid.activeMarker != null)
{
ghost.SetPosition( Griddy.Grid.activeMarker.position.x, Griddy.Grid.activeMarker.position.y );
}
else
{
ghost.SetPosition( -100, -100 );
}
}
}
void SetGhost( Tower _ghost )
{
if( ghost != null )
Destroy(ghost.gameObject);
ghost = (Instantiate( _ghost.gameObject ) as GameObject).GetComponent<Tower>();
foreach( Material material in ghost.renderer.materials )
{
material.shader = Shader.Find("Transparent/Diffuse");
material.color = new Color( material.color.r, material.color.g, material.color.b, 0.7f );
}
}
public void Clear()
{
activeButton = null;
Destroy( ghost );
}
}
Could someone please format my code because my internet is down and I am using my blackberry and cannot do it myself ? :(
There you go! Damn that was painful. But easier on the eyes, now.
Why don't you use the inbuilt tooltip functionality? It's right there in the GUIContent!
Oh ha ha but how would I do that beacuse when I try it gives me errors ?
What kind of errors? All that means is that you're not making the GUIContent properly. I'm expecting something like this-
GUI.Label(some rect, new GUIContent("name on the label", "tooltip on the label"));
Then, you can get that using GUI.tooltip whenever you mouse over something!
That's an answer. PLEASE use answer, or I'll start posting answers just to have an answer. I'd rather you get the credit.
Yes please post an answer because I don't know how to make it answered if no one has actually posted one ?
Answer by Donovan_Rucastle · Nov 01, 2011 at 09:19 PM
@syclamoth thank that did it I was referring to it in the other script :/ don't know what I was thinking.
Answer from @ syclamoth
What kind of errors? All that means is that you're not making the GUIContent properly. I'm expecting something like this-
GUI.Label(some rect, new GUIContent("name on the label", "tooltip on the label")); Then, you can get that using GUI.tooltip whenever you mouse over something!
Your answer
Follow this Question
Related Questions
How can I create complex tooltips 2 Answers
tooltip to show from array C# 1 Answer
Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers
Display tooltips for objects in scene 1 Answer
automatic gui tooltips 1 Answer