UIUtilities.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.Graphing.Util
  7. {
  8. static class UIUtilities
  9. {
  10. public static bool ItemsReferenceEquals<T>(this IList<T> first, IList<T> second)
  11. {
  12. if (first.Count != second.Count)
  13. {
  14. return false;
  15. }
  16. for (int i = 0; i < first.Count; i++)
  17. {
  18. if (!ReferenceEquals(first[i], second[i]))
  19. {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. public static int GetHashCode(params object[] objects)
  26. {
  27. return GetHashCode(objects.AsEnumerable());
  28. }
  29. public static int GetHashCode<T>(IEnumerable<T> objects)
  30. {
  31. var hashCode = 17;
  32. foreach (var @object in objects)
  33. {
  34. hashCode = hashCode * 31 + (@object == null ? 79 : @object.GetHashCode());
  35. }
  36. return hashCode;
  37. }
  38. public static IEnumerable<T> ToEnumerable<T>(this T item)
  39. {
  40. yield return item;
  41. }
  42. public static void Add<T>(this VisualElement visualElement, T elementToAdd, Action<T> action)
  43. where T : VisualElement
  44. {
  45. visualElement.Add(elementToAdd);
  46. action(elementToAdd);
  47. }
  48. public static IEnumerable<Type> GetTypesOrNothing(this Assembly assembly)
  49. {
  50. try
  51. {
  52. return assembly.GetTypes();
  53. }
  54. catch
  55. {
  56. return Enumerable.Empty<Type>();
  57. }
  58. }
  59. }
  60. }