【问题标题】:Get single sprite from sprite atlas从精灵图集中获取单个精灵
【发布时间】:2017-08-10 17:57:15
【问题描述】:

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

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

【问题讨论】:

    标签: c# unity3d 2d textures sprite


    【解决方案1】:

    没有本地 API 可以从 SpriteRenderer 获取单个 sprite,也没有 API 可以通过名称访问单个 Sprite。你可以为这个功能投票here

    您可以创建自己的 API 以从位于 Resources 文件夹中的 Atlas 获取单个精灵,就像您问题中包含的图像一样。

    您可以使用 Resources.LoadAll 从 Atlas 中加载所有精灵,然后将它们存储在字典中。然后可以使用一个简单的函数来访问具有提供的名称的每个 sprite

    一个简单的 Atlas Loader 脚本:

    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;
        }
    }
    

    用法

    对于上面的示例图片,“tile”是基础图片,ballbottompeople , wallframe 是图集中的精灵。

    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");
    }
    

    【讨论】:

      【解决方案2】:

      你可以将你需要的图片单独放在 Resources 文件夹中,然后使用 Resources.Load("spriteName") 来获取它。如果你想得到它作为一个精灵,你会做以下事情:

      Sprite thumbnail = Resources.Load("spriteName", typeof(Sprite)) as Sprite;
      

      来源:https://forum.unity3d.com/threads/how-to-change-sprite-image-from-script.212307/

      【讨论】:

        【解决方案3】:

        使用新的 Unity 版本,您可以使用 SpriteAtlas 类和 GetSprite 方法轻松完成: https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlas.html

        因此,如果您正在使用 Resources 文件夹,您可以这样做:

        Resources.Load<SpriteAtlas>("AtlasName")
        

        【讨论】:

        • 不,这不起作用。请注意,这个问题中的精灵是使用 Unity 的精灵编辑器切割成碎片的单个精灵。此解决方案不像 Håvard Nygård 的 solution 那样工作。
        猜你喜欢
        • 1970-01-01
        • 2022-12-05
        • 1970-01-01
        • 2023-03-15
        • 2011-08-19
        • 1970-01-01
        • 2013-11-29
        • 2019-03-10
        • 1970-01-01
        相关资源
        最近更新 更多