Skip to content

Instantly share code, notes, and snippets.

@Josef212
Created October 27, 2019 22:37
Show Gist options
  • Save Josef212/3e7764c12d719b360d594d0d91a796ca to your computer and use it in GitHub Desktop.
Save Josef212/3e7764c12d719b360d594d0d91a796ca to your computer and use it in GitHub Desktop.
public class GUIDebug : Monobehaviour
{
public static Bounds GetRectTransformBounds(RectTransform transform)
{
Vector3[] WorldCorners = new Vector3[4];
transform.GetWorldCorners(WorldCorners);
Bounds bounds = new Bounds(WorldCorners[0], Vector3.zero);
for(int i = 1; i < 4; ++i)
{
bounds.Encapsulate(WorldCorners[i]);
}
return bounds;
}
private static Texture2D MakeTexture(float opacity)
{
Texture2D texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, true);
texture.SetPixel(0, 0, new Color(1, 0, 0, opacity));
texture.SetPixel(0, 1, new Color(0, 1, 0, opacity));
texture.SetPixel(1, 0, new Color(0, 0, 1, opacity));
texture.SetPixel(1, 1, new Color(1, 1, 1, opacity));
texture.Apply();
texture.filterMode = FilterMode.Point;
return texture;
}
public float opacity = 0.5f;
private Texture2D tex = null;
private void OnValidate() => tex = MakeTexture(opacity);
private void Awake() => tex = MakeTexture(opacity);
private void OnDrawGizmos()
{
Bounds bounds = GetRectTransformBounds(transform as RectTransform);
Rect screenRect = new Rect(bounds.min, bounds.size);
Gizmos.DrawGUITexture(screenRect, tex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment