Advertisement
k0fe

UpdateDictionary

Feb 27th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. [Button("Update List")]
  2.     public void UpdateDictionary()
  3.     {
  4.         ref var data = ref GetData(out _);
  5.         var dictionary = data.UpdateAtLevel;
  6.         // Create values
  7.         if(dictionary.Count == 0)
  8.         {
  9.             foreach (Transform child in transform)
  10.             {
  11.                 dictionary.Add(child.GetSiblingIndex(), child.gameObject);
  12.             }
  13.         }
  14.         // Add new values
  15.         else if(transform.childCount > dictionary.Count)
  16.         {
  17.             for (int i = transform.childCount - (transform.childCount - dictionary.Count); i < transform.childCount; i++)
  18.             {
  19.                 dictionary.Add(dictionary.Keys.Last() + 1, transform.GetChild(i).gameObject);
  20.             }
  21.         }
  22.         // Remove null values
  23.         else if(transform.childCount < dictionary.Count)
  24.         {
  25.             var nullValues = new List<int>();
  26.             // get null values
  27.             foreach (var item in dictionary)
  28.             {
  29.                 if(item.Value.Equals(null))
  30.                 {
  31.                     nullValues.Add(item.Key);
  32.                 }
  33.             }
  34.             // remove 'em
  35.             foreach (var item in nullValues)
  36.             {
  37.                 dictionary.Remove(item);
  38.             }
  39.         }
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement