[Unity] Texture2D Resize하기 (크기조정)
방법 1.
// 가로 , 세로 값 지정해서 Resize하기
private Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, true);
Color[] rpixels = result.GetPixels(0);
float incX = (1.0f / (float)targetWidth);
float incY = (1.0f / (float)targetHeight);
for (int px = 0; px < rpixels.Length; px++)
{
rpixels[px] = source.GetPixelBilinear(incX * ((float)px % targetWidth), incY * ((float)Mathf.Floor(px / targetWidth)));
}
result.SetPixels(rpixels, 0);
result.Apply();
return result;
}
방법 2.
// 비율로 해서 Resize하기
public Texture2D ScaleTexture( Texture2D source, float _scaleFactor)
{
if (_scaleFactor == 1f)
{
return source;
}
else if (_scaleFactor == 0f)
{
return Texture2D.blackTexture;
}
int _newWidth = Mathf.RoundToInt(source.width * _scaleFactor);
int _newHeight = Mathf.RoundToInt(source.height * _scaleFactor);
Color[] _scaledTexPixels = new Color[_newWidth * _newHeight];
for (int _yCord = 0; _yCord < _newHeight; _yCord++)
{
float _vCord = _yCord / (_newHeight * 1f);
int _scanLineIndex = _yCord * _newWidth;
for (int _xCord = 0; _xCord < _newWidth; _xCord++)
{
float _uCord = _xCord / (_newWidth * 1f);
_scaledTexPixels[_scanLineIndex + _xCord] = source.GetPixelBilinear(_uCord, _vCord);
}
}
// Create Scaled Texture
Texture2D result = new Texture2D(_newWidth, _newHeight, source.format, false);
result.SetPixels(_scaledTexPixels, 0);
result.Apply();
return result;
}