Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created September 7, 2011 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/1200314 to your computer and use it in GitHub Desktop.
Save AngryAnt/1200314 to your computer and use it in GitHub Desktop.
Quick example of a custom GUILayout control (untested).
using UnityEngine;
public class CustomGUILayout
{
public static bool MyFocusControl (Texture2D texture, params GUILayoutOption[] options)
{
int id = GUIUtility.GetControlID ();
Color color = GUI.color;
GUI.color = GUIUtility.hotControl == id ? Color.green : Color.red;
Rect rect = GUILayoutUtility.GetRect (texture.width, texture.height, options);
switch (Event.current.type)
{
case EventType.MouseDown:
if (rect.Contains (Event.current.mousePosition))
{
GUIUtility.hotControl = id;
Event.current.Use ();
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id)
{
GUIUtility.hotControl = 0;
Event.current.Use ();
}
break;
case EventType.Repaint:
GUI.DrawTexture (rect, texture);
break;
}
GUI.color = color;
return GUIUtility.hotControl == id;
}
}
/*
Usage:
bool isActive = CustomGUILayout.MyFocusControl (myTexture);
GUILayout.Label (string.Format ("The custom control is {0}", isActive ? "active" : "inactive"));
*/
@petersvp
Copy link

It works. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment