mirror of
https://github.com/tym1116/BH3.git
synced 2025-12-16 08:25:20 +01:00
43 lines
905 B
C#
43 lines
905 B
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace MoleMole
|
|
{
|
|
[ExecuteInEditMode]
|
|
public class MonoImageFitter : MonoBehaviour
|
|
{
|
|
public Image image;
|
|
|
|
public bool fitX = true;
|
|
|
|
public bool fitY;
|
|
|
|
public void Start()
|
|
{
|
|
FitImageSize();
|
|
}
|
|
|
|
public void FitImageSize()
|
|
{
|
|
if (!(image == null) && !(image.sprite == null))
|
|
{
|
|
float num = image.sprite.rect.width;
|
|
float num2 = image.sprite.rect.height;
|
|
Rect rect = GetComponent<RectTransform>().rect;
|
|
if (fitX && num > rect.width)
|
|
{
|
|
num2 *= rect.width / num;
|
|
num = rect.width;
|
|
}
|
|
if (fitY && num2 > rect.height)
|
|
{
|
|
num *= rect.height / num2;
|
|
num2 = rect.height;
|
|
}
|
|
image.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, num);
|
|
image.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, num2);
|
|
}
|
|
}
|
|
}
|
|
}
|