当前位置: 首页 > news >正文

苏州网站建设外包收录网站查询

苏州网站建设外包,收录网站查询,ps切图做网站,凡科做的网站百度不到目录 前言 AB包是什么 AB包有什么作用 1.相对Resources下的资源AB包更好管理资源 2.减小包体大小 3.热更新 官方提供的打包工具:Asset Bundle Browser AB包资源加载 AB包资源管理模块代码 前言 在现代游戏开发中,资源管理是一项至关重要的任务。随着游戏内容…

目录

前言

AB包是什么

AB包有什么作用

1.相对Resources下的资源AB包更好管理资源

2.减小包体大小

3.热更新

官方提供的打包工具:Asset Bundle Browser

AB包资源加载

 AB包资源管理模块代码


前言

在现代游戏开发中,资源管理是一项至关重要的任务。随着游戏内容的日益丰富和复杂,如何高效地加载、管理和卸载游戏资源成为了开发者们必须面对的挑战。Unity,作为一款广泛使用的游戏引擎,提供了一套强大的资源管理方案——AssetBundles(简称AB包)。

AB包是什么

特定于平台的资产压缩包,有点类似压缩文件
资产包括:模型、贴图、预设体、音效、材质球等等


AB包有什么作用

1.相对Resources下的资源AB包更好管理资源

2.减小包体大小

1.压缩资源
2.减少初始包大小

3.热更新

资源热更新
脚本热更新


官方提供的打包工具:Asset Bundle Browser

将以下脚本放置在Editor文件夹中

using UnityEditor;
using UnityEngine;public class CreateAssetBundles
{[MenuItem("Assets/Build AssetBundles")]static void BuildAllAssetBundles(){string assetBundleDirectory = "Assets/AssetBundles";if (!System.IO.Directory.Exists(assetBundleDirectory)){System.IO.Directory.CreateDirectory(assetBundleDirectory);}BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);}
}

创建AB包,选中资源,在Asset Bundle里选择New
我这里取名为icon

点击Asset/Build AssetBundles

这样就会在AssetBundles文件夹里生成一些资源文件

AB包文件:资源文件
manifest文件:AB包文件信息;当加载时,提供了关键信息,资源信息,依赖关系,版本信息等等
关键AB包(和目录名一样的包):主包,AB包依赖关键信息

AB包资源加载

将脚本挂载到Image上,启动项目,资源会从ab包中加载到sprite上

using UnityEngine;
using UnityEngine.UI;public class ABTest : MonoBehaviour
{// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){//第一步 加载 AB包AssetBundle ab= AssetBundle.LoadFromFile("Assets/AssetBundles/icon");//第二步 加载 AB包中的资源//只是用名字加载 会出现 同名不同类型资源 分不清//建议用泛型或Type指定类型Sprite icon = ab.LoadAsset("item_0", typeof(Sprite)) as Sprite;GetComponent<Image>().sprite = icon;}// Update is called once per framevoid Update(){}
}

协程加载

