LoginSignup
5
5

More than 5 years have passed since last update.

UGUI multiple設定で分割した画像を動的に切り替える

Last updated at Posted at 2015-03-04

対象

 ・ケットシーウェアさんの簡易SpritePackerを使うかた  http://caitsithware.com/wordpress/archives/263
 ・spriteModeをmultipleにして1枚の画像で複数の表示をしたい方 

準備

 ・Resourcesフォルダの作成
 ・multiple設定で分割した画像を上記フォルダに入れます

0001.png

※分割方法は省きます。気が向いたら追記します

読み込む

  ・複数割り当てられた1枚画像を1つ1つ分解して読み込んでる
  ・↑それで読み込んだやつをDictionalyで管理
  ・対象画像は必ずResuorseフォルダ以下に置くこと

    static Dictionary<string, Dictionary<string, Sprite>> spriteMstDic 
        = new Dictionary<string, Dictionary<string, Sprite>>();



    //読み込み;
    static void loadSprite(string atlasName)
    {
        if(spriteMstDic.ContainsKey(atlasName))return;

        Sprite[] s = Resources.LoadAll<Sprite>(atlasName);
        int len = s.Length;        
        if(len > 0){

            Dictionary<string , Sprite> sDic = new Dictionary<string,Sprite>();

            for( int i = 0 ; i < len ; i++){
                sDic[s[i].name] = s[i];
            }
            spriteMstDic[atlasName] = sDic;

            Debug.Log(atlasName + "アトラスを読み込みました");
        }

使う

 ・アトラス名と分割した名前を投げてほしい画像を取得する;
 ・例:アトラス名:spriteAtlas スプライト名:texture001 ←ファイル名が混同してわかりにくいと思います すみません

    static Sprite getSprite( string atlasName , string spriteName)
    {
        //既に登録されているならそのまま返す;
        if (spriteMstDic.ContainsKey(atlasName))
        {
            Dictionary<string , Sprite> sDic = spriteMstDic[atlasName];
            if(sDic.ContainsKey(spriteName)){
                return sDic[spriteName];
            }else{
                Debug.Log(atlasName + "に" + spriteName + "は含まれていません");
                return null;
            }
        }
        else
        {
            // 読み込んでおく
            loadAtlas(atlasName);
            //再度呼び出してみる;
            return getSprite(atlasName , spriteName);
        }
    }


5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5