Unity

Sprite Atlas

공부하기 좋은 날 2023. 2. 9. 15:21

sprite atlas

: 여러개의 텍스쳐를 하나의 텍스쳐로 합쳐 UV좌표를 설정하여 여러 텍스쳐가 하나를 사용할 수 있도록 하는 최적화 기법

draw call(한번에 그리는 gpu의 로드 양, game뷰-stats-batch로 확인)을 줄이는 방법

 

 

sprite atlas사용 전, batches= 29

 

sprite packer 설정 - always enabled로 변경
2021버전에서는 package manager에서 2d sprite다운, 2018버전은 다운안받아도 됨

 

sprite atlas를 create
만들어진 sprite atlas 에 사진, 폴더를 넣고 pack preview를 하면 여러 sprite가 하나로 합쳐진 sprite atlas가 만들어짐

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
//sprite atlas : drawcall을 줄일 수 있도록 여러개의 texture를 하나의 texture로 만듬
//플레이시 게임뷰 stats의 batch를 보면 아틀라스 사용시 드로우 콜이 적어지는 것을 확인할 수 있다.
//스프라이트 아틀라스 기능은 2d 프로젝트에 주로 사용되며 밑의 코드처럼 아틀라스를 스프라이트에 직접 할당해 준 후 적용이 된다.


public class SpriteAtlasApply : MonoBehaviour
{
    [SerializeField]SpriteAtlas atlas;
    public string spriteName;    

    void Start()
    {
        spriteName = gameObject.GetComponent<Image>().sprite.name;
        gameObject.GetComponent<Image>().sprite = atlas.GetSprite(spriteName);
    }

}

스크립트로 이름으로 접근하여 sprite atlas 를 sprite에서 적용

sprite atlas 적용 후, batches 8

batches가 8이 되었지만 스프라이트가 너무 타이트하게 잘려서 한 칸에 다른 이미지가 같이 나오고 제멋대로 회전하는 현상이 생김

 

Allow Rotation, Tight Packing 설정하여 해결