unity创建和加载AssetBundle

先说一下为什么要使用AssetBundle吧,以前做东西一直忽略这个问题,现在认为这个步骤很重要,代码是次要的,决策和为什么这样搞才是关键。

一句话概括吧,AssetBundle实现了资源与服务分离,方便做热更新。

一、创建AssetBundle

两步:1.设置AssetBundleName;2.调用BuildPipeline.BuildAssetBundles。详细过程如下:

设置AssetBundleName有两种方式,分为手动和代码

先讲手动,找到你需要被打包的文件,然后如下图

方式2,代码设置这个AssetBundleName,代码如下,创建bundle的代码也一起贴上了:

using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class BuildBundleMenu : MonoBehaviour {

	public static string sourcePathPrefab = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Prefabs/";
	public static string sourcePathMater = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Materials/";

	#if UNITY_EDITOR
	[MenuItem( "Example/Build Asset Bundles" )]
	static void BuildABs( )
	{

		ClearAssetBundlesName ();  

		FindAllFile (sourcePathPrefab);
		FindAllFile (sourcePathMater); 

		// Put the bundles in a folder called "ABs" within the Assets folder.
		BuildPipeline.BuildAssetBundles( "Assets/ABs", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneOSXIntel64);
	}

	/// <summary>
	/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
	/// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
	/// </summary>
	static void ClearAssetBundlesName()
	{
		int length = AssetDatabase.GetAllAssetBundleNames ().Length;
		Debug.Log (length);
		string[] oldAssetBundleNames = new string[length];
		for (int i = 0; i < length; i++)
		{
			oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
		}  

		for (int j = 0; j < oldAssetBundleNames.Length; j++)
		{
			AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);
		}
		length = AssetDatabase.GetAllAssetBundleNames ().Length;
		Debug.Log (length);
	}  

   	/// <summary>
   	/// 遍历文件夹里面的所有文件夹和文件
   	/// </summary>
   	/// <param name="source"></param>
	static void FindAllFile(string source)
	{
		DirectoryInfo folder = new DirectoryInfo (source);
		FileSystemInfo[] files = folder.GetFileSystemInfos ();
		int length = files.Length;
		for (int i = 0; i < length; i++) {
			if(files[i] is DirectoryInfo)
			{
				FindAllFile(files[i].FullName);
			}
			else
			{
				if(!files[i].Name.EndsWith(".meta"))
				{
					SetAssetName (files[i].FullName);
				}
			}
		}
	}  

	/// <summary>
	/// 为需要打包的文件设置assetName.
	/// </summary>
	/// <param name="source"></param>
	static void SetAssetName(string source)
	{
		string _assetPath = "Assets" + source.Substring (Application.dataPath.Length);
		//string _assetPath2 = source.Substring (Application.dataPath.Length + 1);
		//在代码中给资源设置AssetBundleName
		AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);
		//string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);
		//assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");
		//assetImporter.assetBundleName = assetName;
		assetImporter.assetBundleName = "Weather.unity3d";
	}
	#endif
}

  菜单栏会出现这个,点一下就可以了,bundle创建完成。注意打bundle前要现在Assets目录下新建一个ABs文件夹,打的bundle都在这个文件夹里面。

注:只要设置了AssetBundleName的,都会进行打包,不论在什么目录下。

二、bundle的加载

直接贴代码吧

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadAssetbundle : MonoBehaviour {

	private string pathurl = "";
	// Use this for initialization
	void Start () {
		string pathPrefab = "file://" + Application.dataPath + "/ABs/weather.unity3d";

		StartCoroutine (LoadALLGameObject(pathPrefab));
	}

	//读取全部资源
	private IEnumerator LoadALLGameObject(string path)
	{  

		WWW bundle = new WWW(path);  

		yield return bundle;  

		//通过Prefab的名称把他们都读取出来
		Object  obj0 =  bundle.assetBundle.LoadAsset("CloudStorm_02_8x8-64.prefab");   

		//加载到游戏中
		yield return Instantiate(obj0);
		bundle.assetBundle.Unload(false);
	}
}

  

