/// <summary>
        /// 将所有的treeitem数据保存
        /// </summary>
        /// <param name="items"></param>
        public void SaveAllTreeItemData(UITreeview uiTreeview)
        {
            List <TreeViewItem> items             = uiTreeview.allItems;
            ListTreeItemDatas   listTreeItemDatas = new ListTreeItemDatas();

            for (int i = 0; i < items.Count; i++)
            {
                UITreeViewItem item = items[i] as UITreeViewItem;
                if (item != null)
                {
                    listTreeItemDatas.ItemTreeList.Add(new TreeItemData(item.id, item.depth, item.displayName, item.isVariable, item.isProperty, item.isUseEvent, uiTreeview.IsExpanded(item.id)));
                }
            }

            System.IO.File.WriteAllText(jsonDataPath, listTreeItemDatas.ToJson(), System.Text.Encoding.UTF8);
            //Debug.Log("json 生成结束");
        }
        void OnEnable()
        {
            variableAll = EditorPrefs.GetBool(VARIABLEALL, true);
            propertyAll = EditorPrefs.GetBool(PROPERTYALL, true);
            eventAll    = EditorPrefs.GetBool(EVENTALL, true);

            isExpandAll = EditorPrefs.GetBool(ISEXPANDALL, false);

            if (m_TreeViewState == null)
            {
                m_TreeViewState = new UITreeViewState();
            }

            //每次打开或者有脚本重新加载的时候 都会tree形态重新绘制 痛点 要把数据保存
            uiTreeView = new UITreeview(m_TreeViewState);

            m_SearchField = new SearchField();
            m_SearchField.downOrUpArrowKeyPressed += uiTreeView.SetFocusAndEnsureSelectedItem;
        }
Пример #3
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="uitreeView"></param>
        /// <returns></returns>
        public static string StartScriptGenerate(UITreeview uitreeView)
        {
            uiTreeview = uitreeView;

            StringBuilder sb = new StringBuilder();

            sb.Append(Str_Using);
            sb.Append(Str_Class);
            sb.Append("\n{\n");

            List <VariableNameData> listVariableName = new List <VariableNameData>(); //变量名
            List <PathData>         listVariablePath = new List <PathData>();         //变量名路径
            List <VariableNameData> listProperty     = new List <VariableNameData>(); //属性

            List <VariableNameData> listEvent = new List <VariableNameData>();        //事件

            foreach (var item in uiTreeview.allItems)
            {
                UITreeViewItem uitreeitem = item as UITreeViewItem;
                if (uitreeitem.isVariable)
                {
                    //类型 Image Button
                    //Type type = Helper.GetType(Helper.IDToGameObject(uitreeitem.id));
                    string type    = uitreeitem.CurrentGoComponents[uitreeitem.CurrentSeleteComponentIndex];
                    string disName = "m_" + uitreeitem.displayName.Replace(" ", "_");
                    //添加变量
                    listVariableName.Add(new VariableNameData(type, disName));
                    //添加查找路径
                    listVariablePath.Add(new PathData(disName, GetTreeItemFullPath(uitreeitem).ToString(), type));
                    if (uitreeitem.isProperty)
                    {
                        listProperty.Add(new VariableNameData(type, disName));
                    }

                    if (uitreeitem.isUseEvent)
                    {
                        if (hashUIEventType.Contains(type))
                        {
                            listEvent.Add(new VariableNameData(type, disName));
                        }
                    }
                }
            }


            #region 添加变量
            foreach (var item in listVariableName)
            {
                sb.Append($"\t[SerializeField] {item.type} {item.goName};\r\n");
            }
            #endregion
            sb.Append("\r\n");
            #region 添加属性
            foreach (var item in listProperty)
            {
                sb.Append($"\tpublic {item.type} {item.goName.Remove(0, 2)} {{get {{ return {item.goName};}} }} \r\n");
            }
            #endregion
            #region 添加事件
            if (listEvent.Count > 0)
            {
                sb.Append("\r\n");
                sb.Append("\tprivate void Start()\n\t{\n"); //start 方法
                List <string> listMethodName = new List <string>();
                foreach (var item in listEvent)
                {
                    sb.Append($"\t\t{item.goName}.");
                    if (item.type == nameof(Button))
                    {
                        string methodName = $"{item.goName}_OnClick";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onClick.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "()");
                    }
                    else if (item.type == "ButtonPlus")
                    {
                        string methodName = $"{item.goName}_OnClick";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onClick.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "()");
                    }
                    else if (item.type == nameof(Toggle))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(bool isOn)");
                    }
                    else if (item.type == nameof(InputField))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(string arg)");
                    }
                    else if (item.type == nameof(Slider))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(float value)");
                    }
                    else if (item.type == nameof(Dropdown))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(int index)");
                    }
                    else if (item.type == nameof(Scrollbar))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(float value)");
                    }
                    else if (item.type == nameof(ScrollRect))
                    {
                        string methodName = $"{item.goName}_OnValueChanged";
                        methodName = methodName.Remove(0, 2); //移除m_
                        sb.Append($"onValueChanged.AddListener({methodName});\n");
                        listMethodName.Add(methodName + "(Vector2 detal)");
                    }
                }
                sb.Append("\t}\n");
                //添加方法
                foreach (var method in listMethodName)
                {
                    sb.Append($"\tprivate void {method}\n");
                    sb.Append("\t{\n\n\t}\n");
                }
            }
            #endregion

            #region 添加Reset方法 查询路径
            sb.Append("\r\n\r\n\r\n");
            sb.Append("\t#region 用于寻找控件,当控件丢失,点击脚本齿轮->Reset菜单可恢复,也可重新编写下面的路径代码\r\n");
            sb.Append("#if UNITY_EDITOR\n\tprivate void Reset()\n");
            sb.Append("\t{\n");
            //添加寻找代码
            for (int i = 0; i < listVariablePath.Count; i++)
            {
                string needFind = string.IsNullOrEmpty(listVariablePath[i].path) ? string.Empty : $"transform.Find(\"{listVariablePath[i].path}\").";
                sb.Append($"\t\t{listVariablePath[i].variablename}={needFind}GetComponent<{listVariablePath[i].typename}>();\r\n");
            }

            sb.Append("\t}\n");
            sb.Append("#endif\n");
            sb.Append("\t#endregion\n");
            #endregion

            sb.Append("}");
            return(sb.ToString());
        }