using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using XLua;
|
using System.IO;
|
|
public class LuaUtility : SingletonMonobehaviour<LuaUtility>
|
{
|
internal static LuaEnv env = new LuaEnv();
|
static float lastGCTime = 0;
|
const float GCInterval = 1;//1 second
|
|
static LuaUtility()
|
{
|
env.AddLoader(new LuaEnv.CustomLoader(LoadAsset));
|
}
|
|
public void Init()
|
{
|
Do("custom.ButtonUtil");
|
Do("global.logex");
|
|
lastGCTime = 0f;
|
}
|
|
void Update()
|
{
|
if (Time.time - lastGCTime > GCInterval)
|
{
|
env.Tick();
|
lastGCTime = Time.time;
|
}
|
}
|
|
public static void Do(string _file, string chunkName = "chunk", LuaTable _table = null)
|
{
|
var command = string.Format("require '{0}'", _file);
|
env.DoString(command, chunkName, _table);
|
}
|
|
private static byte[] LoadAsset(ref string _fileName)
|
{
|
_fileName = _fileName.Replace('.', '/');
|
|
var path = string.Empty;
|
byte[] bytes = null;
|
|
if (AssetSource.luaFromEditor)
|
{
|
path = ResourcesPath.LUA_FODLER + "/" + _fileName + ".lua";
|
}
|
else
|
{
|
var assetVersion = AssetVersionUtility.GetAssetVersion(StringUtility.Contact("lua/", _fileName, ".lua"));
|
path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, assetVersion.relativePath);
|
}
|
|
bytes = System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
|
return bytes;
|
}
|
|
}
|