/// <summary> /// 创建ui模板代码/自动引用 /// </summary> /// <param name="obj"></param> /// <param name="uiPrefabPath">Assets/Prefabs/Windows/MainPanel.prefab</param> private void CreateCode(GameObject obj, string uiPrefabPath) { if (obj != null) { PrefabType type = PrefabUtility.GetPrefabType(obj); if (PrefabType.Prefab != type) { return; } GameObject clone = PrefabUtility.InstantiatePrefab(obj) as GameObject; if (clone == null) { return; } mPanelCodeData = new PanelCodeData(); mPanelCodeData.mPanelName = clone.name.Replace("(clone)", string.Empty); // 找到所有mark标记 FindAllMarkTrans(clone.transform, string.Empty); // 创建脚本 CreateUIPanelCode(clone, uiPrefabPath); // 记录预制体路径 AddSerializeUIPrefab(uiPrefabPath); GameObject.DestroyImmediate(clone); } }
public static void Generate(string filePath, string scriptName, PanelCodeData panelCodeData) { StreamWriter sw = new StreamWriter(filePath, false, new UTF8Encoding(false)); StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine("using System.Collections;"); strBuilder.AppendLine("using System.Collections.Generic;"); strBuilder.AppendLine("using UnityEngine;"); strBuilder.AppendLine(); strBuilder.AppendFormat("public partial class {0}", scriptName); strBuilder.AppendLine(); strBuilder.AppendLine("{"); // mark组件 for (int i = 0; i < panelCodeData.mMarkObjInfos.Count; i++) { string strUIType = panelCodeData.mMarkObjInfos[i].mMarkObj.mComponentTypeName; strBuilder.AppendFormat("\tpublic {0} {1};\r\n", strUIType, UICodeGenerator.mPreFormat + panelCodeData.mMarkObjInfos[i].mName); if (i < (panelCodeData.mMarkObjInfos.Count - 1)) { strBuilder.AppendLine(); } } strBuilder.AppendLine("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
public void Generate(string generateFilePath, string behaviourName, string nameSpace, PanelCodeData panelCodeData) { var sw = new StreamWriter(generateFilePath, false, new UTF8Encoding(false)); 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("namespace " + nameSpace); strBuilder.AppendLine("{"); strBuilder.AppendLine("\tusing UnityEngine;"); strBuilder.AppendLine("\tusing UnityEngine.UI;"); strBuilder.AppendLine(); strBuilder.AppendFormat("\tpublic partial class {0}\n", behaviourName); strBuilder.AppendLine("\t{"); strBuilder.AppendFormat("\t\tpublic const string NAME = \"{0}\";", behaviourName).AppendLine(); strBuilder.AppendLine(); foreach (var objInfo in panelCodeData.MarkedObjInfos) { var strUIType = objInfo.MarkObj.ComponentName; strBuilder.AppendFormat("\t\t[SerializeField] public {0} {1};\r\n", strUIType, objInfo.Name); } strBuilder.AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void ClearUIComponents()"); strBuilder.Append("\t\t").AppendLine("{"); foreach (var markInfo in panelCodeData.MarkedObjInfos) { strBuilder.AppendFormat("\t\t\t{0} = null;\r\n", markInfo.Name); } strBuilder.AppendLine("\t\t\tmData = null;"); strBuilder.Append("\t\t").AppendLine("}"); strBuilder.AppendLine(); strBuilder.AppendFormat("\t\tprivate {0}Data mPrivateData = null;\n", behaviourName); strBuilder.AppendLine(); strBuilder.AppendFormat("\t\tpublic {0}Data mData\n", behaviourName); strBuilder.AppendLine("\t\t{"); strBuilder.Append("\t\t\tget { return mPrivateData ?? (mPrivateData = new ").Append(behaviourName) .Append("Data()); }") .AppendLine(); strBuilder.AppendLine("\t\t\tset"); strBuilder.AppendLine("\t\t\t{"); strBuilder.AppendLine("\t\t\t\tmUIData = value;"); strBuilder.AppendLine("\t\t\t\tmPrivateData = value;"); strBuilder.AppendLine("\t\t\t}"); strBuilder.AppendLine("\t\t}"); strBuilder.AppendLine("\t}"); strBuilder.AppendLine("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
public void Generate(string generateFilePath, string behaviourName, string nameSpace, PanelCodeData panelCodeData) { var sw = new StreamWriter(generateFilePath, false, new UTF8Encoding(false)); StringBuilder 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 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.Append("\t").AppendFormat("public class {0}Data : UIPanelData", behaviourName).AppendLine(); strBuilder.Append("\t").AppendLine("{"); strBuilder.Append("\t\t").AppendLine("// TODO: Query Mgr's Data"); strBuilder.Append("\t").AppendLine("}"); strBuilder.AppendLine(); strBuilder.AppendFormat("\tpublic partial class {0} : UILuaPanel", behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("\t{"); strBuilder.Append("\t\t").AppendLine("protected override void ProcessMsg (int eventId,QMsg msg)"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t\t").AppendLine("throw new System.NotImplementedException ();"); strBuilder.Append("\t\t").AppendLine("}") .AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void OnInit(IUIData uiData = null)"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t").Append("\t") .AppendLine("mData = uiData as " + behaviourName + "Data ?? new " + behaviourName + "Data();"); strBuilder.Append("\t\t").Append("\t").AppendLine("//please add init code here"); strBuilder.Append("\t\t").Append("\t").AppendLine("BindLuaComponent();"); strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void OnOpen(IUIData uiData = null)"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void OnShow()"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void OnHide()"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.Append("\t\t").AppendLine("protected override void OnClose()"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t").AppendLine("}").AppendLine(); strBuilder.Append("\t\t").AppendLine("void ShowLog(string content)"); strBuilder.Append("\t\t").AppendLine("{"); strBuilder.Append("\t\t\t").AppendFormat("Debug.Log(\"[ {0}:]\" + content);", behaviourName).AppendLine(); strBuilder.Append("\t\t").AppendLine("}"); strBuilder.Append("\t}").AppendLine(); strBuilder.Append("}"); sw.Write(strBuilder); sw.Flush(); sw.Close(); }
public void Generate(string generateFilePath, string behaviourName, string nameSpace, PanelCodeData panelCodeData) { var sw = new StreamWriter(generateFilePath, false, new UTF8Encoding(false)); var strBuilder = new StringBuilder(); strBuilder.AppendLine("--============================================================================="); strBuilder.AppendFormat("-- {0}.{1} {2}\n", DateTime.Now.Year, DateTime.Now.Month, SystemInfo.deviceName); strBuilder.AppendLine("--============================================================================="); strBuilder.AppendFormat("local {0} = class(\"{0}\",LuaBehaviour)\n", behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("--===== 初始化流程:注意Awake方法不要重写 ====="); strBuilder.AppendFormat("function {0}:BindUI()\n", behaviourName); foreach (var objInfo in panelCodeData.MarkedObjInfos) { var strUIType = objInfo.MarkObj.ComponentName; strBuilder.AppendFormat("\tself.{0} = self:Find(self.gameObject,\"{1}\");\r\n", objInfo.Name, objInfo.PathToElement); } strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendFormat("function {0}:RegisterUIEvent()\n", behaviourName); foreach (var objInfo in panelCodeData.MarkedObjInfos) { var strUIType = objInfo.MarkObj.ComponentName; var qUIHelperFunc = GetQUIFunctionNameByUIType(strUIType); if (qUIHelperFunc.IsNotNullAndEmpty()) { strBuilder.AppendLine(); } strBuilder.AppendFormat("\t{0}(self.{1},function()\n", qUIHelperFunc, objInfo.Name); strBuilder.AppendLine(); strBuilder.AppendLine("\tend)"); } strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendLine("--===== Behaviour生命周期函数 ====="); strBuilder.AppendFormat("function {0}:OnEnable()\n", behaviourName); // strBuilder.AppendFormat("\tlog(\"{0}:OnEnable\")\n",behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendFormat("function {0}:Start()\n", behaviourName); // strBuilder.AppendFormat("\tlog(\"{0}:Start\")\n",behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendFormat("function {0}:Update()\n", behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendFormat("function {0}:OnDisable()\n", behaviourName); // strBuilder.AppendFormat("\tlog(\"{0}:OnDisable\")\n",behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("end"); strBuilder.AppendLine(); strBuilder.AppendFormat("function {0}:OnDestroy()\n", behaviourName); // strBuilder.AppendFormat("\tlog(\"{0}:OnDestroy\")\n",behaviourName); strBuilder.AppendLine(); strBuilder.AppendLine("end"); strBuilder.AppendLine("--================================"); strBuilder.AppendFormat("return {0}.new();", behaviourName); sw.Write(strBuilder); sw.Flush(); sw.Close(); }