[Unity热更新]动态加载

参考链接:

http://www.xuanyusong.com/archives/1919


对于动态加载,主要有两种方式:

1.使用Resources.Load

2.使用AssetBundle


在游戏中,有一个很常见的情况:

有多个场景,且一开始时场景中角色和摄像机的位置旋转是不同的。如果我们把角色都放在场景,然后打包,明显是不对的(会增加apk的体积),所以需要把角色和场景分开,放在不同的包中。这时就需要根据配置信息来放置角色和摄像机的位置了。


生成配置文件:

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
using LitJson;

public class ExportScene : Editor {

	//将所有游戏场景导出为XML格式
	[MenuItem ("Tool/ExportXML")]
	static void ExportXML ()
	{
		string filepath = Application.dataPath + @"/StreamingAssets/sceneInfoXML.xml";
		if(File.Exists (filepath))
		{
			File.Delete(filepath);
		}
		XmlDocument xmlDoc = new XmlDocument();
		XmlElement root = xmlDoc.CreateElement("scenes");
		//遍历所有的游戏场景
		foreach (UnityEditor.EditorBuildSettingsScene s in UnityEditor.EditorBuildSettings.scenes)
		{
			//当关卡启用
			if (s.enabled)
			{
				//得到关卡的名称
				string name = s.path;
				//打开这个关卡
				EditorApplication.OpenScene(name);
				XmlElement scene = xmlDoc.CreateElement("scene");
				scene.SetAttribute("name",name);
				foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
				{
					//因为场景内物体都制作为prefab,所以只需遍历父物体而无需遍历子物体(prefab已包含子物体的信息)
					if (obj.transform.parent == null)
					{
						XmlElement gameObject = xmlDoc.CreateElement("gameObject");
						gameObject.SetAttribute("name",obj.name);				
						gameObject.SetAttribute("asset",obj.name + ".prefab");
						XmlElement transform = xmlDoc.CreateElement("transform");

						XmlElement position = xmlDoc.CreateElement("position");
						XmlElement position_x = xmlDoc.CreateElement("x");
						position_x.InnerText = obj.transform.position.x+"";
						XmlElement position_y = xmlDoc.CreateElement("y");
						position_y.InnerText = obj.transform.position.y+"";
						XmlElement position_z = xmlDoc.CreateElement("z");
						position_z.InnerText = obj.transform.position.z+"";
						position.AppendChild(position_x);
						position.AppendChild(position_y);
						position.AppendChild(position_z);
						
						XmlElement rotation = xmlDoc.CreateElement("rotation");
						XmlElement rotation_x = xmlDoc.CreateElement("x");
						rotation_x.InnerText = obj.transform.rotation.eulerAngles.x+"";
						XmlElement rotation_y = xmlDoc.CreateElement("y");
						rotation_y.InnerText = obj.transform.rotation.eulerAngles.y+"";
						XmlElement rotation_z = xmlDoc.CreateElement("z");
						rotation_z.InnerText = obj.transform.rotation.eulerAngles.z+"";
						rotation.AppendChild(rotation_x);
						rotation.AppendChild(rotation_y);
						rotation.AppendChild(rotation_z);
						
						XmlElement scale = xmlDoc.CreateElement("scale");
						XmlElement scale_x = xmlDoc.CreateElement("x");
						scale_x.InnerText = obj.transform.localScale.x+"";
						XmlElement scale_y = xmlDoc.CreateElement("y");
						scale_y.InnerText = obj.transform.localScale.y+"";
						XmlElement scale_z = xmlDoc.CreateElement("z");
						scale_z.InnerText = obj.transform.localScale.z+"";			
						scale.AppendChild(scale_x);
						scale.AppendChild(scale_y);
						scale.AppendChild(scale_z);
						
						transform.AppendChild(position);
						transform.AppendChild(rotation);
						transform.AppendChild(scale);	
						
						gameObject.AppendChild(transform);
						scene.AppendChild(gameObject);
						root.AppendChild(scene);
						xmlDoc.AppendChild(root);
						xmlDoc.Save(filepath);				
					}
				}
			}
		}
		//刷新Project视图, 不然需要手动刷新哦
		AssetDatabase.Refresh();
	}
	
	//将所有游戏场景导出为JSON格式
	[MenuItem ("Tool/ExportJSON")]
	static void ExportJSON ()
	{
		string filepath = Application.dataPath + @"/StreamingAssets/sceneInfoJSON.txt";
		FileInfo t = new FileInfo(filepath);
		if(File.Exists (filepath))
		{
			File.Delete(filepath);
		}
		StreamWriter sw = t.CreateText();
		
		StringBuilder sb = new StringBuilder ();
		JsonWriter writer = new JsonWriter (sb);
		writer.WriteObjectStart ();
		writer.WritePropertyName ("scenes");
		writer.WriteArrayStart ();
		
		foreach (UnityEditor.EditorBuildSettingsScene s in UnityEditor.EditorBuildSettings.scenes)
		{
			if (s.enabled)
			{
				string name = s.path;
				EditorApplication.OpenScene(name);
				writer.WriteObjectStart();
				writer.WritePropertyName("scene");
				writer.WriteObjectStart();
				writer.WritePropertyName("name");
				writer.Write(name);
				writer.WritePropertyName("gameObjects");
				writer.WriteArrayStart ();
				
				foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
				{
					if (obj.transform.parent == null)
					{
						writer.WriteObjectStart();
						writer.WritePropertyName("name");
						writer.Write(obj.name);
						
						writer.WritePropertyName("position");
						writer.WriteArrayStart ();
						writer.WriteObjectStart();
						writer.WritePropertyName("x");
						writer.Write(obj.transform.position.x.ToString("F5"));
						writer.WritePropertyName("y");
						writer.Write(obj.transform.position.y.ToString("F5"));
						writer.WritePropertyName("z");
						writer.Write(obj.transform.position.z.ToString("F5"));
						writer.WriteObjectEnd();
						writer.WriteArrayEnd();
						
						writer.WritePropertyName("rotation");
						writer.WriteArrayStart ();
						writer.WriteObjectStart();
						writer.WritePropertyName("x");
						writer.Write(obj.transform.rotation.eulerAngles.x.ToString("F5"));
						writer.WritePropertyName("y");
						writer.Write(obj.transform.rotation.eulerAngles.y.ToString("F5"));
						writer.WritePropertyName("z");
						writer.Write(obj.transform.rotation.eulerAngles.z.ToString("F5"));
						writer.WriteObjectEnd();
						writer.WriteArrayEnd();
						
						writer.WritePropertyName("scale");
						writer.WriteArrayStart ();
						writer.WriteObjectStart();
						writer.WritePropertyName("x");
						writer.Write(obj.transform.localScale.x.ToString("F5"));
						writer.WritePropertyName("y");
						writer.Write(obj.transform.localScale.y.ToString("F5"));
						writer.WritePropertyName("z");
						writer.Write(obj.transform.localScale.z.ToString("F5"));
						writer.WriteObjectEnd();
						writer.WriteArrayEnd();
						
						writer.WriteObjectEnd();
					}
				}
				
				writer.WriteArrayEnd();//end gameObjects
				writer.WriteObjectEnd();//end name
				writer.WriteObjectEnd();//end scene
			}
		}
		writer.WriteArrayEnd();//end scenes
		writer.WriteObjectEnd ();//end root
		
		sw.WriteLine(sb.ToString());
		sw.Close();
		sw.Dispose();
		AssetDatabase.Refresh();
	}

	//将所有游戏场景导出为Binary格式
	[MenuItem ("Tool/ExportBinary")]
	static void ExportBinary ()
	{
		string filepath = Application.dataPath + @"/StreamingAssets/sceneInfoBinary.txt";
		if(File.Exists (filepath))
		{
			File.Delete(filepath);
		}
		FileStream fs = new FileStream(filepath, FileMode.Create);
		BinaryWriter bw = new BinaryWriter(fs);
		foreach (UnityEditor.EditorBuildSettingsScene s in UnityEditor.EditorBuildSettings.scenes)
		{
			if (s.enabled)
			{
				string sceneName = s.path;
				EditorApplication.OpenScene(sceneName);
				bw.Write(sceneName);

				foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
				{
					if (obj.transform.parent == null)
					{
						bw.Write(obj.name);
						//将float转化为short,这样传输的字节数就会变少
						//接收时将short转化为float就可以了
						bw.Write((short)(obj.transform.position.x * 100));
						bw.Write((short)(obj.transform.position.y * 100));
						bw.Write((short)(obj.transform.position.z * 100));
						bw.Write((short)(obj.transform.rotation.eulerAngles.x * 100));
						bw.Write((short)(obj.transform.rotation.eulerAngles.y * 100));
						bw.Write((short)(obj.transform.rotation.eulerAngles.z * 100));
						bw.Write((short)(obj.transform.localScale.x * 100));
						bw.Write((short)(obj.transform.localScale.y * 100));
						bw.Write((short)(obj.transform.localScale.z * 100));		
					}
				}
			}
		}
		
		bw.Flush();
		bw.Close();
		fs.Close();
		AssetDatabase.Refresh();
	}
}

