Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Last active January 21, 2016 01:34
Show Gist options
  • Save nkjzm/f4329b8b22255b6fd76d to your computer and use it in GitHub Desktop.
Save nkjzm/f4329b8b22255b6fd76d to your computer and use it in GitHub Desktop.
Unityでアトラス化したSpriteをResoucesから読み込んで取得するスクリプト
using UnityEngine;
using System.IO;
using System.Collections.Generic;
public class AtlasSprite
{
private Dictionary<string,Sprite> spriteDic;
public AtlasSprite(string path)
{
spriteDic = new Dictionary<string, Sprite> ();
LoadSprite (path);
}
private void LoadSprite(string path)
{
Sprite[] sprites = Resources.LoadAll<Sprite> (path);
foreach (Sprite s in sprites) {
// use string removed ".png"
spriteDic.Add (Path.GetFileNameWithoutExtension(s.name), s);
}
}
public Sprite GetSprite(string imgName)
{
if (!spriteDic.ContainsKey (imgName)) {
Debug.Log ("以下のspriteが見つかりません: "+imgName);
return null;
}
return spriteDic [imgName];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment