HeavenStudioPlus/Assets/Plugins/Starpelly/Utils.cs

57 lines
1.9 KiB
C#
Raw Normal View History

2021-12-19 04:10:43 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Starpelly
{
public static class Utils
{
static Texture2D _whiteTexture;
public static Texture2D WhiteTexture
{
get
{
if (_whiteTexture == null)
{
_whiteTexture = new Texture2D(1, 1);
_whiteTexture.SetPixel(0, 0, Color.white);
_whiteTexture.Apply();
}
return _whiteTexture;
}
}
public static void DrawScreenRect(Rect rect, Color color)
{
GUI.color = color;
GUI.DrawTexture(rect, WhiteTexture);
GUI.color = Color.white;
}
public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
{
// Top
Utils.DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
// Left
Utils.DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
// Right
Utils.DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
// Bottom
Utils.DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
}
public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
{
// Move origin from bottom left to top left
screenPosition1.y = Screen.height - screenPosition1.y;
screenPosition2.y = Screen.height - screenPosition2.y;
// Calculate corners
var topLeft = Vector3.Min(screenPosition1, screenPosition2);
var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
// Create Rect
return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
}
}