微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Unity之SpriteAtlas

一 , 制作一个SpriteAtlas对象

       1, 将所有要打入目标图集的图片设置为 "Sprite (2D and UI)" , 如下图所示

  2, 新建一个SpriteAtlas对象

     

3, 在Objects for Packing 添加图片 (注意是sprite类型的), 如下图

4, 制作成功,如下图所示

二, 使用图集

1, 代码 , 先上

using UnityEditor;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
/// <summary>
/// 对于Unity图集的测试
/// </summary>
public class AtlasDemo : MonoBehavIoUr
{
    [SerializeReference]
    private SpriteRenderer starRender;//sprite的渲染
    [SerializeReference]
    private Image imgPopularity;//Image的渲染
    // Start is called before the first frame update
    void Start()
    {
        SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>("Assets/Res/OffPrintComeback/OffPrintComeback.spriteatlas");
        #region 关于2D的Sprite
        Sprite sprite = atlas.GetSprite("star");
        this.starRender.sprite = sprite;
        #endregion

        #region 关于UI的Image
        Sprite img = atlas.GetSprite("人气");
        this.imgPopularity.sprite = img;
        this.imgPopularity.SetNativeSize();
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

2, scene介绍

代码既是分别对Sp 和 Image 赋值图集中的纹理

 

三 , 运行结果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