| New file |
| | |
| | | fileFormatVersion: 2 |
| | | guid: 722e5e3d30096674e811f5bd191246a0 |
| | | folderAsset: yes |
| | | timeCreated: 1539228128 |
| | | licenseType: Pro |
| | | DefaultImporter: |
| | | userData: |
| | | assetBundleName: |
| | | assetBundleVariant: |
| New file |
| | |
| | | using UnityEngine; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using XLua; |
| | | using System; |
| | | using System.IO; |
| | | |
| | | [LuaCallCSharp] |
| | | public class LuaBehaviour : MonoBehaviour |
| | | { |
| | | public string fileName; |
| | | private Action luaOnEnable; |
| | | private Action luaStart; |
| | | private Action luaUpdate; |
| | | private Action luaOnDisable; |
| | | private Action luaOnDestroy; |
| | | |
| | | private LuaTable table; |
| | | |
| | | void Awake() |
| | | { |
| | | table = LuaUtility.GetNewTable(); |
| | | var meta = LuaUtility.GetNewTable(); |
| | | meta.Set("__index", LuaUtility.Global); |
| | | table.SetMetaTable(meta); |
| | | meta.Dispose(); |
| | | |
| | | table.Set("this", this); |
| | | LuaUtility.DoString(fileName, "LuaBehaviour", table); |
| | | |
| | | var luaAwake = table.Get<Action>("Awake"); |
| | | table.Get("OnEnable", out luaOnEnable); |
| | | table.Get("Start", out luaStart); |
| | | table.Get("Update", out luaUpdate); |
| | | table.Get("OnDisable", out luaOnDisable); |
| | | table.Get("OnDestroy", out luaOnDestroy); |
| | | |
| | | if (luaAwake != null) |
| | | { |
| | | luaAwake(); |
| | | } |
| | | } |
| | | |
| | | private void OnEnable() |
| | | { |
| | | if (luaOnEnable != null) |
| | | { |
| | | luaOnEnable(); |
| | | } |
| | | } |
| | | |
| | | void Start() |
| | | { |
| | | if (luaStart != null) |
| | | { |
| | | luaStart(); |
| | | } |
| | | } |
| | | |
| | | void Update() |
| | | { |
| | | if (luaUpdate != null) |
| | | { |
| | | luaUpdate(); |
| | | } |
| | | } |
| | | |
| | | private void OnDisable() |
| | | { |
| | | if (luaOnDisable != null) |
| | | { |
| | | luaOnDisable(); |
| | | } |
| | | } |
| | | |
| | | void OnDestroy() |
| | | { |
| | | if (luaOnDestroy != null) |
| | | { |
| | | luaOnDestroy(); |
| | | } |
| | | |
| | | luaOnDisable = null; |
| | | luaOnEnable = null; |
| | | luaOnDestroy = null; |
| | | luaUpdate = null; |
| | | luaStart = null; |
| | | table.Dispose(); |
| | | } |
| | | } |
| New file |
| | |
| | | fileFormatVersion: 2 |
| | | guid: 028ad9d0c327569419d2e22210b67d2d |
| | | timeCreated: 1540173220 |
| | | licenseType: Pro |
| | | MonoImporter: |
| | | serializedVersion: 2 |
| | | defaultReferences: [] |
| | | executionOrder: 0 |
| | | icon: {instanceID: 0} |
| | | userData: |
| | | assetBundleName: |
| | | assetBundleVariant: |
| | |
| | | |
| | | public class LuaUtility : SingletonMonobehaviour<LuaUtility> |
| | | { |
| | | internal static LuaEnv env = new LuaEnv(); |
| | | public readonly static LuaEnv env = new LuaEnv(); |
| | | static float lastGCTime = 0; |
| | | const float GCInterval = 1;//1 second |
| | | |
| | | static LuaUtility() |
| | | { |
| | | env.AddLoader(new LuaEnv.CustomLoader(LoadAsset)); |
| | | env.AddLoader(new LuaEnv.CustomLoader(LoadAssetBytes)); |
| | | } |
| | | |
| | | public static LuaTable GetNewTable() |
| | | { |
| | | return env.NewTable(); |
| | | } |
| | | |
| | | public static LuaTable Global { |
| | | get { return env.Global; } |
| | | } |
| | | |
| | | public void Init() |
| | |
| | | env.DoString(command, chunkName, _table); |
| | | } |
| | | |
| | | private static byte[] LoadAsset(ref string _fileName) |
| | | 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; |
| | | 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); |
| | | path = AssetVersionUtility.GetAssetFilePath(StringUtility.Contact("lua/", _fileName, ".lua")); |
| | | } |
| | | |
| | | bytes = System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path)); |
| | | return bytes; |
| | | return File.ReadAllText(path); |
| | | } |
| | | |
| | | private static byte[] LoadAssetBytes(ref string _fileName) |
| | | { |
| | | return System.Text.Encoding.UTF8.GetBytes(LoadAsset(ref _fileName)); |
| | | } |
| | | } |
| | |
| | | Action onActived; |
| | | Action onLateUpdate; |
| | | |
| | | private LuaTable scriptEnv; |
| | | private LuaTable table; |
| | | |
| | | private void Init() |
| | | { |
| | | scriptEnv = LuaUtility.env.NewTable(); |
| | | LuaTable meta = LuaUtility.env.NewTable(); |
| | | meta.Set("__index", LuaUtility.env.Global); |
| | | scriptEnv.SetMetaTable(meta); |
| | | table = LuaUtility.GetNewTable(); |
| | | var meta = LuaUtility.GetNewTable(); |
| | | meta.Set("__index", LuaUtility.Global); |
| | | table.SetMetaTable(meta); |
| | | meta.Dispose(); |
| | | |
| | | LuaUtility.Do(string.Format("window.{0}", this.gameObject.name), this.gameObject.name, scriptEnv); |
| | | LuaUtility.Do(string.Format("window.{0}", this.gameObject.name), this.gameObject.name, table); |
| | | |
| | | scriptEnv.Get("BindController", out onBindController); |
| | | scriptEnv.Get("AddListeners", out onAddListeners); |
| | | scriptEnv.Get("OnPreOpen", out onPreOpen); |
| | | scriptEnv.Get("OnAfterOpen", out onAfterOpen); |
| | | scriptEnv.Get("OnPreClose", out onPreClose); |
| | | scriptEnv.Get("OnAfterClose", out onAfterClose); |
| | | scriptEnv.Get("OnActived", out onActived); |
| | | scriptEnv.Get("LateUpdate", out onLateUpdate); |
| | | table.Get("BindController", out onBindController); |
| | | table.Get("AddListeners", out onAddListeners); |
| | | table.Get("OnPreOpen", out onPreOpen); |
| | | table.Get("OnAfterOpen", out onAfterOpen); |
| | | table.Get("OnPreClose", out onPreClose); |
| | | table.Get("OnAfterClose", out onAfterClose); |
| | | table.Get("OnActived", out onActived); |
| | | table.Get("LateUpdate", out onLateUpdate); |
| | | |
| | | scriptEnv.Get("OnDestroy", out luaOnDestroy); |
| | | table.Get("OnDestroy", out luaOnDestroy); |
| | | } |
| | | |
| | | void OnDestroy() |
| | |
| | | onActived = null; |
| | | onLateUpdate = null; |
| | | |
| | | if (scriptEnv != null) |
| | | if (table != null) |
| | | { |
| | | scriptEnv.Dispose(); |
| | | table.Dispose(); |
| | | } |
| | | } |
| | | |