miduo_client/Assets/LuaFramework/Scripts/Common/LuaLoader.cs

87 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LuaInterface;
using GameCore;
namespace GameLogic {
/// <summary>
/// 集成自LuaFileUtils重写里面的ReadFile
/// </summary>
public class LuaLoader : LuaFileUtils
{
// Use this for initialization
const string ConfigsName = "InitLuas";
/// <summary>
/// 平台的配置,这些文件默认都会到平台里头去加载。
/// </summary>
List<string> list = new List<string>();
public LuaLoader()
{
instance = this;
beZip = AppConst.luaBundleMode;
InitConfigs();
}
public void InitConfigs() {
string configs = App.ResMgr.LoadAsset<TextAsset>(ConfigsName).text;
App.ResMgr.UnLoadAsset(ConfigsName);
string[] configss = configs.Replace("\r\n", "\n").Replace("\n","|").Split('|');
for (int i = 0; i < configss.Length; i++) {
if (string.IsNullOrEmpty(configss[i])) continue;
if (configss[i].Contains("@")) continue;
list.Add(configss[i]);
}
}
/// <summary>
/// 添加打入Lua代码的AssetBundle
/// </summary>
/// <param name="bundle"></param>
public void AddBundle(string bundleName)
{
string url = Util.DataPath + bundleName.ToLower();
if (File.Exists(url))
{
AssetBundle bundle = AssetBundle.LoadFromFile(url);
if (bundle != null)
{
bundleName = bundleName.Replace("lua/", "").Replace(".unity3d", "");
base.AddSearchBundle(bundleName.ToLower(), bundle);
}
}
}
/// <summary>
/// 当LuaVM加载Lua文件的时候这里就会被调用
/// 用户可以自定义加载行为只要返回byte[]即可。
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public override byte[] ReadFile(string fileName)
{
fileName = fileName.Replace(".lua", string.Empty);
if (AppConst.luaBundleMode)
{
return LoadFromAssetBundle(fileName);
}
else
{
return base.ReadFile(fileName);
}
}
byte[] LoadFromAssetBundle(string fileName)
{
string assetName = fileName.Replace("/", "%");
byte[] bytes = App.ResMgr.LoadAsset<TextAsset>(assetName).bytes;
//App.ResMgr.UnLoadAsset(game, assetName);
return bytes;
}
}
}