Created
September 7, 2011 11:19
-
-
Save AngryAnt/1200314 to your computer and use it in GitHub Desktop.
Quick example of a custom GUILayout control (untested).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works. Thanks.