读取配置文件:

using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;

//建立一个空物体,然后挂上此脚本
public class ResolveSceneInfoXML : MonoBehaviour {

	// Use this for initialization
	void Start () 
	{
		//电脑和iphone上的路径是不一样的,这里用标签判断一下。
		#if UNITY_EDITOR
		string filepath = Application.dataPath +"/StreamingAssets"+"/sceneInfoXML.xml";
		#elif UNITY_IPHONE
		string filepath = Application.dataPath +"/Raw"+"/sceneInfoXML.xml";
		#endif
		//如果文件存在话开始解析。
		if(File.Exists (filepath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filepath);
			XmlNodeList nodeList = xmlDoc.SelectSingleNode("scenes").ChildNodes;
			foreach(XmlElement scene in nodeList)
			{
				//因为我的XML是把所有游戏对象全部导出, 所以这里判断一下只解析需要的场景中的游戏对象
				//JSON和它的原理类似
				if(!scene.GetAttribute("name").Equals("Assets/s1.unity"))
				{
					continue;
				}

				//prefab放置在Resources/Prefab文件夹下
				//遍历所有gameobject
				foreach(XmlElement gameObject in scene.ChildNodes)
				{				
					string asset = "Prefab/" + gameObject.GetAttribute("name");
					Vector3 pos = Vector3.zero;
					Vector3 rot = Vector3.zero;
					Vector3 sca = Vector3.zero;
					XmlNode transform = gameObject.SelectSingleNode("transform");

					foreach(XmlElement prs in transform.ChildNodes)
					{
						if(prs.Name == "position")
						{
							foreach(XmlElement position in prs.ChildNodes)
							{
								switch(position.Name)
								{
								case "x":
									pos.x = float.Parse(position.InnerText);
									break;
								case "y":
									pos.y = float.Parse(position.InnerText);
									break;
								case "z":
									pos.z = float.Parse(position.InnerText);
									break;
								}
							}
						}else if(prs.Name == "rotation")
						{
							foreach(XmlElement rotation in prs.ChildNodes)
							{
								switch(rotation.Name)
								{
								case "x":
									rot.x = float.Parse(rotation.InnerText);
									break;
								case "y":
									rot.y = float.Parse(rotation.InnerText);
									break;
								case "z":
									rot.z = float.Parse(rotation.InnerText);
									break;
								}
							}
						}else if(prs.Name == "scale")
						{
							foreach(XmlElement scale in prs.ChildNodes)
							{
								switch(scale.Name)
								{
								case "x":
									sca.x = float.Parse(scale.InnerText);
									break;
								case "y":
									sca.y = float.Parse(scale.InnerText);
									break;
								case "z":
									sca.z = float.Parse(scale.InnerText);
									break;
								}
							}
						}
					}	
					//拿到 旋转 缩放 平移 以后克隆新游戏对象
					GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
					ob.transform.localScale = sca;
				}
			}
		}
	}
}

using UnityEngine;
using System.Collections;
using System.IO;
using LitJson;

public class ResolveSceneInfoJSON : MonoBehaviour {

