유니티 (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);
}
이렇게 유니티에서 멀티 키 딕셔너리를 구현할 수 있습니다. 코드는 예시일 뿐이며, 실제 프로젝트에서 적절한 방식으로 적용하셔야 합니다.
> Configure project :unityLibrary Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'unityLibrary\build.gradle'
...
[빌드에러 #2]
Execution failed for task ':launcher:checkReleaseDuplicateClasses'. > Could not resolve all files for configuration ':launcher:releaseRuntimeClasspath'.
Also, there is another workaround that keeps the Gradle version (in case some android lib requires it). For that workaround, you will need to copy a file from/Applications/Unity/Hub/Editor/2022.2.3f1/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/settingsTemplate.gradletoAssets/Plugins/Android/settingsTemplate.gradle. That way Unity will use your template inAssets/Plugins/Android. (It's weird that Unity doesn't has a checkbox for that) For my version of unity, it looks like that:
The conflicting part isRepositoriesMode.PREFER_SETTINGS. But you can safely delete the wholedependencyResolutionManagementblock from here. Since the android project still needs to know about local dependencies, copy therepositoriesblock, you will need it later. NowsettingsTemplate.gradleshould look like this: