示例#1
0
        public static void Generate(string generateFilePath, string behaviourName, string nameSpace,
                                    ElementCodeInfo elementCodeInfo)
        {
            var sw         = new StreamWriter(generateFilePath, false, Encoding.UTF8);
            var strBuilder = new StringBuilder();

            strBuilder.AppendLine("/****************************************************************************");
            strBuilder.AppendFormat(" * {0}.{1} {2}\n", DateTime.Now.Year, DateTime.Now.Month, SystemInfo.deviceName);
            strBuilder.AppendLine(" ****************************************************************************/");
            strBuilder.AppendLine();
            strBuilder.AppendLine("using UnityEngine;");
            strBuilder.AppendLine("using UnityEngine.UI;");
            strBuilder.AppendLine("using QFramework;");
            strBuilder.AppendLine();
            strBuilder.AppendLine("namespace " + nameSpace);
            strBuilder.AppendLine("{");
            strBuilder.AppendFormat("\tpublic partial class {0}", behaviourName);
            strBuilder.AppendLine();
            strBuilder.AppendLine("\t{");

            foreach (var markInfo in elementCodeInfo.BindInfos)
            {
                var strUIType = markInfo.BindScript.ComponentName;
                strBuilder.AppendFormat("\t\t[SerializeField] public {0} {1};\r\n",
                                        strUIType, markInfo.Name);
            }

            strBuilder.AppendLine();

            strBuilder.Append("\t\t").AppendLine("public void Clear()");
            strBuilder.Append("\t\t").AppendLine("{");
            foreach (var markInfo in elementCodeInfo.BindInfos)
            {
                strBuilder.AppendFormat("\t\t\t{0} = null;\r\n",
                                        markInfo.Name);
            }

            strBuilder.Append("\t\t").AppendLine("}");
            strBuilder.AppendLine();

            strBuilder.Append("\t\t").AppendLine("public override string ComponentName");
            strBuilder.Append("\t\t").AppendLine("{");
            strBuilder.Append("\t\t\t");
            strBuilder.AppendLine("get { return \"" + elementCodeInfo.BindInfo.BindScript.ComponentName + "\";}");
            strBuilder.Append("\t\t").AppendLine("}");
            strBuilder.AppendLine("\t}");
            strBuilder.AppendLine("}");
            sw.Write(strBuilder);
            sw.Flush();
            sw.Close();
        }
        private static void CreateUIElementCode(string generateDirPath, ElementCodeInfo elementCodeInfo)
        {
            var panelFilePathWhithoutExt = generateDirPath + elementCodeInfo.BehaviourName;

            if (File.Exists(panelFilePathWhithoutExt + ".cs") == false)
            {
                UIElementCodeTemplate.Generate(panelFilePathWhithoutExt + ".cs",
                                               elementCodeInfo.BehaviourName, UIKitSettingData.GetProjectNamespace(), elementCodeInfo);
            }

            UIElementCodeComponentTemplate.Generate(panelFilePathWhithoutExt + ".Designer.cs",
                                                    elementCodeInfo.BehaviourName, UIKitSettingData.GetProjectNamespace(), elementCodeInfo);

            foreach (var childElementCodeData in elementCodeInfo.ElementCodeDatas)
            {
                var elementDir = (panelFilePathWhithoutExt + "/").CreateDirIfNotExists();
                CreateUIElementCode(elementDir, childElementCodeData);
            }
        }
示例#3
0
        public static void Generate(string generateFilePath, string behaviourName, string nameSpace,
                                    ElementCodeInfo elementCodeInfo)
        {
            var sw         = new StreamWriter(generateFilePath, false, new UTF8Encoding(false));
            var strBuilder = new StringBuilder();

            var markType = elementCodeInfo.BindInfo.BindScript.GetBindType();

            strBuilder.AppendLine("/****************************************************************************");
            strBuilder.AppendFormat(" * {0}.{1} {2}\n", DateTime.Now.Year, DateTime.Now.Month, SystemInfo.deviceName);
            strBuilder.AppendLine(" ****************************************************************************/");
            strBuilder.AppendLine();

            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Collections.Generic;");
            strBuilder.AppendLine("using UnityEngine;");
            strBuilder.AppendLine("using UnityEngine.UI;");
            strBuilder.AppendLine("using QFramework;").AppendLine();

            strBuilder.AppendLine("namespace " + nameSpace);
            strBuilder.AppendLine("{");
            strBuilder.AppendFormat("\tpublic partial class {0} : {1}", behaviourName,
                                    markType == BindType.Component ? "UIComponent" : "UIElement");
            strBuilder.AppendLine();
            strBuilder.AppendLine("\t{");
            strBuilder.Append("\t\t").AppendLine("private void Awake()");
            strBuilder.Append("\t\t").AppendLine("{");
            strBuilder.Append("\t\t").AppendLine("}");
            strBuilder.AppendLine();
            strBuilder.Append("\t\t").AppendLine("protected override void OnBeforeDestroy()");
            strBuilder.Append("\t\t").AppendLine("{");
            strBuilder.Append("\t\t").AppendLine("}");
            strBuilder.AppendLine("\t}");
            strBuilder.Append("}");

            sw.Write(strBuilder);
            sw.Flush();
            sw.Close();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="rootTrans"></param>
        /// <param name="curTrans"></param>
        /// <param name="transFullName"></param>
        public static void SearchBinds(Transform curTrans, string transFullName, PanelCodeInfo panelCodeInfo = null, ElementCodeInfo parentElementCodeInfo = null)
        {
            foreach (Transform childTrans in curTrans)
            {
                var uiMark = childTrans.GetComponent <IBind>();

                if (null != uiMark)
                {
                    if (null == parentElementCodeInfo)
                    {
                        if (!panelCodeInfo.BindInfos.Any(markedObjInfo => markedObjInfo.Name.Equals(uiMark.Transform.name)))
                        {
                            panelCodeInfo.BindInfos.Add(new BindInfo
                            {
                                Name          = uiMark.Transform.name,
                                BindScript    = uiMark,
                                PathToElement = CodeGenUtil.PathToParent(childTrans, panelCodeInfo.GameObjectName)
                            });
                            panelCodeInfo.DicNameToFullName.Add(uiMark.Transform.name, transFullName + childTrans.name);
                        }
                        else
                        {
                            Debug.LogError("Repeat key: " + childTrans.name);
                        }
                    }
                    else
                    {
                        if (!parentElementCodeInfo.BindInfos.Any(markedObjInfo => markedObjInfo.Name.Equals(uiMark.Transform.name)))
                        {
                            parentElementCodeInfo.BindInfos.Add(new BindInfo()
                            {
                                Name          = uiMark.Transform.name,
                                BindScript    = uiMark,
                                PathToElement = CodeGenUtil.PathToParent(childTrans, parentElementCodeInfo.BehaviourName)
                            });
                            parentElementCodeInfo.DicNameToFullName.Add(uiMark.Transform.name, transFullName + childTrans.name);
                        }
                        else
                        {
                            Debug.LogError("Repeat key: " + childTrans.name);
                        }
                    }


                    if (uiMark.GetBindType() != BindType.DefaultUnityElement)
                    {
                        var elementCodeData = new ElementCodeInfo
                        {
                            BehaviourName = uiMark.ComponentName,
                            BindInfo      = new BindInfo
                            {
                                BindScript = uiMark
                            }
                        };

                        if (null == parentElementCodeInfo)
                        {
                            panelCodeInfo.ElementCodeDatas.Add(elementCodeData);
                        }
                        else
                        {
                            parentElementCodeInfo.ElementCodeDatas.Add(elementCodeData);
                        }

                        SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, elementCodeData);
                    }
                    else
                    {
                        SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, parentElementCodeInfo);
                    }
                }
                else
                {
                    SearchBinds(childTrans, transFullName + childTrans.name + "/", panelCodeInfo, parentElementCodeInfo);
                }
            }
        }