Skip to content

Instantly share code, notes, and snippets.

@gluschenko
Last active August 29, 2015 14:14
Show Gist options
  • Save gluschenko/f54bca93c91930fd8011 to your computer and use it in GitHub Desktop.
Save gluschenko/f54bca93c91930fd8011 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class UI : MonoBehaviour {
public static bool TooltipButton(Rect rect, Texture Content, string Tooltip, int TooltipSide = 0)
{
Vector2 InputPos = InputPosition();
bool Button = GUI.Button(rect, Content);
if(InputPos.x > rect.x && InputPos.y > rect.y && InputPos.x < rect.x + rect.width && InputPos.y < rect.y + rect.height)
{
if(Tooltip != "")
{
float TooltipWidth = Perc((Tooltip.Length * 2.3f) + 6f);
Rect TooltipRect = new Rect();
if(TooltipSide == 0)TooltipRect = new Rect(rect.x + rect.width/2f - TooltipWidth/2f, rect.y - Perc(8), TooltipWidth, Perc(6));//Верх
if(TooltipSide == 1)TooltipRect = new Rect(rect.x + rect.width/2f - TooltipWidth/2f, rect.y + rect.height + Perc(1), TooltipWidth, Perc(6));//Низ
if(TooltipSide == 2)TooltipRect = new Rect(rect.x - TooltipWidth - Perc(2), rect.y + rect.height/2 - Perc(3), TooltipWidth, Perc(6));//Лево
if(TooltipSide == 3)TooltipRect = new Rect(rect.x + rect.width + Perc (2), rect.y + rect.height/2 - Perc(3), TooltipWidth, Perc(6));//Право
GUI.Box(TooltipRect, Tooltip);
}
}
return Button;
}
/*public static bool CounterButton(Rect rect, Texture texture, int count)
{
bool Button = GUI.Button(rect, texture);
GUI.Box(new Rect(rect.x + rect.width - Perc(7), rect.y + rect.height - Perc(6), Perc(6.5f), Perc(5.5f)), count + "");
///
if(Button)
{
return true;
}
return false;
}*/
///
public static Vector2 InputPosition()
{
if(!Platform.isMobile())
{
return new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
}
else
{
return (Input.touchCount > 0)? new Vector2(Input.touches[0].position.x, Screen.height - Input.touches[0].position.y) : new Vector2(-100, -100);
}
}
public static Rect PercRect(float x, float y, float width, float height)
{
return new Rect(Perc(x), Perc(y), Perc(width), Perc(height));
}
public static float Perc(float rate)
{
float Pix = (Screen.height > Screen.width) ? Screen.width : Screen.height;//За сравнение берется меньший параметр
float OnePerc = Pix / 100;//Количество точек на процент
return OnePerc * rate;
}
public static float Pix(float pixrate)
{
float OnePerc = Perc(1);
return (pixrate/OnePerc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment