Unity/기능

[Unity] 전역 using 사용하기

turbulence93 2025. 6. 30. 20:30

1. Constants 구조 설계

  • string이나 Hash 데이터를 정리할 static 클래스를 생성해  AnimatorHash, SFX, BGM 등을 관리
  • using static 구문으로 호출을 간결하게 만들 수 있다는 점 학습

2. global using 문법 실험

  • C# 10 기능 중 하나로, 프로젝트 전체에서 using 문을 한 번만 선언해 사용하는 방식
  • 코드 길이 축소와 중복 제거에 매우 효과적

3. C# 10 적용을 위한 환경 설정

  • Unity 2022.1 이상
  • Visual Studio 2022 이상
  • .NET SDK 6 이상
  • Unity 프로젝트 세팅에서 NetFramwork 2.1 설정

4. 적용

  • 팀 환경과 유지보수를 고려해 global using은 도입 보류
  • global using static Constants.AnimationHash 등으로 처리
public static class Constants
{
	public static class BGM
    {
    	public static readonly string MainBGM = "MainBGM";
    }
    
     public static class AnimatorHash
	 {
    	 public static readonly int LookDirHash = Animator.StringToHash("LookDir");
    	 public static readonly int MoveStateHash = Animator.StringToHash("MoveState");
   	 	 public static readonly int AttackStateHash = Animator.StringToHash("AttackState");
    	 public static readonly int DamagedStateHash = Animator.StringToHash("DamagedState");
  		 public static readonly int DashStateHash = Animator.StringToHash("DashState");
     	 public static readonly int DeathTriggerHash = Animator.StringToHash("DeathTrigger");
    	 public static readonly int DeathStateHash = Animator.StringToHash("DeathState");
    	 public static readonly int IdleStateHash = Animator.StringToHash("IdleState");
 	}
}

//스크립트마다 using 문을 작성하지 않아도 Constants.AnimatorHash 내부 hash값 접근 가능
global using static Constants.AnimatorHash
  • 글로벌로 한번 만 정의해도 다른 cs에서도 hash 내부 요소에 편하게 접근가능