	// Use this for initialization
	void Start () 
	{
		#if UNITY_EDITOR
		string filepath = Application.dataPath +"/StreamingAssets"+"/sceneInfoJSON.txt";
		#elif UNITY_IPHONE
		string filepath = Application.dataPath +"/Raw"+"/json.txt";
		#endif	
		
		StreamReader sr  = File.OpenText(filepath);
		string strLine = sr.ReadToEnd();
		JsonData jd = JsonMapper.ToObject(strLine);
		JsonData scenes = jd["scenes"];
		int i,j;
		for (i = 0; i < scenes.Count; i++)
		{
			JsonData scene = scenes[i]["scene"];
			string sceneName = (string)scene["name"];
			if(!sceneName.Equals("Assets/s1.unity"))
			{
				continue;
			}
			JsonData gameObjects = scene["gameObjects"];

			for (j = 0; j < gameObjects.Count; j++)
			{
				string objectName = (string)gameObjects[j]["name"];
				string asset = "Prefab/" + objectName;
				Vector3 pos = Vector3.zero;
				Vector3 rot = Vector3.zero;
				Vector3 sca = Vector3.zero;
				
				JsonData position = gameObjects[j]["position"];
				JsonData rotation = gameObjects[j]["rotation"];
				JsonData scale = gameObjects[j]["scale"];

				pos.x = float.Parse((string)position[0]["x"]);
				pos.y = float.Parse((string)position[0]["y"]);
				pos.z = float.Parse((string)position[0]["z"]);
				
				rot.x = float.Parse((string)rotation[0]["x"]);
				rot.y = float.Parse((string)rotation[0]["y"]);
				rot.z = float.Parse((string)rotation[0]["z"]);
				
				sca.x = float.Parse((string)scale[0]["x"]);
				sca.y = float.Parse((string)scale[0]["y"]);
				sca.z = float.Parse((string)scale[0]["z"]);
				
				GameObject go = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
				go.transform.localScale = sca;
			}
		}
	}
}

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System;

public class ResolveSceneInfoBinary : MonoBehaviour {

	// Use this for initialization
	void Start () 
	{
		string filepath = Application.dataPath + @"/StreamingAssets/sceneInfoBinary.txt";
		
		if (File.Exists (filepath)) 
		{
			FileStream fs = new FileStream (filepath, FileMode.Open);
			BinaryReader br = new BinaryReader (fs);

			string sceneName = br.ReadString();
			while(fs.Position < fs.Length)
			{
				string objName = br.ReadString();
				float px = br.ReadInt16() / 100f;
				float py = br.ReadInt16() / 100f;
				float pz = br.ReadInt16() / 100f;
				float rx = br.ReadInt16() / 100f;
				float ry = br.ReadInt16() / 100f;
				float rz = br.ReadInt16() / 100f;
				float sx = br.ReadInt16() / 100f;
				float sy = br.ReadInt16() / 100f;
				float sz = br.ReadInt16() / 100f;

				string asset = "Prefab/" + objName;
				Vector3 pos = new Vector3 (px,py,pz);
				Vector3 rot = new Vector3(rx,ry,rz);
				Vector3 sca = new Vector3(sx,sy,sz);
				GameObject go = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
				go.transform.localScale = sca;
			}
		}
	}
}


上面使用的是Resources.Load,下面就使用AssetBundle吧!这里本人使用的是unity5的打包系统,先从本地服务器下载AB包到本地,再加载。

测试场景很简单,AB包有六个:总的AB包,3个物体的包,cube的材质,材质的纹理,下载后就把文件放在Application.streamingAssetsPath下,加载时就加载camera、cube和灯光。服务器是tomcat服务器,当然也可以跳过下载的过程,直接本地加载AB包。要注意的是,测试最好使用new WWW,而不是WWW.LoadFromCacheOrDownload,因为LoadFromCacheOrDownload需要提供一个版本号,如果不更改版本号就不会下载新的AB包了。




创建AB:

using UnityEngine;
using System.Collections;
using UnityEditor;

/*
 * 注意:
 * 1.prefab的名称以及生成的AB包的名称最好不要带空格,否则从服务器下载AB包时,下载到本地的AB包可能为空。
 * 2.prefab的名称最好全部小写,因为在设置AB包的名称时,unity会将设置的名称全部改为小写。
 * 这样prefab的名称就可以与AB包的名称一一对应了
 */
public class CreateAssetBundle : Editor {

	[MenuItem("Tool/SetFileBundleName")]
	static void SetBundleName()
	{
		#region 设置资源的AssetBundle的名称和文件扩展名
		UnityEngine.Object[] selects = Selection.objects;
		foreach (UnityEngine.Object selected in selects)
		{
			string path = AssetDatabase.GetAssetPath(selected);
			AssetImporter asset = AssetImporter.GetAtPath(path);
			asset.assetBundleName = selected.name; //设置Bundle文件的名称
			asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
			asset.SaveAndReimport();		
		}
		AssetDatabase .Refresh();
		#endregion
	}

	[MenuItem("Tool/BuildAll")]
	static void Build()
	{
		BuildPipeline.BuildAssetBundles(Application.dataPath + "/AB");
	}
}

下载AB:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;

