LuaFramework 框架下的Lua组件的实现

LuaComponent .cs

/* bo 2018-06-21 15:16:11 
 * 注意事项:
 * 1.必须在lua框架即LuaManager启动后使用
 * 2.lua类似于C#组件,通过C#转发达到调用组件方法的目的,方便用于对目前大量的C#UI进行一定能力的热更
 * 3.考虑调用性能问题,暂时不考虑Update系列方法
 * 4.LUA脚本路径的根路径为lua,此处路径填写lua文件夹下的文件路径,如:XXX | projx/UI/XXX
 * 5.约定lua形式如下:
local TestLuaComponent = {}

function TestLuaComponent:Awake( obj )
    -- body
    print("Awake:"..obj.name)
end

function TestLuaComponent:OnEnable( obj )
    -- body
    print("OnEnable:"..obj.name)
end

function TestLuaComponent:Start( obj )
    -- body
    print("Start:"..obj.name)
end

function TestLuaComponent:OnDisable()
    -- body
    print("OnDisable")
end

function TestLuaComponent:OnDestory()
    -- body
    print("OnDestory")
end

return TestLuaComponent
 */

using UnityEngine;
using LuaInterface;
using LuaFramework;
[AddComponentMenu("Lua/LuaComponent")]
public class LuaComponent : MonoBehaviour
{
    [Header("如:XXX | projx/UI/XXX")]
    public string m_luaPath;

    public LuaTable LuaModule = null;

    void RunLuaFile(string luaPath)
    {
        //必须先初始化LuaManager
        object[] luaRet = LuaManager.GetInstance().DoFile(luaPath);
        if (luaRet != null && luaRet.Length >= 1)
        {
            // 约定:第一个返回的Table对象作为Lua模块
            LuaModule = luaRet[0] as LuaTable;
        }
        else
        {
            UtilLog.LogError("Lua脚本没有返回Table对象");
        }
    }

    void Awake()
    {
        if (string.IsNullOrEmpty(m_luaPath))
        {
            UtilLog.LogError("Lua脚本路径不能为空");
            return;
        }
        RunLuaFile(m_luaPath);
        CallLuaFunction("Awake", LuaModule, gameObject);
    }

    void OnEnable()
    {
        CallLuaFunction("OnEnable", LuaModule, gameObject);
    }

    void Start()
    {
        CallLuaFunction("Start", LuaModule, gameObject);
    }

    void OnDisable()
    {
        CallLuaFunction("OnDisable", LuaModule);
    }

    void OnDestory()
    {
        CallLuaFunction("OnDestory", LuaModule);

        if (LuaModule != null)
        {
            LuaModule.Dispose();
            LuaModule = null;
        }
    }

    void CallLuaFunction(string funcName, params object[] args)
    {
        if (LuaModule == null)
            return;

        LuaFunction func = LuaModule[funcName] as LuaFunction;
        if (func != null)
            func.LazyCall(args);
    }
}


LuaManager.cs

using UnityEngine;
using System.Collections;
using LuaInterface;

namespace LuaFramework
{
    /// <summary>
    /// Lua对外调用接口
    /// </summary>
    public class LuaManager : LuaManagerBase
    {
        private static LuaManager instance = null;

        public static LuaManager GetInstance()
        {
            return instance;
        }
        static bool _isInitLua;
        void Start()
        {
            if(_isInitLua)
            {
                GameObject.Destroy(gameObject);
                return;
            }
            _isInitLua = true;
            instance = this;
            GameObject.DontDestroyOnLoad(gameObject);
            InintLua();
        }

        private void InintLua()
        {
            //初始化Lua
            if (AppConst.LuaBundleMode)
            {
                UtilLog.Log("初始化LuaAB");
                LuaLoader loader = new LuaLoader();
                loader.AddBundle("lua/lua.ab");
                loader.AddBundle("lua/lua_system.ab");
                loader.AddBundle("lua/lua_system_reflection.ab");
                loader.AddBundle("lua/lua_unityengine.ab");
                loader.AddBundle("lua/lua_misc.ab");
                loader.AddBundle("lua/lua_protobuf.ab");
                loader.AddBundle("lua/lua_3rd_cjson.ab");
                loader.AddBundle("lua/lua_3rd_luabitop.ab");
                loader.AddBundle("lua/lua_3rd_pbc.ab");
                loader.AddBundle("lua/lua_3rd_pblua.ab");
                loader.AddBundle("lua/lua_3rd_sproto.ab");
                //自定义的AB
                loader.AddBundle("lua/lua_projx_ui.ab");
                loader.AddBundle("lua/lua_projx_model.ab");
            }

            DelegateFactory.Init();

            this.Init();
            //初始化的lua脚本
            this.DoFile("init.lua");
        }

        void OnApplicationQuit()
        {
            this.Close();
        }
    }
}

AddBundle方法

        public void AddBundle(string bundleName)
        {
            //调整思路,如果 Application.persistentDataPath + "/" 路径下没有,再寻找 Application.streamingAssetsPath(android不一样)
            string url = Util.DataPath + bundleName.ToLower();
            if (File.Exists(url))
            {
                AssetBundle bundle = AssetBundle.LoadFromFile(url);
                if (bundle != null)
                {
                    bundleName = bundleName.Replace("lua/", "").Replace(".ab", "");
                    base.AddSearchBundle(bundleName.ToLower(), bundle);
                }
                return;
            }

            string url_SA = Util.SAPath() + bundleName.ToLower();
            AssetBundle bundle_SA = AssetBundle.LoadFromFile(url_SA);
            if (bundle_SA != null)
            {
                bundleName = bundleName.Replace("lua/", "").Replace(".ab", "");
                base.AddSearchBundle(bundleName.ToLower(), bundle_SA);
            }
        }

猜你喜欢

转载自blog.csdn.net/u010314160/article/details/81067750
今日推荐