Hey everyone, is there a way to put an if statement inside of a GUI.Tooltip? I want an if statement to check if a variable is null and if it is don’t display it in the GUI.Tooltip. I’m not sure if I want to put the if statement inside of the GUI.Button or above the GUI.Label.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public int someInt;
public string someString;
public float someFloat;
void OnGUI() {
GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", someInt.ToString(),"
“+someString,”
"+someFloat.ToString()));
GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
}
}
public int someInt;
public string someString;
public float someFloat;
void OnGUI()
{
GUI.Button(new Rect(10, 10, 100, 20), new GUIContent(
“Click me”,
someInt.ToString() +
(string.IsNullOrEmpty(someString) ? “” : "
"+someString) +
"
"+someFloat.ToString()));
GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
}
You can just create the tool tip string as a variable like this:
void OnGUI() {
if(someVar == null)
{
// someVar is null so don't add it to the tool tip
var buttonString = someFloat.ToString();
}
else
{
// someVar is not null so add it to the tool tip
var buttonString = someFloat.ToString()+"
"+someVar.ToString();
}
GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", buttonString));
GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
}