diff --git a/MrBigsock/Assets/Code/PlayerController.cs b/MrBigsock/Assets/Code/PlayerController.cs index f47089b0b0f93a26fd60f78fb3814b6d2c1f1329..105e032730bb1b605d017de02442534f42bf3144 100644 --- a/MrBigsock/Assets/Code/PlayerController.cs +++ b/MrBigsock/Assets/Code/PlayerController.cs @@ -65,6 +65,7 @@ namespace BigSock { //TryPickUpItem(ItemService.SINGLETON.Get(101)); //var tmp = PrefabService.SINGLETON; + var tmp = SpriteService.SINGLETON; _testAttack = (IAttack) AbilityService.SINGLETON.Get(101); _testAttack2 = (IAttack) AbilityService.SINGLETON.Get(102); diff --git a/MrBigsock/Assets/Code/Services/SpriteService.cs b/MrBigsock/Assets/Code/Services/SpriteService.cs new file mode 100644 index 0000000000000000000000000000000000000000..8ae74750fb8dba2304f271b48ab90b2e39628a03 --- /dev/null +++ b/MrBigsock/Assets/Code/Services/SpriteService.cs @@ -0,0 +1,69 @@ +using System.Collections; +using System; +using System.Linq; +using System.Collections.Generic; +using System.Reflection; + +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEditor; + +using BigSock.Item; + + +namespace BigSock.Service { + + /* + Service for handling sprites. + */ + public partial class SpriteService { + /* + The instance to use. + */ + public static readonly SpriteService SINGLETON = new SpriteService(); + + /* + Get a prefab of a name. + */ + public Sprite Get(string name) { + if(_sprites.TryGetValue(_sanitize(name), out var res)) return res; + return null; + } + + } + + public partial class SpriteService { + private Dictionary<string, Sprite> _sprites = new Dictionary<string, Sprite>(); + + private System.Random _rnd = new System.Random(); + + private SpriteService() { + _loadItems(); + } + + /* + Load the items into the dictionary. + Based on: https://stackoverflow.com/a/67670629 + */ + private void _loadItems() { + string[] guids = AssetDatabase.FindAssets( "t:Sprite", new string[] {"Assets/Sprites"} ); + + var dict = new Dictionary<string, Sprite>(); + + foreach(var guid in guids) { + var path = AssetDatabase.GUIDToAssetPath( guid ); + var name = _sanitize(path.Replace(".png", "").Replace(".jpg", "").Replace("Assets/Sprites/", "")); + Sprite go = AssetDatabase.LoadAssetAtPath<Sprite>( path ); + + Debug.Log($"[SpriteService._loadItems()] {name}"); + dict[name] = go; + } + + _sprites = dict; + } + + private string _sanitize(string name) + => name.Replace(" ", "").Replace("_", "").ToLower(); + + } +} \ No newline at end of file diff --git a/MrBigsock/Assets/Code/Services/SpriteService.cs.meta b/MrBigsock/Assets/Code/Services/SpriteService.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..d0885c43b69b894488dfabd81d6b6c1b43692f10 --- /dev/null +++ b/MrBigsock/Assets/Code/Services/SpriteService.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 884f816930ab2e045b11aaeb06ef8bce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: