[Unity] Unity 2017 – Sprite Atlas



Overview

1. Create => Sprite Atlas
2. Editor Settings => Sprite Packer Mode = Always Enabled
3. SpriteAtlas GetSprite API


Simple Sprite Atlas Loader

1. Create => Sprite Atlas

建立完成後可以直接拖曳單張或整個資料夾進去

Sprite Atlas

2. Editor Settings => Sprite Packer Mode = Always Enabled

如果要預覽打包結果的話,要去 Editor Settings 下把設定打開

Sprite Atlas

3. SpriteAtlas GetSprite API

利用 GetSprites 取出來的順序會是依照打包順序
若是順序不如預期就需要再依名稱排序一下

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.U2D;

public class SpritePackerLoader : MonoBehaviour
{
    [SerializeField]
    private SpriteAtlas m_atlas;

    [SerializeField]
    private SpriteRenderer m_renderer;

    private List<Sprite> m_sprites;

    void Start()
    {
        var sprites = new Sprite[m_atlas.spriteCount];
        m_atlas.GetSprites(sprites);
        m_sprites = sprites.ToList();

        m_renderer.sprite = m_sprites.First();
    }

    void OnGUI()
    {
        if (GUILayout.Button("Next Sprite"))
        {
            m_renderer.sprite = m_sprites.CircularNext(m_renderer.sprite);
        }
    }
}

需要 ContainerExtension 的朋友請至這裡:
https://goo.gl/ynpFXH




歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: