Example #1
0
    /// <summary>
    /// 生成LuaPanel
    /// </summary>
    /// <param name="document"></param>
    private void GetLuaPanelScript(LuaDocumentNode document)
    {
        var panelName  = $"{gameObject.name}Panel";
        var luaUIPanel = gameObject.GetComponent <MLuaUIPanel>();

        if (luaUIPanel == null)
        {
            EditorUtility.DisplayDialog("导出错误", "目标找不到LuaUIPanel", "ok");
            return;
        }

        document.ModelNode = new LuaModelNode($"UI.{panelName}");
        document.RemoveFunction("Bind");

        //Bind函数
        var bindFunction = new LuaFunctionNode("Bind", LuaMemberType.Global, new List <string>()
        {
            "ctrl"
        },
                                               new List <LuaBaseStatementNode>());

        document.AddFunction(bindFunction);

        bindFunction.statementNodes.Add(new LuaScriptStatementNode("--dont override this function"));
        bindFunction.statementNodes.Add(new LuaScriptStatementNode("local l_panel = {}"));
        bindFunction.statementNodes.Add(new LuaScriptStatementNode("l_panel.PanelRef = ctrl.uObj:GetComponent(\"MLuaUIPanel\")"));

        Dictionary <string, string> comRefsCodes = LuaUIGroupEditor.GetComRefsCodes(luaUIPanel.ComRefs);

        foreach (var item in comRefsCodes)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("l_panel.");
            builder.Append(item.Key);
            if (!string.IsNullOrEmpty(item.Value))
            {
                builder.Append(" = l_panel.PanelRef.");
                builder.Append(item.Value);
            }
            var getComRefStatement = new LuaScriptStatementNode(builder.ToString());
            bindFunction.statementNodes.Add(getComRefStatement);
        }

        var groupsCodes = LuaUIGroupEditor.GetGroupsCodes(luaUIPanel.Groups, "l_panel.", " = l_panel.PanelRef.");

        foreach (var item in groupsCodes)
        {
            var getComRefStatement = new LuaScriptStatementNode(item.Key + item.Value);
            bindFunction.statementNodes.Add(getComRefStatement);
        }

        //返回代码
        bindFunction.statementNodes.Add(new LuaScriptStatementNode("return l_panel"));

        MFileEx.SaveText(document.ToString(), LuaPanelPath);
    }
