유니티 최적화 : parameter ID의 Hash value 캐싱

 

 

animator나 material, shader의 parameter를 호출할 때,

간편하게 string형으로 호출하는 경우가 많다.

 

animator.SetTrigger("Attack");
material.SetTexture("_MainMap", texture);
shader.SetGlobalColor("_MainColor", color);

 

명시성도 좋고 간편하지만, 성능상으로는 매번 문자열에서 Hash로 변환하기때문에 성능상 좋지않다고한다.

아래와같이 멤버변수로 캐싱해놓고 쓰는 것을 추천한다.

 

int id = Animator.StringToHash("Attack");
animator.SetTrigger(id);

int id = Shader.PropertyToID("_MainMap");
material.SetTexture(id, texture);

int id = Shader.PropertyToID("_MainColor");
shader.SetGlobalColor(id, color);

 

아래와같이 바로 멤버변수로 초기화도 가능 

private readonly int MATCAP_HASH = Shader.PropertyToID("_MatCap");

private void Awake()
{
    materialPropertyBlock.SetTexture(MATCAP_HASH, hurtMatcap);
}

 

Animator에서는 StringToHash, 같지만 Shader에서는 PropertyToID 를 헷갈리지 않도록 주의!

댓글

Designed by JB FACTORY