Extensions.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Assets.HeroEditor4D.Common.Scripts.Data;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace Assets.HeroEditor4D.Common.Scripts.Common
  8. {
  9. public static class Extensions
  10. {
  11. public static void SetActive(this Component target, bool active)
  12. {
  13. target.gameObject.SetActive(active);
  14. }
  15. public static bool IsEmpty(this string target)
  16. {
  17. return string.IsNullOrEmpty(target);
  18. }
  19. public static void Clear(this Transform transform)
  20. {
  21. foreach (Transform child in transform)
  22. {
  23. Object.Destroy(child.gameObject);
  24. }
  25. }
  26. public static T ToEnum<T>(this string value) where T : Enum
  27. {
  28. if (string.IsNullOrEmpty(value)) return (T) Enum.GetValues(typeof(T)).GetValue(0);
  29. return (T) Enum.Parse(typeof(T), value);
  30. }
  31. public static T Random<T>(this T[] source)
  32. {
  33. return source[UnityEngine.Random.Range(0, source.Length)];
  34. }
  35. public static T Random<T>(this List<T> source)
  36. {
  37. return source[UnityEngine.Random.Range(0, source.Count)];
  38. }
  39. public static T Random<T>(this List<T> source, int seed)
  40. {
  41. UnityEngine.Random.InitState(seed);
  42. return source[UnityEngine.Random.Range(0, source.Count)];
  43. }
  44. public static Sprite FindSprite(this IEnumerable<ItemSprite> list, string id)
  45. {
  46. return list.Single(i => i.Id == id).Sprite;
  47. }
  48. public static List<Sprite> FindSprites(this IEnumerable<ItemSprite> list, string id)
  49. {
  50. return list.Single(i => i.Id == id).Sprites.ToList();
  51. }
  52. }
  53. }