UGUI图集

Edit====》Project  Setting=====》Editor    

或者Window====》2D====》Sprite packer

图集的包

 

Unity2017生成图集文件是以PackingTag标签生成的

       和         Argb和RGB的一块打图集会分成两个图集来打

 

打assetbundle包的时候以文件夹为单位,

可以查看图集

 

 

 

AB包加载资源

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using XLua;

[LuaCallCSharp]
public class AssetBundleMgr : Singleton<AssetBundleMgr>
{
    private AssetBundleManifest m_Manifest; //依赖文件配置

    /// <summary>
    /// 加载依赖文件配置
    /// </summary>
    private void LoadManifestBundle()
    {
        if (m_Manifest != null)
        {
            return;
        }

        string assetName = string.Empty;
#if UNITY_STANDALONE_WIN
        assetName = "Windows";
#elif UNITY_ANDROID
        assetName = "Android";
#elif UNITY_IPHONE
        assetName = "iOS";
#endif

        using (AssetBundleLoader loader = new AssetBundleLoader(assetName))
        {
            m_Manifest = loader.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        }
        //AppDebug.Log("加载依赖文件配置 完毕");
    }

    /// <summary>
    /// 加载镜像
    /// </summary>
    /// <param name="path"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public GameObject Load(string path, string name)
    {
#if DISABLE_ASSETBUNDLE && UNITY_EDITOR
        return UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(string.Format("Assets/{0}", path.Replace("assetbundle", "prefab")));
#else
        using (AssetBundleLoader loader = new AssetBundleLoader(path))
        {
            return loader.LoadAsset<GameObject>(name);
        }
#endif
    }

    /// <summary>
    /// 所有加载的Asset资源镜像
    /// </summary>
    private Dictionary<string, Object> m_AssetDic = new Dictionary<string, Object>();

    /// <summary>
    /// 依赖项的列表
    /// </summary>
    private Dictionary<string, AssetBundleLoader> m_AssetBundleDic = new Dictionary<string, AssetBundleLoader>();

    public void LoadOrDownloadForLua(string path, string name, XLuaCustomExport.OnCreate OnCreate)
    {
        LoadOrDownload<GameObject>(path, name, null, OnCreate: OnCreate, type: 0);
    }

    public void LoadOrDownload<T>(string path, string name, System.Action<T> onComplete, XLuaCustomExport.OnCreate OnCreate = null, byte type = 0) where T : Object
    {
        lock (this)
        {
#if DISABLE_ASSETBUNDLE

            string newPath = string.Empty;
            switch (type)
            {
                case 0:
                    newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "prefab"));
                    break;
                case 1:
                    newPath = string.Format("Assets/{0}", path.Replace("assetbundle", "png"));
                    break;
            }

            if (onComplete != null)
            {
                Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(newPath);
                onComplete(obj as T);
            }
            if (OnCreate != null)
            {
                Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(newPath);
                OnCreate(obj as GameObject);
            }
#else
            //1.加载依赖文件配置
            LoadManifestBundle();

            //2.加载依赖项开始
            string[] arrDps = m_Manifest.GetAllDependencies(path);
            //先检查所有依赖项 是否已经下载 没下载的就下载
            CheckDps(0, arrDps, () =>
            {
                //=============下载主资源开始===================
                string fullPath = (LocalFileMgr.Instance.LocalFilePath + path).ToLower();

                //AppDebug.Log("fullPath=" + fullPath);

            #region 下载或者加载主资源
                if (!File.Exists(fullPath))
                {
            #region 如果文件不存在 需要下载
                    DownloadDataEntity entity = DownloadMgr.Instance.GetServerData(path);
                    if (entity != null)
                    {
                        AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity,
                            (bool isSuccess) =>
                            {
                                if (isSuccess)
                                {

                                    if (m_AssetDic.ContainsKey(fullPath))
                                    {
                                        if (onComplete != null)
                                        {
                                            onComplete(m_AssetDic[fullPath] as T);
                                        }
                                        return;
                                    }

                                    for (int i = 0; i < arrDps.Length; i++)
                                    {
                                        if (!m_AssetDic.ContainsKey((LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()))
                                        {
                                            AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]);
                                            Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i]));
                                            m_AssetBundleDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader;
                                            m_AssetDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj;
                                        }
                                    }

                                    //直接加载
                                    using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true))
                                    {
                                        if (onComplete != null)
                                        {
                                            Object obj = loader.LoadAsset<T>(name);
                                            m_AssetDic[fullPath] = obj;
                                            //进行回调
                                            onComplete(obj as T);
                                        }

                                        //todu 进行xlua的回调
                                    }
                                }
                            }));
                    }
            #endregion
                }
                else
                {
                    if (m_AssetDic.ContainsKey(fullPath))
                    {
                        if (onComplete != null)
                        {
                            onComplete(m_AssetDic[fullPath] as T);
                        }
                        return;
                    }

                    //===================================
                    for (int i = 0; i < arrDps.Length; i++)
                    {
                        if (!m_AssetDic.ContainsKey((LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()))
                        {
                            AssetBundleLoader loader = new AssetBundleLoader(arrDps[i]);
                            Object obj = loader.LoadAsset(GameUtil.GetFileName(arrDps[i]));
                            m_AssetBundleDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = loader;
                            m_AssetDic[(LocalFileMgr.Instance.LocalFilePath + arrDps[i]).ToLower()] = obj;
                        }
                    }
                    //===================================
                    //直接加载
                    using (AssetBundleLoader loader = new AssetBundleLoader(fullPath, isFullPath: true))
                    {
                        if (onComplete != null)
                        {
                            Object obj = loader.LoadAsset<T>(name);
                            m_AssetDic[fullPath] = obj;
                            //进行回调
                            onComplete(obj as T);
                        }

                        //todu 进行xlua的回调
                    }
                }
            #endregion

                //=============下载主资源结束===================
            });
