방법 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;
}
'유니티 > 프로그래밍' 카테고리의 다른 글
[Unity] AudioSource 사운드 재생 갯수 제한두기 (0) | 2019.07.11 |
---|---|
[Unity] 파티클 타겟지점으로 움직이게하기 (0) | 2019.06.26 |
[Unity] 카메라흔들기 (3인칭) (0) | 2019.04.08 |
[Unity] Rigidbody.RigidbodyConstraints 코드제어 (0) | 2019.04.04 |
[Unity] NavMeshAgent.SetDestination 호출후 주의사항 (0) | 2019.03.29 |