스프라이트 아틀라스
유니티 프로젝트에서 텍스처를 사용하려면 드로우 콜이 발행한다
많은 텍스처를 사용 할수록 드로우 콜이 많이 발행돼서
프로젝트의 성능이 저하될 수 있다
이런 일을 방지하기 위해 스프라이트 아틀라스를 사용한다
스프라이트 아틀라스는 여러 개의 텍스처를 단일 텍스처로 결합하는 에셋이다
따라서 하나의 드로우 콜만 발행해서 큰 성능 저하가 일어나지 않는다
만들기
Atlas 폴더 만들고 Create 2D Sprite Atlas (이름 정하기)
Objects for Packing에 아틀라스로 만들 텍스처 끌어 넣기 or
오른쪽 위 자물쇠 누르고 아틀라스로 만들 텍스처 선택해서 끌어 넣기
(넣고 자물쇠 풀어야 함)
Packing-AllowRotation 해제, TightPacking 해제, Padding 8
Pack Preview
적용하기
Create Empty AtlasTestMain + AtlasTestMain(Script)
ImgChest에 imgChest(이미지 띄울 곳) 넣기
srcTextures에 띄울 이미지들(Texture2D) 넣기
Atlas에 위에서 만든 Atlas 넣기
클릭한 버튼에 따라 아틀라스 이미지 띄우는 코드
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class AtlasTestMain : MonoBehaviour
{
public Button[] arrBtns;
public Image imgChest; //변경 되어야 할 타겟 이미지
public Texture2D[] srcTextures; //소스 이미지
public SpriteAtlas atlas;
void Start()
{
for(int i = 0; i < this.arrBtns.Length; i++)
{
var btn = this.arrBtns[i];
int idx = i; //변수 캡처
btn.onClick.AddListener(() => {
//클로저(람다)
//Debug.LogFormat("{0},{1}",i,idx);
Texture2D texture = this.srcTextures[idx];
//이름으로 가져오기
var spriteName = string.Format("shop_img_chest_close_m_0{0}", idx);
var sprite = this.atlas.GetSprite(spriteName);
//Sprite 생성(아틀라스 사용 안 할 때)
//Sprite sprite =
//Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
this.imgChest.sprite = sprite;
this.imgChest.SetNativeSize();
});
}
}
}
참고
https://docs.unity3d.com/kr/2021.3/Manual/class-SpriteAtlas.html
스프라이트 아틀라스 - Unity 매뉴얼
2D 프로젝트는 스프라이트와 다른 그래픽스를 사용하여 씬의 시각적 요소를 만듭니다. 따라서 단일 프로젝트에 다수의 텍스처 파일이 들어 있을 수 있습니다. 일반적으로 Unity는 씬의 각 텍스처
docs.unity3d.com
'Unity > 기타' 카테고리의 다른 글
스크린샷 기능 구현 (0) | 2023.03.15 |
---|---|
CardboardVR (0) | 2023.03.05 |
프리팹(prefab) 개념 및 응용/제너레이터 스크립트 (0) | 2023.01.31 |
게임 오브젝트/컴포넌트/인스턴스 생성/애트리뷰트 (0) | 2023.01.30 |
디자인 패턴-singleton (0) | 2023.01.16 |