时间: 2024-08-06 07:37:25

unity创建和加载AssetBundle的相关文章

创建控制器的3种方式、深入了解view的创建和加载顺序

转载自:http://blog.csdn.net/weisubao/article/details/41012243 (1)创建控制器的3种方式 [objc] view plaincopy - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc]initWithFrame:[

【iOS开发-73】创建控制器的3种方式、深入了解view的创建和加载顺序

(1)创建控制器的3种方式 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; //关于主窗口 //在iOS6之前,

2018-11-13#Hive外表创建和加载数据

hive 关联表和外表的区别: 1. 外部表需要external关键之,location是数据文件默认存放位置,不管是不管是select 还是load的数据都存放在这里.导入数据到外部表,数据并没有mv到数据仓库目录,而是在loacation目录. 2.内部表建表时也能加上location,作用和外部表一样,都是表数据的存放路径,不同的是drop table时内部表会将hdfs上的数据也删掉,外部表仅仅是删除表的元数据,原始数据不会删除. Hive 外表的优点: 1.安全,外部表不用担心表删除带

Unity最新版打包AssetBundle和加载的方法

一.设置assetBundleName二.构建AssetBundle包三.上传AssetBundle到服务器四.把AssetBundle放到本地五.操作AssetBundle六.完整例子七.AssetBundle Manager管理工具八.备注知识 一.设置assetBundleName 如果没有设置AssetBundleName,会打包所有的Assets下的资源,如果设置,就只打包设置了名字的资源 1.在unity编辑器界面手动设置 输入所需的AssetBundle名称.请注意,AssetBu

unity 加载Assetbundle文件夹路径需要注意

测试时 在Editor下加载Assetbundle文件夹下路径成功 在Android下使用www一直失败 使用Assetbundle.loadfromfile可以 发现:使用AssetBundle.LoadFromFile的路径是path,那么使用www加载需要前缀 "File://"+path才可以 ( Application.persistentDataPath;) 加载StreamAssets路径下同理,  www需要加前缀 "jar:File://";   

Unity资源解决方案之AssetBundle

1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material.Texture.Audio.noxss等等,同时,AssetBundle也可以包含开发者自定义的二进制文件,只需要将自定义文件的扩展名改为.bytes,Unity就可以把它识别为TextAsset,进而就可以被打包到AssetBundle中.Unity引擎所能识别的资源我们称为Asset,Asset

Unity资源打包之Assetbundle

转  Unity资源打包之Assetbundle 本文原创版权归 csdn janeky 所有,转载请详细注明原创作者及出处,以示尊重! 作者:janeky 原文:http://blog.csdn.net/janeky/article/details/17652021 如果这篇文章对你有帮助,敬请关注作者<Unity手游之路>系列教程. 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制成预设Prefab,打包成场景.今天我们来一起学习官方推荐的As

【Unity游戏开发】AssetBundle杂记--AssetBundle的二三事

一.简介 马三在公司大部分时间做的都是游戏业务逻辑和编辑器工具等相关工作,因此对Unity AssetBundle这块的知识点并不是很熟悉,自己也是有打算想了解并熟悉一下AssetBundle,掌握一下Unity资源管理方面的知识,因此有了这篇博客. 我会在博客中整理出一些自己学习时收集到的一些比较好的AssetBundle学习资料,并且结合现在正在参与开发的商业项目随时写下一些自己的拙见.本篇博客权当是马三自己的笔记和一些杂谈,会不断地随时随地的更新一些内容. 二.收集整理的一些AssetBu

源码跟读,Spring是如何解析和加载xml中配置的beans

Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086cd440b7799a63 1.配置启动Spring所需的监听器 web.xml中配置监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-cla