[RequireComponent(typeof(LoadAssetbBundle))]
public class DownLoadAssetbBundle : MonoBehaviour {

	//要下载的AB包
	string[] s = new string[]{"AB","directionallight","cube","camera","m1","t1"};
	
	// Use this for initialization
	void Start () 
	{
		//print (Application.dataPath);
		//print (Application.persistentDataPath);
		//print (Application.streamingAssetsPath);
		//print (Application.temporaryCachePath);
		
		StartCoroutine (DownLoad(s));
	}
	
	IEnumerator DownLoad(string[] assetBundleNames)
	{
		FileStream fs = null;
		for (int i = 0; i < assetBundleNames.Length; i++) 
		{
			string savePath;
			string url;
			if(assetBundleNames[i].Equals("AB"))
			{
				savePath = Application.streamingAssetsPath + "/" + assetBundleNames[i];
				url = "http://172.25.225.24:8080/UpdateServer" + "/" + assetBundleNames[i];
			}
			else
			{
				savePath = Application.streamingAssetsPath + "/" + assetBundleNames[i] + ".unity3d";
				url = "http://172.25.225.24:8080/UpdateServer" + "/" + assetBundleNames[i] + ".unity3d";
			}
			
			//如果不存在则下载AB,并保存到指定文件夹中
			if (!File.Exists (savePath)) 
			{
				fs = new FileStream(savePath, FileMode.Create);
				WWW www = new WWW (url);
				yield return www;
				byte[] bytes = www.bytes;
				print(assetBundleNames[i] + "   " + bytes.Length);
				fs.Write(bytes,0,bytes.Length);
			}
		}
		if (fs != null) 
		{
			fs.Close ();
			fs.Dispose ();
		}

		//加载AB
		LoadAssetbBundle lab = GetComponent<LoadAssetbBundle> ();
		if (lab != null) 
		{
			lab.StartLoad("file://" + Application.streamingAssetsPath + "/",
                          new string[] { "camera", "cube", "directionallight"});//要加载的AB包
		}
	}
}

加载AB:

using UnityEngine;
using System.Collections;

public class LoadAssetbBundle : MonoBehaviour {
	
	public void StartLoad(string assetBundlePath, string[] assetBundleNames)
	{
		StartCoroutine(LoadGo(assetBundlePath,assetBundleNames));
	}

	/*
	 * assetBundlePath:Assetbundle的文件夹,末尾要加"/"
	 * assetBundleNames:要加载的AssetBundle的名字,不用加后缀
	 * 注意:www需要加后缀,而AssetBundle.LoadAsset不需要
	 * */
	IEnumerator LoadGo(string assetBundlePath, string[] assetBundleNames)
	{
		AssetBundleManifest manifest = null;
		//首先加载Manifest文件;
        WWW mwww = new WWW(assetBundlePath + "AB");
		yield return mwww;
		if (!string.IsNullOrEmpty (mwww.error)) 
		{
			Debug.LogError (mwww.error);
		} 
		else 
		{
			AssetBundle ab = mwww.assetBundle;
			manifest = (AssetBundleManifest)ab.LoadAsset ("AssetBundleManifest");
			ab.Unload (false);
		}

		for (int i = 0; i < assetBundleNames.Length; i++) 
		{
			//获取依赖文件列表;
			string[] depends = manifest.GetAllDependencies(assetBundleNames[i] + ".unity3d");
			AssetBundle[] dependsAssetBundle = new AssetBundle[depends.Length];
			
			for(int index = 0;index < depends.Length;index++)
			{
				//加载所有的依赖文件;
				WWW dwww = new WWW(assetBundlePath + depends[index]);
				yield return dwww;
				dependsAssetBundle[index] = dwww.assetBundle;
			}
			
			//加载我们需要的文件;
			WWW gowww = new WWW(assetBundlePath + assetBundleNames[i] + ".unity3d");
			yield return gowww;
			if(!string.IsNullOrEmpty(gowww.error))
			{
				Debug.LogError(gowww.error);
			}
			else
			{
				AssetBundle goAB = gowww.assetBundle;
                GameObject go = goAB.LoadAsset(assetBundleNames[i]) as GameObject;
				if(go != null)
				{
					Instantiate(go);
				}
				goAB.Unload(false);
			}
			
			//卸载依赖文件的包
			for(int index = 0;index < depends.Length;index++)
			{
				dependsAssetBundle[index].Unload(false);
			}
		}
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值