133 lines
2.7 KiB
C#
133 lines
2.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace EnergyBarToolkit
|
|
{
|
|
public class Utils
|
|
{
|
|
public class TextureReadable : IDisposable
|
|
{
|
|
private readonly bool isReadable;
|
|
|
|
private readonly string assetPath;
|
|
|
|
public TextureReadable(Texture texture)
|
|
{
|
|
DisplayError(texture);
|
|
}
|
|
|
|
private void DisplayError(Texture texture)
|
|
{
|
|
Debug.LogError(string.Concat("Texture ", texture, " must be set as readable!"));
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
|
|
public static Vector4 ComputePadding(Texture2D texture)
|
|
{
|
|
return ComputePadding(texture, new Rect(0f, 0f, texture.width, texture.height));
|
|
}
|
|
|
|
public static Vector4 ComputePadding(Texture2D texture, Rect rect)
|
|
{
|
|
Color32[] pixels = texture.GetPixels32();
|
|
float num = 0f;
|
|
float num2 = 0f;
|
|
float num3 = 0f;
|
|
float num4 = 0f;
|
|
int width = texture.width;
|
|
int num5 = (int)rect.xMin;
|
|
int num6 = (int)rect.yMin;
|
|
int num7 = (int)(rect.xMax - 1f);
|
|
int num8 = (int)(rect.yMax - 1f);
|
|
for (int i = num5; i < num7; i++)
|
|
{
|
|
if (HasVisibleVert(i, pixels, rect, width))
|
|
{
|
|
num = i;
|
|
break;
|
|
}
|
|
}
|
|
for (int j = num5; j < num7; j++)
|
|
{
|
|
int x = num7 - j - 1;
|
|
if (HasVisibleVert(x, pixels, rect, width))
|
|
{
|
|
num2 = j;
|
|
break;
|
|
}
|
|
}
|
|
for (int k = num6; k < num8; k++)
|
|
{
|
|
if (HasVisibleHoriz(k, pixels, rect, width))
|
|
{
|
|
num4 = k;
|
|
break;
|
|
}
|
|
}
|
|
for (int l = num6; l < num8; l++)
|
|
{
|
|
int y = num8 - l - 1;
|
|
if (HasVisibleHoriz(y, pixels, rect, width))
|
|
{
|
|
num3 = l;
|
|
break;
|
|
}
|
|
}
|
|
num = Mathf.Max(0f, num - 2f);
|
|
num4 = Mathf.Max(0f, num4 - 2f);
|
|
num2 = Mathf.Max(0f, num2 - 2f);
|
|
num3 = Mathf.Max(0f, num3 - 2f);
|
|
return new Vector4(num, num4, num2, num3);
|
|
}
|
|
|
|
private static bool HasVisibleHoriz(int y, Color32[] colors, Rect rect, int stride)
|
|
{
|
|
return HasVisible((int)rect.xMin, y, (int)rect.xMax - 1, y, colors, stride);
|
|
}
|
|
|
|
private static bool HasVisibleVert(int x, Color32[] colors, Rect rect, int stride)
|
|
{
|
|
return HasVisible(x, (int)rect.yMin, x, (int)rect.yMax - 1, colors, stride);
|
|
}
|
|
|
|
private static bool HasVisible(int minX, int minY, int maxX, int maxY, Color32[] colors, int stride)
|
|
{
|
|
for (int i = minY; i <= maxY; i++)
|
|
{
|
|
for (int j = minX; j <= maxX; j++)
|
|
{
|
|
int num = Index(j, i, stride);
|
|
if (num >= colors.Length)
|
|
{
|
|
Debug.LogError("out of range " + num + ", size " + colors.Length);
|
|
return false;
|
|
}
|
|
Color32 color = colors[num];
|
|
if (color.a != 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static int Index(int x, int y, int w)
|
|
{
|
|
return y * w + x;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
}
|
|
}
|
|
}
|