#endif
        }
    }

    /// <summary>
    /// 检查依赖项
    /// </summary>
    /// <param name="index"></param>
    /// <param name="arrDps"></param>
    /// <param name="onComplete"></param>
    private void CheckDps(int index, string[] arrDps, System.Action onComplete)
    {
        lock (this)
        {
            if (arrDps == null || arrDps.Length == 0)
            {
                if (onComplete != null) onComplete();
                return;
            }

            string fullPath = LocalFileMgr.Instance.LocalFilePath + arrDps[index];

            if (!File.Exists(fullPath))
            {
                //如果文件不存在 需要下载
                DownloadDataEntity entity = DownloadMgr.Instance.GetServerData(arrDps[index]);
                if (entity != null)
                {
                    AssetBundleDownload.Instance.StartCoroutine(AssetBundleDownload.Instance.DownloadData(entity,
                        (bool isSuccess) =>
                        {
                            index++;
                            if (index == arrDps.Length)
                            {
                                if (onComplete != null) onComplete();
                                return;
                            }

                            CheckDps(index, arrDps, onComplete);
                        }));
                }
            }
            else
            {
                index++;
                if (index == arrDps.Length)
                {
                    if (onComplete != null) onComplete();
                    return;
                }

                CheckDps(index, arrDps, onComplete);
            }
        }
    }

    /// <summary>
    /// 加载或者下载资源
    /// </summary>
    /// <param name="path">短路径</param>
    /// <param name="name"></param>
    /// <param name="onComplete"></param>
    public void LoadOrDownload(string path, string name, System.Action<GameObject> onComplete)
    {
        LoadOrDownload<GameObject>(path, name, onComplete, type: 0);
    }

    /// <summary>
    /// 卸载依赖项
    /// </summary>
    public void UnloadDpsAssetBundle()
    {
        foreach (var item in m_AssetBundleDic)
        {
            item.Value.Dispose();
        }
        m_AssetBundleDic.Clear();

        m_AssetDic.Clear();
    }

    /// <summary>
    /// 加载克隆
    /// </summary>
    /// <param name="path"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public GameObject LoadClone(string path, string name)
    {
#if DISABLE_ASSETBUNDLE
        GameObject obj = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(string.Format("Assets/{0}", path.Replace("assetbundle", "prefab")));
        return Object.Instantiate(obj);
#else
        using (AssetBundleLoader loader = new AssetBundleLoader(path))
        {
            GameObject obj = loader.LoadAsset<GameObject>(name);
            return Object.Instantiate(obj);
        }
#endif
    }

    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <param name="path"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public AssetBundleLoaderAsync LoadAsync(string path, string name)
    {
        GameObject obj = new GameObject("AssetBundleLoadAsync");
        AssetBundleLoaderAsync async = obj.GetOrCreatComponent<AssetBundleLoaderAsync>();
        async.Init(path, name);
        return async;
    }
}

加载图片

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

/// <summary>
/// 自动加载图片
/// </summary>
public class AutoLoadTexture : MonoBehaviour
{
    /// <summary>
    /// 图片名称
    /// </summary>
    public string ImgName;

    /// <summary>
    /// 图片路径
    /// </summary>
    public string ImgPath;

    /// <summary>
    /// 是否设置图片原本大小
    /// </summary>
    public bool IsSetNativeSize;

    void Start()
    {
    }

    public void SetImg()
    {
        Image img = GetComponent<Image>();

        if (img != null && !string.IsNullOrEmpty(ImgPath))
        {
            AssetBundleMgr.Instance.LoadOrDownload<Object>(ImgPath, ImgName, (UnityEngine.Object asset, object userData) =>
            {
                Sprite obj = null;
                if (asset is Sprite)
                {
                    obj = (Sprite)asset;
                }
                else if (asset is Texture2D)
                {
                    Texture2D texture = asset as Texture2D;
                    obj = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
                }

                if (img == null) return;

                img.overrideSprite = obj;
                if (IsSetNativeSize)
                {
                    img.SetNativeSize();
                }
            }, type: 1);
        }
    }
}

图片名,图集路径

替换图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值