using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; using System.IO; public class LuaUtility : SingletonMonobehaviour { public readonly static LuaEnv env = new LuaEnv(); static float lastGCTime = 0; const float GCInterval = 1;//1 second static LuaUtility() { env.AddLoader(new LuaEnv.CustomLoader(LoadAssetBytes)); } public static LuaTable GetNewTable() { return env.NewTable(); } public static LuaTable Global { get { return env.Global; } } public void Init() { Do("global.util"); Do("custom.ButtonUtil"); Do("global.logex"); Do("global.functions"); Do("global.object"); 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); } public static void DoString(string _file, string chunkName = "chunk", LuaTable _table = null) { var content = LoadAsset(ref _file); env.DoString(content, chunkName, _table); } private static string LoadAsset(ref string _fileName) { _fileName = _fileName.Replace('.', '/'); var path = string.Empty; if (AssetSource.luaFromEditor) { path = ResourcesPath.LUA_FODLER + "/" + _fileName + ".lua"; } else { path = AssetVersionUtility.GetAssetFilePath(StringUtility.Contact("lua/", _fileName, ".lua")); } return File.ReadAllText(path); } private static byte[] LoadAssetBytes(ref string _fileName) { return System.Text.Encoding.UTF8.GetBytes(LoadAsset(ref _fileName)); } }