본문 바로가기
개발/Unity

Unity) Texture2D Resize Code (텍스쳐 2D 사이즈 변경)

by 테샤르 2023. 5. 10.

Texture2D Resize Code (텍스쳐 2D 사이즈 변경)

 

Texture 2D의 Size를 다시 처리하는 코드이다.

해당 코드는 원본 (source) 을 기준으로 새로운 Texture2D를 생성해서 사이즈를 변경한다.

 

< 샘플 코드 >

private Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight)
{
            RenderTexture rt = new RenderTexture(newWidth, newHeight, 0);
            Graphics.Blit(source, rt);
            Texture2D newTexture = new Texture2D(newWidth, newHeight);
            newTexture.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0);
            newTexture.Apply();
            RenderTexture.active = null;
            rt.Release();
            return newTexture;
}

 

반응형

 

 

Texture2D.Resize가 Obsolete으로 실제 Unity 내부 소스에는 다음과 같이 처리가되어있다.

Unity-Technologies/UnityCsReferences  Texture.cs : [링크]

 [Obsolete("Texture2D.Resize(int, int) has been deprecated because it actually reinitializes the texture. Use Texture2D.Reinitialize(int, int) instead (UnityUpgradable) -> Reinitialize([*] System.Int32, [*] System.Int32)", false)]
        public bool Resize(int width, int height)
        {
            return Reinitialize(width, height);
        }

 

 

Unity Texture2D.Resize : [링크]

 

Unity - Scripting API: Texture2D.Resize

Changes size of texture to width by height, format to textureFormat and optionally creates mipmaps. After resizing, texture pixels will be undefined. This function is very similar to the texture constructor, except it works on existing texture object. Call

docs.unity3d.com

 

Unity Texture2D.Reinitalize : [링크]

 

Unity - Scripting API: Texture2D.Reinitialize

Description Reinitializes a Texture2D, making it possible for you to replace width, height, textureformat, and graphicsformat data for that texture. This action also clears the pixel data associated with the texture from the CPU and GPU. This function is v

docs.unity3d.com

 

★☆☆☆☆

 

반응형

댓글