Last active
January 21, 2016 01:34
-
-
Save nkjzm/f4329b8b22255b6fd76d to your computer and use it in GitHub Desktop.
Unityでアトラス化したSpriteをResoucesから読み込んで取得するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
詳細はここ
Unityでアトラス化したSpriteをResoucesから読み込んで取得するスクリプト - プログラミングで世界を変える