ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1118)
    • 개인 프로젝트 (2)
      • Unity (2)
      • Unreal (0)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (191)
      • Unreal (69)
      • Unity (111)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (242)
      • 공통 (21)
      • 네트워크 (45)
      • OS & 하드웨어 (60)
      • 자료구조 & 알고리즘 (99)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (377)
      • C++ (168)
      • C# (97)
      • Java (11)
      • Python (36)
      • SQL (44)
      • JavaScript (9)
      • React (7)
    • 그 외 (15)
      • Math (7)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 언리얼
  • 함수
  • 파이썬
  • Unity
  • 유니티
  • 문자열
  • string
  • 포인터
  • c#
  • 배열
  • 오블완
  • 티스토리챌린지
  • 알고리즘
  • C++
  • python
  • SQL
  • 클래스
  • 백준
  • 그래픽스
  • C

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

게임엔진/Unity

[Unity] ServiceLocatorManager > 구조 변경 / indexer, Type

2025. 11. 30. 14:57

일단은 서비스 딕셔너리를 생성해준 후 indexer로 Type 값을 반환한다

여기서 instance는 싱글톤 객체를 의미한다

private Dictionary<Type, object> _services = new Dictionary<Type, object>();

public object this[Type type]
{
    get
    {
        if (_services.TryGetValue(type, out object service))
            return service;
        
        Debug.LogWarning($"Service of type {type.Name} not found!");
        return null;
    }
    set
    {
        if (_services.ContainsKey(type))
        {
            Debug.LogWarning($"Service of type {type.Name} already registered. Overwriting...");
            _services[type] = value;
        }
        else
        {
            _services.Add(type, value);
            Debug.Log($"Service of type {type.Name} registered successfully.");
        }
    }
}

 

자주 쓰여지는 것들은 아래 함수들을 활용하면 된다

public static object GetInstance(Type type) =>instance[type];

public static T GetInstance<T>() where T : class => HasInstance<T>() ? instance.Get<T>()  : null;

public static void RegisterInstance<T>(T service) where T : class => instance.Register(service);

public static void UnregisterInstance<T>() where T : class => instance.Unregister<T>();

public static bool HasInstance<T>() where T : class => instance.HasService<T>();

이렇게 활용하면 된다

ServiceLocatorManager.GetInstance<PlayerController>();

 

자주 안쓰여지는것들은 instance. 해도 무방하다

public void Register<T>(T service) where T : class
{
    Type type = typeof(T);
    this[type] = service;
}

public T Get<T>() where T : class
{
    Type type = typeof(T);
    object service = this[type];
    return service as T;
}

public void Unregister<T>() where T : class
{
    Type type = typeof(T);
    if (_services.ContainsKey(type))
    {
        _services.Remove(type);
        Debug.Log($"Service of type {type.Name} unregistered.");
    }
}

public bool HasService<T>() where T : class => _services.ContainsKey(typeof(T));

public void ClearAll()
{
    _services.Clear();
    Debug.Log("All services cleared.");
}
저작자표시 (새창열림)

'게임엔진 > Unity' 카테고리의 다른 글

[Unity] 커스텀 셰이더 UnityCG.cginc 헤더 파일 못찾는 이유  (0) 2025.12.27
[Unity] fps 로그창 GUI  (0) 2025.12.13
[Unity] 애니메이션 마지막 프레임에서 이벤트 호출안되는 이유  (0) 2025.11.23
[Unity] 로그 (Log) 출력시 스택 트레이스 (Stack Trace) 관리하기  (0) 2025.10.07
[Unity] PlayerInput을 활용한 캐릭터 움직이는 방법  (0) 2025.08.04
    '게임엔진/Unity' 카테고리의 다른 글
    • [Unity] 커스텀 셰이더 UnityCG.cginc 헤더 파일 못찾는 이유
    • [Unity] fps 로그창 GUI
    • [Unity] 애니메이션 마지막 프레임에서 이벤트 호출안되는 이유
    • [Unity] 로그 (Log) 출력시 스택 트레이스 (Stack Trace) 관리하기
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바