从精灵图集中获取单个精灵 [英] Get single sprite from sprite atlas

查看:40
本文介绍了从精灵图集中获取单个精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 SpriteRenderer 组件中获取 GameObject 的单个精灵.不幸的是,这段代码返回了整个图集,但我需要这个图集的一部分.

Texture2D 缩略图 = GetComponent().sprite.texture;

解决方案

没有从 SpriteRenderer 获取单个 sprite 的原生 API,也没有按名称访问单个 Sprite 的 API.您可以为此功能投票

对于上面的示例图片,tile"是基础图片,ballbottompeople, 和 wallframe 是地图集中的精灵.

void Start(){AtlasLoader atlasLoader = new AtlasLoader("tiles");Debug.Log("地图集计数:" + atlasLoader.atlasCount());精灵球 = atlasLoader.getAtlas("ball");Sprite bottom = atlasLoader.getAtlas("bottom");Sprite people = atlasLoader.getAtlas("people");Sprite wallframe = atlasLoader.getAtlas("wallframe");}

I'd like to get a single sprite which GameObject has in a SpriteRenderer component. Unfortunately, this code returns the whole atlas, but I need to a part of this atlas.

Texture2D thumbnail = GetComponent<SpriteRenderer>().sprite.texture;

解决方案

There is no native API to get single sprite from the SpriteRenderer and no API to access individual Sprite by name. You can vote for this feature here.

You can make your own API to get single sprite from Atlas located in the Resources folder like the image included in your question.

You can load all the sprites from the Atlas with Resources.LoadAll then store them in a dictionary.A simple function can then be used to access each sprite with the provided name.

A simple Atlas Loader script:

public class AtlasLoader
{
    public Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();

    //Creates new Instance only, Manually call the loadSprite function later on 
    public AtlasLoader()
    {

    }

    //Creates new Instance and Loads the provided sprites
    public AtlasLoader(string spriteBaseName)
    {
        loadSprite(spriteBaseName);
    }

    //Loads the provided sprites
    public void loadSprite(string spriteBaseName)
    {
        Sprite[] allSprites = Resources.LoadAll<Sprite>(spriteBaseName);
        if (allSprites == null || allSprites.Length <= 0)
        {
            Debug.LogError("The Provided Base-Atlas Sprite `" + spriteBaseName + "` does not exist!");
            return;
        }

        for (int i = 0; i < allSprites.Length; i++)
        {
            spriteDic.Add(allSprites[i].name, allSprites[i]);
        }
    }

    //Get the provided atlas from the loaded sprites
    public Sprite getAtlas(string atlasName)
    {
        Sprite tempSprite;

        if (!spriteDic.TryGetValue(atlasName, out tempSprite))
        {
            Debug.LogError("The Provided atlas `" + atlasName + "` does not exist!");
            return null;
        }
        return tempSprite;
    }

    //Returns number of sprites in the Atlas
    public int atlasCount()
    {
        return spriteDic.Count;
    }
}

Usage:

With the Example image above, "tile" is the base image and ball, bottom, people, and wallframe are the sprites in the atlas.

void Start()
{
    AtlasLoader atlasLoader = new AtlasLoader("tiles");

    Debug.Log("Atlas Count: " + atlasLoader.atlasCount());

    Sprite ball = atlasLoader.getAtlas("ball");
    Sprite bottom = atlasLoader.getAtlas("bottom");
    Sprite people = atlasLoader.getAtlas("people");
    Sprite wallframe = atlasLoader.getAtlas("wallframe");
}

这篇关于从精灵图集中获取单个精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