using System.Collections;
using UnityEngine;
using UnityEngine.UI;public class ABTest : MonoBehaviour
{// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){StartCoroutine(LoadABRes("item_0"));}IEnumerator LoadABRes(string resName){AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync("Assets/AssetBundles/icon");yield return abcr;//加载资源AssetBundleRequest abq= abcr.assetBundle.LoadAssetAsync(resName,typeof(Sprite));yield return null;GetComponent<Image>().sprite = abq.asset as Sprite;}}

卸载AB包

//卸载所有加载的AB包 参数为ture 会把通过AB包加载的资源也卸载了AssetBundle.UnloadAllAssetBundles(false);

关于AB包的依赖,一个资源身上用到了别的AB包中的资源,这个时候,如果只加载自己的AB包,通过它创建对象会出现资源丢失的情况,这种时候 需要把依赖包 一起加载了 才能正常。

依赖包的固定写法

using System.Collections;
using UnityEngine;
using UnityEngine.UI;public class ABTest : MonoBehaviour
{// Start is called once before the first execution of Update after the MonoBehaviour is createdvoid Start(){//第一步 加载 AB包AssetBundle ab = AssetBundle.LoadFromFile("AB包文件路径");//依赖包的关键知识点一利用主包 获取依赖信息//加载主包AssetBundle abMain = AssetBundle.LoadFromFile("主包文件路径");//加载主包中的固定文件AssetBundleManifest abManifest = abMain.LoadAsset<AssetBundleManifest> ("AssetBundleManifest");//从固定文件中 得到依赖信息string[] strs = abManifest.GetAllDependencies("AB包名");for (int i = 0; i < strs.Length; i++) {AssetBundle.LoadFromFile("前置路径/" + strs[i]);}}}

 AB包资源管理模块代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class ABMgr : MonoBehaviour
{private static ABMgr instance;public static ABMgr GetInstance(){if (instance == null){GameObject obj = new GameObject();//设置对象的名字为脚本obj.name = typeof(GameObject).ToString();instance = obj.AddComponent<ABMgr>();}return instance;}//主包private AssetBundle mainAB = null;//主包依赖获取配置文件private AssetBundleManifest manifest = null;//选择存储 AB包的容器//AB包不能够重复加载 否则会报错//字典知识 用来存储 AB包对象private Dictionary<string, AssetBundle> abDic = new Dictionary<string, AssetBundle>();/// <summary>/// 获取AB包加载路径/// </summary>private string PathUrl{get{return Application.streamingAssetsPath + "/";}}/// <summary>/// 主包名 根据平台不同 报名不同/// </summary>private string MainName{get{
#if UNITY_IOSreturn "IOS";
#elif UNITY_ANDROIDreturn "Android";
#elsereturn "PC";
#endif}}/// <summary>/// 加载主包 和 配置文件/// 因为加载所有包是 都得判断 通过它才能得到依赖信息/// 所以写一个方法/// </summary>private void LoadMainAB(){if (mainAB == null){mainAB = AssetBundle.LoadFromFile(PathUrl + MainName);manifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");}}/// <summary>/// 加载指定包的依赖包/// </summary>/// <param name="abName"></param>private void LoadDependencies(string abName){//加载主包LoadMainAB();//获取依赖包string[] strs = manifest.GetAllDependencies(abName);for (int i = 0; i < strs.Length; i++){if (!abDic.ContainsKey(strs[i])){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + strs[i]);abDic.Add(strs[i], ab);}}}/// <summary>/// 泛型资源同步加载/// </summary>/// <typeparam name="T"></typeparam>/// <param name="abName"></param>/// <param name="resName"></param>/// <returns></returns>public T LoadRes<T>(string abName, string resName) where T : Object{//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//得到加载出来的资源T obj = abDic[abName].LoadAsset<T>(resName);//如果是GameObject 因为GameObject 100%都是需要实例化的//所以我们直接实例化if (obj is GameObject)return Instantiate(obj);elsereturn obj;}/// <summary>/// Type同步加载指定资源/// </summary>/// <param name="abName"></param>/// <param name="resName"></param>/// <param name="type"></param>/// <returns></returns>public Object LoadRes(string abName, string resName, System.Type type){//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//得到加载出来的资源Object obj = abDic[abName].LoadAsset(resName, type);//如果是GameObject 因为GameObject 100%都是需要实例化的//所以我们直接实例化if (obj is GameObject)return Instantiate(obj);elsereturn obj;}/// <summary>/// 名字 同步加载指定资源/// </summary>/// <param name="abName"></param>/// <param name="resName"></param>/// <returns></returns>public Object LoadRes(string abName, string resName){//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//得到加载出来的资源Object obj = abDic[abName].LoadAsset(resName);//如果是GameObject 因为GameObject 100%都是需要实例化的//所以我们直接实例化if (obj is GameObject)return Instantiate(obj);elsereturn obj;}/// <summary>/// 泛型异步加载资源/// </summary>/// <typeparam name="T"></typeparam>/// <param name="abName"></param>/// <param name="resName"></param>/// <param name="callBack"></param>public void LoadResAsync<T>(string abName, string resName, UnityAction<T> callBack) where T : Object{StartCoroutine(ReallyLoadResAsync<T>(abName, resName, callBack));}//正儿八经的 协程函数private IEnumerator ReallyLoadResAsync<T>(string abName, string resName, UnityAction<T> callBack) where T : Object{//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//异步加载包中资源AssetBundleRequest abq = abDic[abName].LoadAssetAsync<T>(resName);yield return abq;if (abq.asset is GameObject)callBack(Instantiate(abq.asset) as T);elsecallBack(abq.asset as T);}/// <summary>/// Type异步加载资源/// </summary>/// <param name="abName"></param>/// <param name="resName"></param>/// <param name="type"></param>/// <param name="callBack"></param>public void LoadResAsync(string abName, string resName, System.Type type, UnityAction<Object> callBack){StartCoroutine(ReallyLoadResAsync(abName, resName, type, callBack));}private IEnumerator ReallyLoadResAsync(string abName, string resName, System.Type type, UnityAction<Object> callBack){//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//异步加载包中资源AssetBundleRequest abq = abDic[abName].LoadAssetAsync(resName, type);yield return abq;if (abq.asset is GameObject)callBack(Instantiate(abq.asset));elsecallBack(abq.asset);}/// <summary>/// 名字 异步加载 指定资源/// </summary>/// <param name="abName"></param>/// <param name="resName"></param>/// <param name="callBack"></param>public void LoadResAsync(string abName, string resName, UnityAction<Object> callBack){StartCoroutine(ReallyLoadResAsync(abName, resName, callBack));}private IEnumerator ReallyLoadResAsync(string abName, string resName, UnityAction<Object> callBack){//加载依赖包LoadDependencies(abName);//加载目标包if (!abDic.ContainsKey(abName)){AssetBundle ab = AssetBundle.LoadFromFile(PathUrl + abName);abDic.Add(abName, ab);}//异步加载包中资源AssetBundleRequest abq = abDic[abName].LoadAssetAsync(resName);yield return abq;if (abq.asset is GameObject)callBack(Instantiate(abq.asset));elsecallBack(abq.asset);}//卸载AB包的方法public void UnLoadAB(string name){if (abDic.ContainsKey(name)){abDic[name].Unload(false);abDic.Remove(name);}}//清空AB包的方法public void ClearAB(){AssetBundle.UnloadAllAssetBundles(false);abDic.Clear();//卸载主包mainAB = null;}
}

http://www.hkea.cn/news/623108/

相关文章:

  • 电子商务网站建设与维护展望今日新闻联播
  • 网站建设主流技术站长之家ping检测
  • 温州建设集团有限公司网站首页百度手机版网页
  • 广西网络干部学院官网seo推广人员
  • 可以做红娘的相亲网站江北seo综合优化外包
  • 公司建设网站需要注意什么软文广告示范
  • 高端网站建设 引擎技企业网页
  • 模仿别人网站百度外链查询工具
  • 教程建设网站广告免费发布信息平台
  • wordpress php5.4支持宁波seo排名优化
  • 宁波制作网站哪个好百度怎么发自己的小广告
  • 新浪网站用什么语言做的百度软件下载
  • wordpress如何做网站重庆seo俱乐部联系方式
  • 教育局两学一做网站深圳全网推广平台
  • 淘宝做详情页代码网站免费大数据查询平台
  • 苹果做安卓游戏下载网站好新媒体营销案例ppt
  • 网络营销实务关键词优化seo优化排名
  • 网站推广优化教程游戏代理加盟平台
  • 网站提升权重全国疫情高峰感染进度
  • 营销型网站怎么做智能建站abc
  • 捷信做单官方网站网络服务主要包括什么
  • 网站建设的方案费用什么时候网络推广
  • 这么做3d展示网站公司百度官网优化
  • 工业设计软件上市公司搜索引擎优化的方法
  • 网站建设公司创意网站网络推广推广
  • 浙江三建建设集团有限公司网站关键词的作用
  • 网站建设官方网站教育培训机构加盟十大排名
  • 万网上传网站seo免费
  • 孝感做网站公司百度热议排名软件
  • 建设网站费用吗廊坊seo快速排名