Example #2
0
    //生成Template的lua代码
    private void GetLuaUITemplateScript(LuaDocumentNode document)
    {
        document.ModelNode = new LuaModelNode("UITemplate");
        document.AddRequire(new LuaRequireNode("UI/BaseUITemplate"));
        document.ClassName          = group.ClassName;
        document.ClassInitStatement = new LuaScriptStatementNode($"class(\"{group.ClassName}\", super)");
        document.AddField(new LuaFieldNode("super", LuaMemberType.Local, new LuaScriptStatementNode("UITemplate.BaseUITemplate")));

        document.RemoveFunction("ParameterDeclarations");

        var SetDataFunc = new LuaFunctionNode("SetData", LuaMemberType.Local, new List <string>()
        {
            "data"
        });
        var InitFunc       = new LuaFunctionNode("Init", LuaMemberType.Local);
        var OnDestroyFunc  = new LuaFunctionNode("OnDestroy", LuaMemberType.Local);
        var OnDeActiveFunc = new LuaFunctionNode("OnDeActive", LuaMemberType.Local);

        var ParameterDeclarationsFunction = new LuaFunctionNode("ParameterDeclarations", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        ParameterDeclarationsFunction.statementNodes.Add(new LuaScriptStatementNode("self.Parameter.LuaUIGroup = self:transform():GetComponent(\"MLuaUIGroup\")"));

        var groupCodes = GetGroupCodes(group, "self.Parameter.", " = self.Parameter.LuaUIGroup.");

        foreach (var item in groupCodes)
        {
            var getComRefStatement = new LuaScriptStatementNode(item.Key + item.Value);
            ParameterDeclarationsFunction.statementNodes.Add(getComRefStatement);
        }

        document.AddFunction(InitFunc);
        document.AddFunction(OnDestroyFunc);
        document.AddFunction(OnDeActiveFunc);
        document.AddFunction(SetDataFunc);
        document.AddFunction(ParameterDeclarationsFunction);

        MFileEx.SaveText(document.ToString(), LuaTemplatePath);
    }
Example #3
0
    /// <summary>
    /// 生成LuaCtrl
    /// </summary>
    /// <param name="document"></param>
    private void GetLuaCtrlScript(LuaDocumentNode document)
    {
        var panelName       = $"{gameObject.name}Panel";
        var ctrlName        = $"{gameObject.name}Ctrl";
        var defultModelName = $"{gameObject.name}Model";

        var luaUIPanel = gameObject.GetComponent <MLuaUIPanel>();

        if (luaUIPanel == null)
        {
            EditorUtility.DisplayDialog("导出错误", "目标找不到LuaUIPanel", "欧克");
            return;
        }

        //不再覆盖
        //document.RemoveFunction("BindEvents");
        //document.RemoveFunction("UnBindEvents");

        document.AddRequire(new LuaRequireNode("UI/UIBaseCtrl"));
        document.AddRequire(new LuaRequireNode($"UI/Panel/{panelName}"));

        document.ModelNode = new LuaModelNode("UI");

        document.ClassName          = ctrlName;
        document.ClassInitStatement = new LuaScriptStatementNode($"class(\"{ctrlName}\", super)");
        document.AddField(new LuaFieldNode("super", LuaMemberType.Local, new LuaScriptStatementNode("UI.UIBaseCtrl")));

        var ctorFunc = new LuaFunctionNode("ctor", LuaMemberType.Local, null, new List <LuaBaseStatementNode>()
        {
            new LuaScriptStatementNode($"super.ctor(self, CtrlNames.{gameObject.name}, UILayer.{uiLayer.ToString()}, nil, ActiveType.{activeType.ToString()})")
        });

        var initFunc = new LuaFunctionNode("Init", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        initFunc.statementNodes.Add(new LuaScriptStatementNode($"self.panel = UI.{panelName}.Bind(self)"));

        var unInitFunc = new LuaFunctionNode("Uninit", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        unInitFunc.statementNodes.Add(new LuaScriptStatementNode("super.Uninit(self)"));
        unInitFunc.statementNodes.Add(new LuaScriptStatementNode("self.panel = nil"));

        LuaFunctionNode setupHandlerFunc = null;

        if (luaUIPanel.HandlerRef != null)
        {
            setupHandlerFunc = new LuaFunctionNode("SetupHandlers", LuaMemberType.Local, null, new List <LuaBaseStatementNode>()
            {
                new LuaScriptStatementNode("super.SetupHandlers(self)"),
                new LuaScriptStatementNode($"self:SetHandlerToggleTpl(self.panel.{luaUIPanel.HandlerRef.Name})"),
                new LuaScriptStatementNode("--self:EnsureCreateHandler(xxx, xxx)")
            });
        }

        var onActiveFunc = new LuaFunctionNode("OnActive", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        var onDeActiveFunc = new LuaFunctionNode("OnDeActive", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        var updateFunc = new LuaFunctionNode("Update", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        var refreshFunc = new LuaFunctionNode("Refresh", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        var logoutFunc = new LuaFunctionNode("OnLogout", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());

        var reconnectedFunc = new LuaFunctionNode("OnReconnected", LuaMemberType.Local, new List <string>()
        {
            "roleData"
        }, new List <LuaBaseStatementNode>());

        var showFunc = new LuaFunctionNode("Show", LuaMemberType.Local, null, new List <LuaBaseStatementNode>()
        {
            new LuaScriptStatementNode("super.Show(self)")
        });

        var hideFunc = new LuaFunctionNode("Hide", LuaMemberType.Local, null, new List <LuaBaseStatementNode>()
        {
            new LuaScriptStatementNode("super.Hide(self)")
        });

        var bindFunc = new LuaFunctionNode("BindEvents", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());
        //bindFunc.statementNodes.Add(new LuaScriptStatementNode("--dont override this function"));
        var unbindFunc = new LuaFunctionNode("UnBindEvents", LuaMemberType.Local, null, new List <LuaBaseStatementNode>());
        //unbindFunc.statementNodes.Add(new LuaScriptStatementNode("--dont override this function"));

        Dictionary <string, int> comCountNumber = new Dictionary <string, int>();
        List <string>            unbindEvent    = new List <string>();

        for (int i = 0; i < luaUIPanel.ComRefs.Length; i++)
        {
            var comRef = luaUIPanel.ComRefs[i];
            if (comRef.BindCom == null || string.IsNullOrEmpty(comRef.DataName) || !comRef.GenBindScript)
            {
                continue;
            }
            var modelName = comRef.ModelName ?? defultModelName;

            document.AddRequire(new LuaRequireNode($"Data/Model/{modelName}"));

            var eventNameFieldName = $"{modelName}.{comRef.DataName.ToUpper()}";

            if (comRef.IsArray)
            {
                if (!comCountNumber.ContainsKey(comRef.Name))
                {
                    comCountNumber[comRef.Name] = 0;
                }
                comCountNumber[comRef.Name] = comCountNumber[comRef.Name] + 1;
                bindFunc.statementNodes.Add(new LuaScriptStatementNode(
                                                $"self:Bind(Data.{eventNameFieldName}, self.panel.{comRef.Name}[{comCountNumber[comRef.Name]}])"));
            }
            else
            {
                bindFunc.statementNodes.Add(new LuaScriptStatementNode($"self:Bind(Data.{eventNameFieldName}, self.panel.{comRef.Name})"));
            }
            if (!unbindEvent.Contains(eventNameFieldName))
            {
                unbindFunc.statementNodes.Add(new LuaScriptStatementNode($"self:UnBind(Data.{eventNameFieldName})"));
                unbindEvent.Add(eventNameFieldName);
            }
        }

        document.AddFunction(ctorFunc);
        document.AddFunction(initFunc);
        if (setupHandlerFunc != null)
        {
            document.AddFunction(setupHandlerFunc);
        }
        document.AddFunction(unInitFunc);
        document.AddFunction(onActiveFunc);
        document.AddFunction(onDeActiveFunc);
        document.AddFunction(updateFunc);
        document.AddFunction(refreshFunc);
        document.AddFunction(logoutFunc);
        document.AddFunction(reconnectedFunc);
        document.AddFunction(showFunc);
        document.AddFunction(hideFunc);
        document.AddFunction(bindFunc);
        document.AddFunction(unbindFunc);

        MFileEx.SaveText(document.ToString(), LuaCtrlPath);
    }