Skip to content
Snippets Groups Projects
Commit d6a7615a authored by Robin Halseth Sandvik's avatar Robin Halseth Sandvik
Browse files

Added SpriteSerivice.

parent a01bf45a
No related branches found
No related tags found
1 merge request!45Alexander
......@@ -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);
......
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
fileFormatVersion: 2
guid: 884f816930ab2e045b11aaeb06ef8bce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment