static public void Export(string prefabPath, string scriptPath, string cellPath, string baseClassName = "HS_ViewBase", Transform root = null) { if (root == null) { GameObject c = GameObject.Find("UI").gameObject; if (c == null) { Debug.LogError("No UI Compent in Hierarchy."); return; } root = c.transform; } if (scriptPath.IndexOf("Assets/") != 0 || prefabPath.IndexOf("Assets/") != 0) { Debug.LogError("Path alway start with \"Assets/\" ."); return; } HS_Directory.CreateDirectory(prefabPath); for (int i = root.childCount - 1; i >= 0; --i) { Transform child = root.GetChild(i); if (child.gameObject.activeSelf && child.gameObject.CompareTag(VIEW_UI)) { string viewName = child.gameObject.name; mObjectNameIds.Clear(); mObjectNames.Clear(); mPathDict.Clear(); mPropertyDict.Clear(); mPropertyNameDict.Clear(); mUIListViewList.Clear(); ParseCanvas(child, child, ""); for (int j = 0; j < mUIListViewList.Count; j++) { HS_ListViewBase listView = mUIListViewList[j].GetComponent <HS_ListViewBase>(); if (listView != null) { GameObject cellPrefab = listView.GetCellPrefab(); if (cellPrefab == null) { string cellName = listView.GetCellPrefabName(); string cellPrefabPath = GetCellPathName(cellName, cellPath); Debug.Log("load path:" + cellPrefabPath); GameObject prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(cellPrefabPath, typeof(GameObject)) as GameObject; cellPrefab = GameObject.Instantiate(prefab) as GameObject; cellPrefab.transform.SetParent(listView.GetGridContent().transform); cellPrefab.transform.localScale = Vector3.one; cellPrefab.name = "CellPrefab"; } if (cellPrefab == null) { Debug.LogError("No cell prefab:" + mUIListViewList[j].name); break; } listView.SetCellPrefab(cellPrefab); ParseCanvas(cellPrefab.transform, cellPrefab.transform, ""); } else { mUIListViewList.RemoveAt(j--); } } string newBaseClassName = GenerateViewBaseClass(child, prefabPath, scriptPath + "/Base", cellPath, baseClassName); GenerateViewClass(scriptPath, viewName, newBaseClassName); } } Debug.Log("Export is completed"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
static private string GenerateViewBaseClass(Transform panel, string prefabPath, string scriptPath, string cellPath, string baseClassName) { // generate script StringBuilder code = new StringBuilder(); string className = "Base" + UpperFirstLetter(panel.name) + "View"; code.AppendLine("using System;"); code.AppendLine("using System.Collections;"); code.AppendLine("using System.Collections.Generic;"); code.AppendLine("using UnityEngine;"); code.AppendLine("using HS.Base;"); code.AppendLine("using HS.UI;"); code.AppendLine("using HS.Manager;"); code.AppendLine(""); code.AppendLine("public class " + className + " : " + baseClassName); code.AppendLine("{"); List <Property> properties; if (mPropertyDict.TryGetValue(panel, out properties)) { for (int i = 0; i < properties.Count; i++) { Property p = properties[i]; if (!p.isPrivate) { code.Append(TAB1).AppendLine(string.Format("protected {0} {1};", p.type, p.name)); } } } string viewPath = ""; int index = prefabPath.LastIndexOf("/Resources"); if (index > 0) { if (prefabPath[index + 10] == '/') { viewPath = prefabPath.Substring(index + 11); } } code.Append(TAB1).AppendLine(""); code.Append(TAB1).AppendLine("internal override GameObject GetViewPrefab()"); code.Append(TAB1).AppendLine("{"); code.Append(TAB2).AppendLine(string.Format("return HS_ResourceManager.LoadAsset<GameObject>(\"{0}\");", viewPath + (string.IsNullOrEmpty(viewPath) ? "" : "/") + panel.name)); code.Append(TAB1).AppendLine("}"); code.Append(TAB1).AppendLine(""); code.Append(TAB1).AppendLine("protected override void OnCreated()"); code.Append(TAB1).AppendLine("{"); code.Append(TAB2).AppendLine("base.OnCreated();"); code.Append(TAB1).AppendLine(""); code.Append(TAB2).AppendLine("Transform transform = this.transform;"); code.Append(TAB2).AppendLine("if (transform == HS_ViewManager.root.transform) return;"); if (properties != null) { foreach (Property p in properties) { code.Append(TAB2).AppendLine(""); System.Type type = p.type; string propName = "this." + p.name; if (p.isPrivate) { propName = p.name; code.Append(TAB2).AppendLine(string.Format("{2} {0} = HS_Base.FindProperty<{2}>(transform, \"{1}\");", propName, p.path, type)); } else { code.Append(TAB2).AppendLine(string.Format("{0} = HS_Base.FindProperty<{2}>(transform, \"{1}\");", propName, p.path, type)); } if (type == typeof(Button)) { code.Append(TAB2).AppendLine("this.RegisterButtonClickEvent (" + propName + ");"); } else if (type == typeof(Slider)) { code.Append(TAB2).AppendLine("this.RegisterSliderEvent (" + propName + ");"); } else if (type == typeof(Toggle)) { code.Append(TAB2).AppendLine("this.RegisterToggleEvent (" + propName + ");"); } else if (type == typeof(Dropdown)) { code.Append(TAB2).AppendLine("this.RegisterDropDownEvent (" + propName + ");"); } else if (type == typeof(InputField)) { code.Append(TAB2).AppendLine("this.RegisterInputFieldEvent (" + propName + ");"); } else if (type == typeof(Text)) { HS_LocalizationText localizationText = p.com.transform.GetComponent <HS_LocalizationText>(); if (localizationText != null && localizationText.languageKey != "") { //Localization.Format (); code.Append(TAB2).AppendLine("LocalizationText " + p.name + "LocalizationText = " + p.name + ".transform.GetComponent<LocalizationText> ();"); code.Append(TAB2).AppendLine(propName + ".text = Localization.Format (" + p.name + "LocalizationText.languageKey);"); } else { code.Append(TAB2).AppendLine(propName + ".text = \"" + Regex.Replace(((Text)p.com).text, "(\\\"|\\\\)", "\\$0") + "\";"); } } else if (type == typeof(HS_UIListView)) { code.Append(TAB2).AppendLine(propName + ".onInit += OnListViewInit;"); code.Append(TAB2).AppendLine(propName + ".onCellCreated += OnCellCreated;"); if (type == typeof(HS_UIListView)) { code.Append(TAB2).AppendLine(propName + ".onClick += OnListViewClick;"); code.Append(TAB2).AppendLine(propName + ".onSelected += OnListViewSelected;"); code.Append(TAB2).AppendLine(propName + ".onDeselected += OnListViewDeselected;"); } } } } code.Append(TAB1).AppendLine("}"); System.Action <Transform, string> generateCellClass = delegate(Transform cell, string name) { List <Property> props; if (!mPropertyDict.TryGetValue(cell, out props)) { props = new List <Property>(); } code.Append(TAB2).AppendLine("public class " + name); code.Append(TAB2).AppendLine("{"); foreach (Property p in props) { if (!p.isPrivate) { code.Append(TAB3).AppendLine(string.Format("public {0} {1};", p.type, p.name)); } } code.Append(TAB2).AppendLine("}"); code.Append(TAB2).AppendLine(""); }; System.Action <Transform, string, string, string> generateCellProperty = delegate(Transform cell, string cellClassName, string insName, string space) { List <Property> props; if (!mPropertyDict.TryGetValue(cell, out props)) { props = new List <Property>(); } code.Append(space).AppendLine(cellClassName + " " + insName + " = new " + cellClassName + "();"); foreach (Property p in props) { if (p.isPrivate) { code.Append(space).AppendLine(string.Format("{2} {0} = HS_Base.FindProperty<{2}>(t, \"{1}\");", p.name, p.path, p.type)); if (p.type == typeof(Text)) { code.Append(space).AppendLine(p.name + ".text = \"" + Regex.Replace(((Text)p.com).text, "(\\\"|\\\\)", "\\$0") + "\";"); } } else { code.Append(space).AppendLine(string.Format("{0}.{1} = HS_Base.FindProperty<{3}>(t, \"{2}\");", insName, p.name, p.path, p.type)); } } }; foreach (Transform t in mUIListViewList) { HS_ListViewBase listView = t.GetComponent <HS_ListViewBase>(); GameObject cellPrefab = listView.GetCellPrefab(); if (cellPrefab == null) { Debug.LogError("No cell prefab:" + t.name); break; } string panelName = mObjectNames[t]; string panelClassName = "TV" + UpperFirstLetter(panelName); string cellStructName = "Cell"; code.Append(TAB1).AppendLine(""); code.Append(TAB1).AppendLine("#region " + panelName); code.Append(TAB1).AppendLine("protected static class " + panelClassName); code.Append(TAB1).AppendLine("{"); mObjectNameIds.Clear(); generateCellClass(cellPrefab.transform, "Cell"); code.Append(TAB2).AppendLine("static public " + cellStructName + " Get(HS_UIListViewCell cell)"); code.Append(TAB2).AppendLine("{"); code.Append(TAB3).AppendLine("Transform t = cell.transform;"); generateCellProperty(cellPrefab.transform, "Cell", "obj", TAB3); code.Append(TAB3).AppendLine("return obj;"); code.Append(TAB2).AppendLine("}"); code.Append(TAB1).AppendLine("}"); code.Append(TAB1).AppendLine("#endregion"); } code.AppendLine("}"); WriteString(scriptPath + "/" + className + ".cs", code.ToString()); foreach (Transform t in mUIListViewList) { HS_ListViewBase listView = t.GetComponent <HS_ListViewBase>(); if (listView != null) { string cellName = GetCellPrefabName(panel, t); //Debug.Log("RecordCellPrefabName " + cellName); listView.RecordCellPrefabName(cellName); GameObject cell = listView.GetCellPrefab(); if (cell != null) { string cellPrefabPath = GetCellPathName(cellName, cellPath); HS_Directory.CreateDirectory(cellPath); PrefabUtility.CreatePrefab(cellPrefabPath, cell, ReplacePrefabOptions.ConnectToPrefab); } GameObject gridContent = listView.GetGridContent(); for (int i = gridContent.transform.childCount - 1; i >= 0; i--) { GameObject.DestroyImmediate(gridContent.transform.GetChild(i).gameObject); } } } MakeDirs(prefabPath); PrefabUtility.CreatePrefab(prefabPath + "/" + panel.name + ".prefab", panel.gameObject, ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(panel.gameObject); return(className); }