一.架构
1. AssetBundle打包管理
2.类对象池资源池对象池 打包管理ssetBundle打包管理
3.离线数据及配置表
4.使用框架制作简单的UI系统
预备知识 :
1.程序集
2.unity资源加载方式介绍
3.c#的xml序列化
4.unity asset序列化
二.程序集
unity2017.3之后可以自定义程序集,方便解耦,项目中一些底层的东西可以打包以方便跨平台使用,新建test脚本:
该dll生成目录为(2018之后还会默认生成一些dll) :
自定义程序集 :
你会发现先前创建的test.cs会属于新创建的test1.dll,之前默认的dll也不见了:
也就是说,新建的 dll会包含同级目录以及子目录中的脚本(不会包含同级文件夹中的脚本,总的来说就是就近原则,以文件夹区分)
我们都知道没有引用一个库(using),是无法调用其中的脚本的,当自定义程序集想要引用其他自定义的程序集时,需要添加依赖,这样我们可以自定义程序集之间的依赖关系
而默认程序集(Assembly-CSharp.dll)是自动依赖所有自定义的程序集,不需手动添加依赖
三.资源加载
拖到组件上(商业基本不会用)
2.Resources.load
商用不常用,Resources底下资源是有限的,大概2个G,一般放配置表之类的
3.AssetBundle (商用模式常用,比Resources.load 效率高,占用内存小)
①.打包
public class BundleEditor : MonoBehaviour {
[MenuItem("Tool/打包")]
public static void Build()
{
//将AB打包在StreamingAssets文件夹下,注意先创建该文件夹
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath,
BuildAssetBundleOptions.ChunkBasedCompression,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh(); //编辑器的刷新
}
}
将要打包的资源进行AB命名:
Tool - 打包 :
②.访问
void Start () {
AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/abtest.aaa");
GameObject gameObject = Instantiate(assetBundle.LoadAsset<GameObject>("attack"));
}
//Assets目录下的相对路径,要加上后缀
//主要用于在编辑器中用代码更改本地文件
GameObject gameObject = Instantiate(
UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameData/Prefabs/Attack.prefab"));
四.c#的xml序列化 (将一个类序列化本地数据)
1.类与xml相互转化(实际上不用,太明显,数据量也大)
需要转化的类 :
using System.Xml.Serialization;
//需要序列化的类上需要有这个标签
[System.Serializable]
public class ToXml
{ //注意不要继承MonoBehaviour
[XmlAttribute("id")]
public int id;
[XmlElement("list")]//也可以用XmlArray
public List<int> list { get; set; }
}
类转xml :
using System.Xml.Serialization;
public class LoadFromStreamingFile : MonoBehaviour {
void Start () {
OnSerialize();
}
void Update () {
}
public void OnSerialize()
{
ToXml toXml = new ToXml();
toXml.id = -1;
toXml.list = new List<int>();
toXml.list.Add(1);
toXml.list.Add(2);
xmlSerialize(toXml);
}
public void xmlSerialize(ToXml toxml)
{
FileStream fileStream = new FileStream(Application.dataPath + "/test.xml",FileMode.Create,FileAccess.ReadWrite,
FileShare.ReadWrite ); //打开文件流
StreamWriter sw = new StreamWriter(fileStream,System.Text.Encoding.UTF8); //打开写入流
XmlSerializer xmlSerializer = new XmlSerializer(toxml.GetType()); //需要传入序列化类的类型
xmlSerializer.Serialize(sw, toxml);
//关闭
sw.Close();
fileStream.Close();
}
}
转化之后:
xml文件转类:
public void DeSerialize()
{
ToXml toXml = xmlDeSerialize();
Debug.Log(toXml.id);
foreach(int i in toXml.list)
{
Debug.Log(i);
}
}
public ToXml xmlDeSerialize()
{
Debug.Log(Application.dataPath + "/test.xml");
FileStream fileStream = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite,
FileShare.ReadWrite); //打开文件流
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ToXml)); //需要传入序列化类的类型
ToXml toXml = (ToXml)xmlSerializer.Deserialize(fileStream);
fileStream.Close();
return toXml;
}
2.类转成二进制
序列化:
public void OnBinarySerialize()
{
ToXml toXml = new ToXml();
toXml.id = -1;
toXml.list = new List<int>();
toXml.list.Add(1);
toXml.list.Add(2);
BinarySerilizer(toXml);
}
public void BinarySerilizer(ToXml toXml)
{
Debug.Log(Application.dataPath);
FileStream fileStream = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite,
FileShare.ReadWrite); //打开文件流
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fileStream, toXml);
fileStream.Close();
}
反序列化:
public void DeBinarySerialize()
{
ToXml toXml = BinaryDeSerialize();
Debug.Log(toXml.id);
foreach (int i in toXml.list)
{
Debug.Log(i);
}
}
public ToXml BinaryDeSerialize()
{
TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes");
MemoryStream stream = new MemoryStream(textAsset.bytes);
BinaryFormatter bf = new BinaryFormatter();
ToXml toXml = (ToXml)bf.Deserialize(stream);
stream.Close();
return toXml;
}
五.unity asset序列化
[CreateAssetMenu(fileName ="TestAssets",menuName = "CreateAssets",order = 0)]
public class AssetSerialize : ScriptableObject {
public int id;
public List<string> TestList;
}
注意dictionary是序列化不好的,一般使用两个list保存
public void ReadTestAssets()
{
AssetSerialize assetSerialize = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetSerialize>
("Assets/1.架构以及预备知识/scripts/TestAssets.asset");
Debug.Log(assetSerialize.id);
foreach (string i in assetSerialize.TestList)
{
Debug.Log(i);
}
}