I like to use Handles.Button for various tools, but from time to time I’d like to pass my own controlID to the function. This function already exists, but is currently internal. Please simply make this overload public!
For example, one of my use cases: I have a series of buttons in the scene which can be clicked to select and connect different nodes in a graph. But before clicking, I would like to show a status text with the name of the target currently hovered and some detail information. For this, I would use HandleUtility.nearestControl == controlID to implement a simple Tooltip.
What I’m currently doing looks like the following:
public class MyCoolTool
{
private delegate bool ButtonDelegate(
int controlID, Vector3 position, Quaternion rotation, float size, float pickSize, Handles.CapFunction capFunction);
private readonly ButtonDelegate buttonMethod;
public MyCoolTool()
{
var type = typeof(Handles);
var method = type.GetMethod(
"Button",
BindingFlags.Static | BindingFlags.NonPublic,
null,
new[] { typeof(int), typeof(Vector3), typeof(Quaternion), typeof(float), typeof(float), typeof(Handles.CapFunction) },
null);
buttonMethod = (ButtonDelegate)System.Delegate.CreateDelegate(typeof(ButtonDelegate), method);
}
public void OnGUI()
{
int controlID = GUIUtility.GetControlID(FocusType.Passive);
if (buttonMethod.Invoke(controlID, Vector3.zero, Quaternion.identity, 1f, 1f, Handles.SphereHandleCap))
{
Debug.Log("Click!");
}
if (HandleUtility.nearestControl == controlID)
Handles.Label(new Vector3(0.4f, 0.6f, 0f), "Hover Tooltip");
}
}