前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AssetBundle打包工具实现

AssetBundle打包工具实现

作者头像
孙寅
发布2020-06-02 11:50:24
7090
发布2020-06-02 11:50:24
举报
文章被收录于专栏:宜达数字宜达数字

什么是AssetBundle,点击查看

先看一下工具的使用

商用为目的的打包工具

同步读取AssetBundle
  • 创建单例
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  class  Singleton<T> where T : class, new()
    {
        private static T _instance;
        private static readonly object syslock = new object();

        public static T Instance()
        {
            if (_instance == null)
            {
                lock (syslock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                    }
                }
            }
            return _instance;
        }
    }
  • 利用单例进行文件读取单例类的创建
代码语言:javascript
复制
using System.IO;
using UnityEngine;

/// <summary>
/// 文件读取
/// </summary>
public class LocalFileMgr : Singleton<LocalFileMgr> {

#if UNITY_EDITOR
 
#if UNITY_STANDALONE_WIN
    public readonly string LocalFilePath = Application.dataPath + "/../AssetBundles/Windows/";
#elif UNITY_ANDROID
    public readonly string LocalFilePath = Application.dataPath + "/../AssetBundles/Windows/Android/";
#elif UNITY_IPHONE
    public readonly string LocalFilePath = Application.dataPath + "/../AssetBundles/Windows/iOS/";
#endif
#elif UNITY_ANDROID || UNITY_IPHONE || UNITY_STANDALONE_WIN
    public readonly string LocalFilePath = Application.persistentDataPath + "/";
#endif
    /// <summary>
    /// 读取本地文件到byte数组
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>

    public byte[] GetBuffer(string path)
    {
        byte[] buffer = null;
        using (FileStream fs = new FileStream(path, FileMode.Open))
        {
            buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
        }
       return buffer;
    }
}
  • 加载文件资源
代码语言:javascript
复制
void Start () {

        byte[] buffer = LocalFileMgr.Instance().GetBuffer(@"F:\项目\Unity\YD_Destop_ImmersionClick\Assetbundles\Windows\Role\item.assetbundle");

        AssetBundle bundle = AssetBundle.LoadFromMemory(buffer);

        GameObject go = bundle.LoadAsset("Item") as GameObject;

        bundle.Unload(false);

        Instantiate(go);
    }
同步加载封装
  • 资源加载类
代码语言:javascript
复制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 加载资源包
/// </summary>
public class AssetBundlLoad:IDisposable  {

    private AssetBundle bundle;

    // 构造方法
    public AssetBundlLoad(string assetBundlePath)
    {
        string fullPath = LocalFileMgr.Instance().LocalFilePath + assetBundlePath;
        bundle = AssetBundle.LoadFromMemory(LocalFileMgr.Instance().GetBuffer(fullPath));
    }

    public void Dispose()
    {
        if (bundle != null) bundle.Unload(false);
    }

    // 
    public T LoadAsset<T>(string name) where T : UnityEngine.Object
    {
        if (bundle == null) return default(T);
        return bundle.LoadAsset(name) as T;
    }
}
  • 资源加载管理类
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssetBundleMgr : Singleton<AssetBundleMgr> {

    public GameObject Load(string path, string name)
    {
        using (AssetBundlLoad loder = new AssetBundlLoad(path))
        {
            return loder.LoadAsset<GameObject>(name);
        }
    }
}
  • 加载场景Scene
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadScene : MonoBehaviour {
    void Start () {

        GameObject obj = AssetBundleMgr.Instance().Load(@"Role\item.assetbundle", "Item"); 
        Instantiate(obj);
    }
异步读取AssetBundle
  • 单例类改造
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  class  Singleton<T> where T : class, new()
    {
        private static T _instance;
        private static readonly object syslock = new object();

        public static T Instance
        {
        get {
            if (_instance == null)
            {
                lock (syslock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                    }
                    
                }            
            }
            return _instance;
        }
                    
        }
  • 异步加载控制器
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssetBundleMgr : Singleton<AssetBundleMgr> {

    public GameObject Load(string path, string name)
    {
        using (AssetBundlLoad loder = new AssetBundlLoad(path))
        {
            return loder.LoadAsset<GameObject>(name);
        }
    }


    public AssetBundleLoaderAsync LoadAsyncObj(string path, string name)
    {
        GameObject obj = new GameObject("AssetBundleLoadAsync");
        AssetBundleLoaderAsync async =  obj.AddComponent<AssetBundleLoaderAsync>();
        async.Init(path, name);
        return async;
    }
}
  • Start异步加载
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadScene : MonoBehaviour {
    void Start () {c'c

       // 异步加载 
        AssetBundleLoaderAsync async = AssetBundleMgr.Instance.LoadAsyncObj(@"Role\item.assetbundle", "Item");
        async.OnLoadComplete = OnLoadComplete;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 同步读取AssetBundle
    • 同步加载封装
    • 异步读取AssetBundle
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档