SLua 优化初始化速度

发表于2017-11-24
评论0 3.5k浏览

导出很多类到 Lua中,直接造成游戏打开的时候初始化 SLua 花费了 3-4 秒,为此,下面就给大家介绍下SLua如何优化初始化速度。


对项目中的 SLua 进行了两个优化:

1、导出的类中,没有在 Lua 中使用的 Public 函数、变量 ,全部添加   [SLua.DoNotToLua]  属性。


2、在 LuaSvr 中,有下面这样一段代码:

static void bindAll(IntPtr l)  
{  
    // add RELEASE macro to switch on below codes  
#if RELEASE && (UNITY_IOS || UNITY_ANDROID)  
    BindUnity.Bind(l);  
    BindUnityUI.Bind(l); // delete this line if not found  
    BindDll.Bind(l); // delete this line if not found  
    BindCustom.Bind(l);   
#else  
    Assembly[] ams = AppDomain.CurrentDomain.GetAssemblies();  
    List<Type> bindlist = new List<Type>();  
    foreach(Assembly a in ams)   
    {  
        Type[] ts=a.GetExportedTypes();  
        foreach (Type t in ts)  
        {  
            if (t.GetCustomAttributes(typeof(LuaBinderAttribute),false).Length > 0)  
            {  
                bindlist.Add(t);  
            }  
        }  
    }  
    bindlist.Sort( new System.Comparison<Type>((Type a,Type b) =>  
    {  
        LuaBinderAttribute la = (LuaBinderAttribute)a.GetCustomAttributes(typeof(LuaBinderAttribute),false)[0];  
        LuaBinderAttribute lb = (LuaBinderAttribute)b.GetCustomAttributes(typeof(LuaBinderAttribute),false)[0];  
        return la.order.CompareTo(lb.order);  
    })  
    );  
    foreach (Type t in bindlist)  
    {  
        t.GetMethod("Bind").Invoke(null, new object[] { l });  
    }  
#endif  
}  

所以可以 添加 RELEASE 宏定义 。


处理完以后,大家会发现初始化速度加快了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: