123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using UnityEngine;
- using System;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- public static class ExtensionDictionary
- {
- public static T2 Random<T1, T2>(this Dictionary<T1, T2> dic, bool remove = false, Func<T2,bool> func = null)
- {
- int antiCrush = 0;
- while (true)
- {
- if (antiCrush++>10000)
- {
- throw new Exception();
- }
- int index = UnityEngine.Random.Range(0, dic.Count);
- T1 resultKey = dic.Keys.ToList()[index];
- T2 resultValue = dic.Values.ToList()[index];
-
- if (func != null)
- {
- if (!func(resultValue))
- {
- continue;
- }
- }
- if (remove)
- {
- dic.Remove(resultKey);
- return resultValue;
- }
- else
- {
- return resultValue;
- }
- }
- }
- public static bool UniqueAdd<T1, T2>(this Dictionary<T1, T2> dic, T1 t1, T2 t2)
- {
- if (dic.ContainsKey(t1))
- {
- return false;
- }
- else
- {
- dic.Add(t1, t2);
- return true;
- }
- }
- }
|