Skip to content

Instantly share code, notes, and snippets.

@ethanedits
Created July 13, 2021 06:36
Show Gist options
  • Select an option

  • Save ethanedits/9c97dfc3746412007c43101e93dfbc98 to your computer and use it in GitHub Desktop.

Select an option

Save ethanedits/9c97dfc3746412007c43101e93dfbc98 to your computer and use it in GitHub Desktop.
Render Class for GUI methods in Unity
using System;
using UnityEngine;
public class Render : MonoBehaviour
{
public static GUIStyle StringStyle { get; set; } = new GUIStyle(GUI.skin.label);
public static Color Color
{
get { return GUI.color; }
set { GUI.color = value; }
}
public static void DrawBox(Vector2 position, Vector2 size, Color color, bool centered = true)
{
Color = color;
DrawBox(position, size, centered);
}
public static void DrawBox(Vector2 position, Vector2 size, bool centered = true)
{
var upperLeft = centered ? position - size / 2f : position;
GUI.DrawTexture(new Rect(position, size), Texture2D.whiteTexture, ScaleMode.StretchToFill);
}
public static void DrawString(Vector2 position, string label, Color color, bool centered = true)
{
Color = color;
DrawString(position, label, centered);
}
public static void DrawString(Vector2 position, string label, bool centered = true)
{
var content = new GUIContent(label);
var size = StringStyle.CalcSize(content);
var upperLeft = centered ? position - size / 2f : position;
GUI.Label(new Rect(upperLeft, size), content);
}
public static Texture2D lineTex;
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)
{
Matrix4x4 matrix = GUI.matrix;
if (!lineTex)
lineTex = new Texture2D(1, 1);
Color color2 = GUI.color;
GUI.color = color;
float num = Vector3.Angle(pointB - pointA, Vector2.right);
if (pointA.y > pointB.y)
num = -num;
GUIUtility.ScaleAroundPivot(new Vector2((pointB - pointA).magnitude, width), new Vector2(pointA.x, pointA.y + 0.5f));
GUIUtility.RotateAroundPivot(num, pointA);
GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1f, 1f), lineTex);
GUI.matrix = matrix;
GUI.color = color2;
}
public static void DrawBox(float x, float y, float w, float h, Color color, float thickness)
{
DrawLine(new Vector2(x, y), new Vector2(x + w, y), color, thickness);
DrawLine(new Vector2(x, y), new Vector2(x, y + h), color, thickness);
DrawLine(new Vector2(x + w, y), new Vector2(x + w, y + h), color, thickness);
DrawLine(new Vector2(x, y + h), new Vector2(x + w, y + h), color, thickness);
}
public static void DrawBoxOutline(Vector2 Point, float width, float height, Color color, float thickness)
{
DrawLine(Point, new Vector2(Point.x + width, Point.y), color, thickness);
DrawLine(Point, new Vector2(Point.x, Point.y + height), color, thickness);
DrawLine(new Vector2(Point.x + width, Point.y + height), new Vector2(Point.x + width, Point.y), color, thickness);
DrawLine(new Vector2(Point.x + width, Point.y + height), new Vector2(Point.x, Point.y + height), color, thickness);
}
}
Copy link

ghost commented Oct 14, 2022

Helpful

@Da3m0nRU
Copy link

TY!

@mechv0d
Copy link

mechv0d commented Jan 30, 2024

w

@ALBETSY
Copy link

ALBETSY commented Feb 18, 2024

thanks

@vintagekid80s
Copy link

thanks g

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