框架开发
- Bundle处理(构建-加载-更新)
- C#调用Lua(Lua加载与管理、Lua绑定与执行)
- 向Lua提供接口
- 完善和优化
资源目录划分
将Assets根据是否需要打bundle分类
1:Lua,UI,模型,特效,声音,动画
0:
xlua
c#:
bundle构建工具等工具
与Lua交互:
c#调用Lua(加载Lua脚本,执行Lua逻辑)
Lua调用c#(c#给Lua提供接口用于资源加载、资源管理、事件管理、场景模型加载等)`
Bundle构建
xLua下载链接:xLua
解压后Assets下的文件导入Unity
策略:对每个单独文件打包bundle
使用Unity提供的BuildPipeline构建
//BuildPipeline(输出路径,数组列表,压缩格式,bundle目标平台)
BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
查找BuildResources下的资源文件
//获取所有文件策略 GetFiles(路径,搜索的匹配字符串,搜索选项:全部文件夹)
string[] files = Directory.GetFiles(PathUtil.BuildResourcesPath, "*", SearchOption.AllDirectories);
生成标签:
[MenuItem("Tools/Build Windows Bundle")]
static void BundleWindowsBuild()
{
Build(BuildTarget.StandaloneWindows);
}
放入UI
运行Build Windows Bundle
结果:路径斜杠混乱
对路径规范处理:
public static string GetStandardPath(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
return path.Trim().Replace("\\", "/");
}
Code
PathUtil.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathUtil
{
//根目录
public static readonly string AseetsPath = Application.dataPath;
//需要打bundle的目录
public static readonly string BuildResourcesPath = Application.dataPath + "/BuildResources";
//bundle输出目录
public static readonly string BundleOutPath = Application.streamingAssetsPath;
public static string GetUnityPath(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
return path.Substring(path.IndexOf("Assets"));
}
//获取标准路径
public static string GetStandardPath(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
return path.Trim().Replace("\\", "/");//替换
}
}
BuildTool.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class BuildTool : Editor
{
[MenuItem("Tools/Build Windows Bundle")]
static void BundleWindowsBuild()
{
Build(BuildTarget.StandaloneWindows);
}
[MenuItem("Tools/Build Android Bundle")]
static void BundleAndroidBuild()
{
Build(BuildTarget.Android);
}
[MenuItem("Tools/Build iPhone Bundle")]
static void BundleiPhoneBuild()
{
Build(BuildTarget.iOS);
}
static void Build(BuildTarget target)
{
List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
//获取所有文件策略 GetFiles(路径,搜索的匹配字符串,搜索选项:全部文件夹)
string[] files = Directory.GetFiles(PathUtil.BuildResourcesPath, "*", SearchOption.AllDirectories);
//排除.meta文件
for(int i = 0; i < files.Length; i++)
{
if (files[i].EndsWith(".meta"))
continue;
AssetBundleBuild assetBundle = new AssetBundleBuild();
string fileName = PathUtil.GetStandardPath(files[i]);
Debug.Log("file:" + fileName);
string assetName = PathUtil.GetUnityPath(fileName);
assetBundle.assetNames=new string[]{ assetName};
string bundleName = fileName.Replace(PathUtil.BuildResourcesPath, "").ToLower();
assetBundle.assetBundleName = bundleName + ".ab";
assetBundleBuilds.Add(assetBundle);
}
//判断输出目录是否存在,若里面由文件则删除再创建
if (Directory.Exists(PathUtil.BundleOutPath))
Directory.Delete(PathUtil.BundleOutPath, true);
Directory.CreateDirectory(PathUtil.BundleOutPath);
//BuildPipeline(输出路径,数组列表,压缩格式,bundle目标平台)
BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);
}
}