ItemSprite.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Assets.HeroEditor4D.Common.Scripts.Common;
  5. using Newtonsoft.Json;
  6. using UnityEngine;
  7. namespace Assets.HeroEditor4D.Common.Scripts.Data
  8. {
  9. /// <summary>
  10. /// Represents sprite entry in SpriteCollection.
  11. /// </summary>
  12. [Serializable]
  13. public class ItemSprite
  14. {
  15. public string Name;
  16. public string Id;
  17. public string Edition;
  18. public string Collection;
  19. public string Path;
  20. public Sprite Sprite;
  21. public List<Sprite> Sprites;
  22. public List<string> Tags = new List<string>();
  23. public string Meta;
  24. public Dictionary<string, string> MetaDict
  25. {
  26. get => Meta == "" ? new Dictionary<string, string>() : JsonConvert.DeserializeObject<Dictionary<string, string>>(Meta);
  27. set => Meta = JsonConvert.SerializeObject(value);
  28. }
  29. public ItemSprite(string edition, string collection, string type, string name, string path, Sprite sprite, List<Sprite> sprites)
  30. {
  31. Id = $"{edition}.{collection}.{type}.{name}";
  32. if (sprites == null || sprites.Count == 0)
  33. {
  34. throw new Exception($"Please set [Texture Type = Sprite] for [{Id}] from Import Settings!");
  35. }
  36. Name = name;
  37. Collection = collection;
  38. Edition = edition;
  39. Path = path;
  40. Sprite = sprite;
  41. Sprites = sprites.OrderBy(i => i.name).ToList();
  42. }
  43. public Sprite GetSprite(string name)
  44. {
  45. return Sprites.Single(j => j.name == name);
  46. }
  47. }
  48. }