Answers for "unity show dictionary in inspector"

C#
0

unity show dictionary in inspector

/*
* Unity cannot serialize dictionaries by default. Make a struct/class 
* that contains a key and a value, and expose a list of those objects. 
* Then populate your dictionary from that list in Awake() or Start():
*/

[Serializable]
public class KeyValuePair {
  public MyKeyType key;
  public MyValueType val;
}
 
public List<KeyValuePair> MyList = new List<KeyValuePair>();
Dictionary<MyKeyType, MyValueType> myDict = new Dictionary<MyKeyType, MyValueType>();
 
void Awake() {
  foreach (var kvp in MyList) {
    myDict[kvp.key] = kvp.val;
  }
}
Posted by: Guest on August-11-2021

Code answers related to "unity show dictionary in inspector"

C# Answers by Framework

Browse Popular Code Answers by Language