개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.08.12 04:46

UIArea::onDraw 관련 질문

목록
  • 코딩테스트러버 2시간 전 2025.08.12 04:46 새글
  • 1
    1
제가 현재 Unity에서 UIArea::onDraw 함수를 사용하여 UI를 그리려고 하는데, 발생하는 문제점이 있습니다.

UIArea::onDraw 함수 내에서 Texture2D를 생성하고 이를 UI의 이미지로 사용하려고 하는데, Texture2D가 매 프레임 생성되고 있기 때문에 성능이 매우 저하됩니다.

이 문제를 해결하기 위해 Texture2D를 한 번만 생성하고 재사용하는 방법이 있는지 궁금합니다.

    댓글목록

    profile_image
    나우호스팅  2시간 전



    UIArea::onDraw 함수 내에서 Texture2D를 매 프레임 생성하는 것은 비효율적입니다.

    이 문제를 해결하기 위해 다음 방법을 고려할 수 있습니다.

    1. Texture2D를 한 번만 생성하는 방법 : Texture2D를 생성한 후, UIArea::onDraw 함수 내에서 매 프레임 재사용하는 대신, Texture2D의 내용을 매 프레임 업데이트하는 방법을 사용할 수 있습니다.

    #hostingforum.kr
    csharp
    
    public class MyUI : MonoBehaviour
    
    {
    
        private Texture2D texture;
    
    
    
        private void Start()
    
        {
    
            texture = new Texture2D(1024, 1024);
    
            // Texture2D를 생성한 후, 내용을 업데이트합니다.
    
            for (int x = 0; x < texture.width; x++)
    
            {
    
                for (int y = 0; y < texture.height; y++)
    
                {
    
                    texture.SetPixel(x, y, Color.white);
    
                }
    
            }
    
            texture.Apply();
    
        }
    
    
    
        private void Update()
    
        {
    
            // 매 프레임 Texture2D의 내용을 업데이트합니다.
    
            for (int x = 0; x < texture.width; x++)
    
            {
    
                for (int y = 0; y < texture.height; y++)
    
                {
    
                    texture.SetPixel(x, y, Color.Lerp(Color.white, Color.black, Time.time));
    
                }
    
            }
    
            texture.Apply();
    
        }
    
    
    
        private void OnDrawGizmos()
    
        {
    
            // UIArea::onDraw 함수 내에서 Texture2D를 사용합니다.
    
            Gizmos.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
    
        }
    
    }
    
    


    2. Texture2D를 캐싱하는 방법 : Texture2D를 생성한 후, 캐시를 사용하여 매 프레임 재사용하는 방법을 사용할 수 있습니다.

    #hostingforum.kr
    csharp
    
    public class MyUI : MonoBehaviour
    
    {
    
        private Texture2D texture;
    
        private Dictionary textureCache = new Dictionary();
    
    
    
        private void Start()
    
        {
    
            texture = new Texture2D(1024, 1024);
    
            // Texture2D를 생성한 후, 내용을 업데이트합니다.
    
            for (int x = 0; x < texture.width; x++)
    
            {
    
                for (int y = 0; y < texture.height; y++)
    
                {
    
                    texture.SetPixel(x, y, Color.white);
    
                }
    
            }
    
            texture.Apply();
    
        }
    
    
    
        private void Update()
    
        {
    
            // 매 프레임 Texture2D의 내용을 업데이트합니다.
    
            for (int x = 0; x < texture.width; x++)
    
            {
    
                for (int y = 0; y < texture.height; y++)
    
                {
    
                    texture.SetPixel(x, y, Color.Lerp(Color.white, Color.black, Time.time));
    
                }
    
            }
    
            texture.Apply();
    
        }
    
    
    
        private void OnDrawGizmos()
    
        {
    
            // UIArea::onDraw 함수 내에서 Texture2D를 사용합니다.
    
            Gizmos.DrawTexture(new Rect(0, 0, texture.width, texture.height), texture);
    
        }
    
    
    
        private void OnDestroy()
    
        {
    
            // 캐시를 삭제합니다.
    
            textureCache.Clear();
    
        }
    
    }
    
    


    3. Texture2D를 사용하지 않는 방법 : Texture2D를 사용하지 않고, UI를 직접 그리거나, 다른 방법을 사용하는 방법을 고려할 수 있습니다.

    #hostingforum.kr
    csharp
    
    public class MyUI : MonoBehaviour
    
    {
    
        private void OnDrawGizmos()
    
        {
    
            // UIArea::onDraw 함수 내에서 UI를 직접 그립니다.
    
            Gizmos.color = Color.white;
    
            Gizmos.DrawWireSphere(new Vector3(0, 0, 0), 1f);
    
        }
    
    }
    
    


    이러한 방법 중 하나를 사용하여 Texture2D를 매 프레임 생성하는 문제를 해결할 수 있습니다.

    2025-08-12 04:47

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 43,446건 / 1 페이지

검색

게시물 검색