블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-28 00:03

유니티 (Unity)에서 멀티 키 딕셔너리 (Multi Key Dictionary)를 사용하는 방법에 대해 검색 결과를 제공해드리겠습니다.

멀티 키 딕셔너리는 두 개 이상의 키로 값을 저장하고 검색할 수 있는 자료구조입니다. 유니티에서는 기본적으로 이러한 기능을 제공하지 않지만, 다음과 같은 방법을 사용하여 멀티 키 딕셔너리를 구현할 수 있습니다:

Tuple을 사용한 멀티 키 딕셔너리 구현: Tuple을 이용하여 여러 키를 하나의 키로 묶고, 해당 키에 대한 값을 딕셔너리에 저장하는 방식입니다. 이렇게 하면 여러 키로 값을 조회하거나 저장할 수 있습니다.

using System;
using System.Collections.Generic;

public class MultiKeyDictionary<TKeyTuple, TValue>
{
    private Dictionary<TKeyTuple, TValue> dictionary = new Dictionary<TKeyTuple, TValue>();

    public void Add(TKeyTuple keys, TValue value)
    {
        dictionary[keys] = value;
    }

    public bool TryGetValue(TKeyTuple keys, out TValue value)
    {
        return dictionary.TryGetValue(keys, out value);
    }
}

// 사용 예시
var multiKeyDict = new MultiKeyDictionary<(int, string), int>();
multiKeyDict.Add((1, "key1"), 100);
multiKeyDict.Add((2, "key2"), 200);

if (multiKeyDict.TryGetValue((1, "key1"), out int result))
{
    Debug.Log("Value found: " + result);
}


C# 9의 init-only 프로퍼티와 튜플 사용: C# 9부터 init-only 프로퍼티를 사용하여 딕셔너리의 값을 읽기 전용으로 설정하고, 튜플을 활용하여 멀티 키를 표현할 수 있습니다.

using System;
using System.Collections.Generic;

public class MultiKeyDictionary<TValue>
{
    private Dictionary<(int, string), TValue> dictionary = new Dictionary<(int, string), TValue>();

    public void Add(int key1, string key2, TValue value)
    {
        dictionary[(key1, key2)] = value;
    }

    public IReadOnlyDictionary<(int, string), TValue> Dictionary => dictionary;
}

// 사용 예시
var multiKeyDict = new MultiKeyDictionary<int>();
multiKeyDict.Add(1, "key1", 100);
multiKeyDict.Add(2, "key2", 200);

if (multiKeyDict.Dictionary.TryGetValue((1, "key1"), out int result))
{
    Debug.Log("Value found: " + result);
}

이렇게 유니티에서 멀티 키 딕셔너리를 구현할 수 있습니다. 코드는 예시일 뿐이며, 실제 프로젝트에서 적절한 방식으로 적용하셔야 합니다.

 

[출처] ChatGPT

 

[참조] https://stackoverflow.com/questions/1171812/multi-key-dictionary-in-c

 

Multi-key dictionary in c#?

I know there isn't one in the BCL but can anyone point me to a good opensource one? By Multi I mean 2 keys. ;-)

stackoverflow.com

 

 

반응형
Posted by blueasa
, |