protected override void RowGUI(RowGUIArgs args) { //base.RowGUI(args); UITreeViewItem item = (UITreeViewItem)args.item; var rowsRect = args.rowRect; var toggleRect = rowsRect; toggleRect.xMin = (15 * (item.depth + 1)); toggleRect.y += 1f; toggleRect.width = 16; EditorGUI.BeginChangeCheck(); if (item.Path != "this") { item.Select = EditorGUI.Toggle(toggleRect, item.Select); // hide when outside cell rect } Rect labelRect = toggleRect; labelRect.xMin += toggleRect.width + 2f; labelRect.width = 1000; if (EditorGUI.EndChangeCheck()) { UIScript.CreateUICode(); } if (item.Select) { GUI.Label(labelRect, args.label, GUIStyleTrue); return; } GUI.Label(labelRect, args.label, GUIStyleFalse); }
/// <summary> /// 获取被选中的数量 /// </summary> /// <returns></returns> public int GetSelectNum() { int num = 0; for (int i = 0; i < m_TreeViewItemList.Count; i++) { UITreeViewItem item = (UITreeViewItem)m_TreeViewItemList[i]; if (item.Select) { num++; } } return(num); }
/// <summary> /// 递归函数,遍历所有节点 /// </summary> private static void ForeachGameObject(GameObject go) { int m_depth = 0; string m_displayName = go.name; string m_Name = go.name; string path = go.name; if (go == m_Root) { path = "this"; } Transform ts = go.transform; while (ts.parent != null && ts.gameObject != m_Root) { ts = ts.parent; m_depth++; if (ts.gameObject != m_Root) { path = ts.name + "/" + path; } else { break; } } UITreeViewItem uITreeViewItem = new UITreeViewItem { id = m_Id++, depth = m_depth, displayName = m_displayName, Select = false, Path = path, Name = m_Name }; m_TreeViewItem.Add(uITreeViewItem); for (int i = 0; i < go.transform.childCount; i++) { ForeachGameObject(go.transform.GetChild(i).gameObject); } }
/// <summary> /// 生成UI代码字符串 /// </summary> /// <returns></returns> public static string CreateUICode() { Sort(m_TreeViewItem); UICode = ""; //组件变量代码字符串 string m_UIComponentCode = ""; //查找组件代码字符串 string m_FindUIComponentCode = ""; //注册按钮代码 string m_RegistUICode = ""; //按钮事件代码 string m_UIEventCode = ""; m_NameList.Clear(); for (int i = 0; i < m_TreeViewItem.Count; i++) { UITreeViewItem uITreeViewItem = (UITreeViewItem)m_TreeViewItem[i]; if (!uITreeViewItem.Select) { continue; } int j = 1; string nameStr = uITreeViewItem.Name; while (m_NameList.Contains(nameStr)) { nameStr = uITreeViewItem.displayName + "_" + j++; } uITreeViewItem.Name = nameStr; m_NameList.Add(nameStr); //单条组件变量代码字符串 string m_UIComCode = ""; //单条查找组件代码字符串 string m_FindUIComCode = ""; //单条注册按钮代码 string m_ReUICode = ""; //单条按钮事件代码 string m_UIEveCode = ""; string name = uITreeViewItem.displayName.ToUpper(); if (name.Contains("BUTTON") || name.Contains("BTN"))//按钮 { m_FindUIComCode = string.Format(FindButton, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Button, uITreeViewItem.Name.Replace(" ", "")); m_ReUICode = string.Format(RegistEvent, uITreeViewItem.Name.Replace(" ", "")); m_UIEveCode = string.Format(EventFuncCode, uITreeViewItem.Name.Replace(" ", "")) + "{\n\t}"; if (m_RegistUICode != "") { m_RegistUICode = m_RegistUICode + "\n" + m_ReUICode; } else { m_RegistUICode += m_ReUICode; } if (m_UIEventCode != "") { m_UIEventCode = m_UIEventCode + "\n" + m_UIEveCode; } else { m_UIEventCode += m_UIEveCode; } } //图片 else if (name.Contains("IMAGE") || name.Contains("IMG") || name.Contains("BG") || name.Contains("ICON") || name.Contains("TEXTURE")) { m_FindUIComCode = string.Format(FindImage, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Image, uITreeViewItem.Name.Replace(" ", "")); } //文本 else if (name.Contains("TEXT") || name.Contains("TXT")) { m_FindUIComCode = string.Format(FindText, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Text, uITreeViewItem.Name.Replace(" ", "")); } //InputField else if (name.Contains("INPUTFIELD")) { m_FindUIComCode = string.Format(FindInputField, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(InputField, uITreeViewItem.Name.Replace(" ", "")); } //Dropdown else if (name.Contains("DROPDOWN")) { m_FindUIComCode = string.Format(FindDropdown, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Dropdown, uITreeViewItem.Name.Replace(" ", "")); } //Scrollbar else if (name.Contains("SCROLLBAR")) { m_FindUIComCode = string.Format(FindScrollbar, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Scrollbar, uITreeViewItem.Name.Replace(" ", "")); } //ScrollRect else if (name.Contains("SCROLL")) { m_FindUIComCode = string.Format(FindScrollRect, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(ScrollRect, uITreeViewItem.Name.Replace(" ", "")); } //Slider else if (name.Contains("SLIDER")) { m_FindUIComCode = string.Format(FindSlider, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Slider, uITreeViewItem.Name.Replace(" ", "")); } //Toggle else if (name.Contains("TOGGLE")) { m_FindUIComCode = string.Format(FindToggle, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Toggle, uITreeViewItem.Name.Replace(" ", "")); } //Tranform else { m_FindUIComCode = string.Format(FindTransform, uITreeViewItem.Name.Replace(" ", ""), uITreeViewItem.Path); m_UIComCode = string.Format(Transform, uITreeViewItem.Name.Replace(" ", "")); } if (m_FindUIComponentCode != "") { m_FindUIComponentCode = m_FindUIComponentCode + "\n" + m_FindUIComCode; } else { m_FindUIComponentCode += m_FindUIComCode; } if (m_UIComponentCode != "") { m_UIComponentCode = m_UIComponentCode + "\n" + m_UIComCode; } else { m_UIComponentCode += m_UIComCode; } } UICode = Resources.Load <TextAsset>("Template").text; UICode = UICode.Replace("$UI Component Variables$", m_UIComponentCode).Replace("$FindUICode$", m_FindUIComponentCode).Replace("$NewBehaviourScript$", m_ScriptName); UICode = UICode.Replace("$AddBtnCode$", m_RegistUICode).Replace("$UIEventCode$", m_UIEventCode); UICode = UICode.Replace("$Time$", string.Concat(DateTime.Now.Year, "/", DateTime.Now.Month, "/", DateTime.Now.Day, " ", DateTime.Now.Hour, ":", DateTime.Now.Minute, ":", DateTime.Now.Second)); return(UICode); }