|
public static class CustomGUI { |
|
public static bool CustomButton(Rect position, GUIContent label, GUIStyle style) { |
|
bool wasClicked = false; |
|
|
|
int controlID = GUIUtility.GetControlID(FocusType.Passive); |
|
switch (Event.current.GetTypeForControl(controlID)) { |
|
case EventType.MouseDown: |
|
if (Event.current.button == 0 && position.Contains(Event.current.mousePosition)) { |
|
GUIUtility.hotControl = controlID; |
|
Event.current.Use(); |
|
} |
|
break; |
|
case EventType.MouseDrag: |
|
if (GUIUtility.hotControl == controlID) |
|
Event.current.Use(); |
|
break; |
|
case EventType.MouseUp: |
|
if (GUIUtility.hotControl == controlID) { |
|
GUIUtility.hotControl = 0; |
|
if (Event.current.button == 0 && position.Contains(Event.current.mousePosition)) |
|
wasClicked = true; |
|
Event.current.Use(); |
|
} |
|
break; |
|
case EventType.KeyDown: |
|
if (Event.current.keyCode == KeyCode.Escape && GUIUtility.hotControl == controlID) { |
|
GUIUtility.hotControl = 0; |
|
Event.current.Use(); |
|
} |
|
break; |
|
case EventType.Repaint: |
|
style.Draw(position, label, controlID); |
|
break; |
|
} |
|
|
|
return wasClicked; |
|
} |
|
|
|
// Default button style overload: |
|
public static bool CustomButton(Rect position, GUIContent label) { |
|
return CustomButton(position, label, GUI.skin.button); |
|
} |
|
|
|
// Layout overload: |
|
public static bool CustomButton(GUIContent label, GUIStyle style) { |
|
Rect position = GUILayoutUtility.GetRect(label, style); |
|
return CustomButton(position, label, style); |
|
} |
|
|
|
// Layout overload with default button style overload: |
|
public static bool CustomButton(GUIContent label) { |
|
return CustomButton(label, GUI.skin.button); |
|
} |
|
} |