Exemplo n.º 1
0
        // [MenuItem("CONTEXT/UICacheView/生成View代码")]
        public static void GenUIViewCode()
        {
            GameObject goSelected = Selection.activeGameObject;

            if (goSelected == null)
            {
                Debug.LogError("没有选中Prefab!");
                return;
            }

            string strTplPath = Application.dataPath + "/" + ConstDefine.UIEdirtorViewTemplatePath;

            if (!File.Exists(strTplPath))
            {
                Debug.LogError("模板文件不存在!" + strTplPath);
                return;
            }

            string className = goSelected.name;
            string filePath  = EditorUtility.SaveFilePanel("Generate Code", Application.dataPath + "/Script/UGUI_Panel", className + ".cs", "cs");

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            className = Path.GetFileNameWithoutExtension(filePath);

            List <GameObject> goChilds = goSelected.GetChildCollectionRecursive();

            goChilds.Insert(0, goSelected);

            StringBuilder            uiParameter       = new StringBuilder();
            StringBuilder            viewTouiParameter = new StringBuilder();
            StringBuilder            sbInitView        = new StringBuilder();
            Dictionary <string, int> propertyNames     = new Dictionary <string, int>();

            foreach (GameObject go in goChilds)
            {
                if (TagObjs.ContainsKey(go.tag))
                {
                    TagInfor tagObj       = TagObjs[go.tag];
                    string   objName      = tagObj.objName;
                    string   propertyName = tagObj.prefix + UnityExpand.FormatName(go.name);
                    UnityExpand.UniqueName(ref propertyNames, ref propertyName);
                    string path  = go.transform.GetPath(goSelected.transform);
                    string value = string.Format(tagObj.tplText, objName, propertyName, path);
                    sbInitView.Append(value);
                    //对UICacheItemMark组件进行额外处理
                    //if (go.tag == "UICacheItemMark")
                    //{
                    //    UICacheItemMark cacheItem = go.GetComponent<UICacheItemMark>();
                    //    if (cacheItem == null)
                    //        continue;
                    //    string[] componentNames = cacheItem.m_ComponentNames;
                    //    foreach (string componentName in componentNames)
                    //    {
                    //        value = string.Format("\t\t\t{0}.onLoad += () => {{ {2} = {0}.GetAttachComponent<{1}>(); }};\n",
                    //            propertyName, componentName, GetPropertyName(propertyName));
                    //        sbInitView.Append(value);
                    //    }
                    //}

                    uiParameter.Append("private " + objName + " m_" + propertyName + " ;\n");
                    viewTouiParameter.Append("m_" + propertyName + "=" + propertyName + ";\n");
                }
            }

            string       strInitView = sbInitView.ToString();
            StreamReader sr          = new StreamReader(strTplPath, Encoding.UTF8);
            string       strTpl      = sr.ReadToEnd();

            sr.Close();


            strTpl = Regex.Replace(strTpl, " #UIPARAMETER#", uiParameter.ToString());
            strTpl = Regex.Replace(strTpl, " #INITVIEWTOPARAMETER#", viewTouiParameter.ToString());

            strTpl = Regex.Replace(strTpl, "#CLASSNAME#", className);
            Debug.Log(className);
            strTpl = Regex.Replace(strTpl, "#INITVIEW#", strInitView);
            Debug.Log(strInitView);

            //拷贝到剪贴板
            TextEditor te = new TextEditor();

            te.text = strInitView;
            te.SelectAll();
            te.Copy();

            StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);

            sw.Write(strTpl);
            sw.Close();

            AssetDatabase.Refresh();
            Debug.Log("生成View代码完成!");
        }
Exemplo n.º 2
0
        //     [MenuItem("CONTEXT/UICacheView/拷贝View代码")]
        public static void CopyViewCodeToClipbord()
        {
            GameObject goSelected = Selection.activeGameObject;

            if (goSelected == null)
            {
                Debug.LogError("没有选中Prefab!");
                return;
            }

            string strTplPath = Application.dataPath + "/" + ConstDefine.UIEdirtorViewTemplatePath;

            if (!File.Exists(strTplPath))
            {
                Debug.LogError("模板文件不存在!" + strTplPath);
                return;
            }

            List <GameObject> goChilds = goSelected.GetChildCollectionRecursive();

            goChilds.Insert(0, goSelected);

            StringBuilder            sbInitView    = new StringBuilder();
            Dictionary <string, int> propertyNames = new Dictionary <string, int>();

            foreach (GameObject go in goChilds)
            {
                if (TagObjs.ContainsKey(go.tag))
                {
                    TagInfor tagObj       = TagObjs[go.tag];
                    string   objName      = tagObj.objName;
                    string   propertyName = tagObj.prefix + UnityExpand.FormatName(go.name);
                    UnityExpand.UniqueName(ref propertyNames, ref propertyName);
                    string path  = go.transform.GetPath(goSelected.transform);
                    string value = string.Format(tagObj.tplText, objName, propertyName, path);
                    sbInitView.Append(value);
                    //对UICacheItemMark组件进行额外处理
                    //if (go.tag == "UICacheItemMark")
                    //{
                    //    UICacheItemMark cacheItem = go.GetComponent<UICacheItemMark>();
                    //    if (cacheItem == null)
                    //        continue;
                    //    string[] componentNames = cacheItem.m_ComponentNames;
                    //    foreach (string componentName in componentNames)
                    //    {
                    //        value = string.Format("\t\t\t{0}.onLoad += () => {{ {2} = {0}.GetAttachComponent<{1}>(); }};\n",
                    //            propertyName, componentName, GetPropertyName(propertyName));
                    //        sbInitView.Append(value);
                    //    }
                    //}
                }
            }

            string       strInitView = sbInitView.ToString();
            StreamReader sr          = new StreamReader(strTplPath, Encoding.UTF8);
            string       strTpl      = sr.ReadToEnd();

            sr.Close();

            strTpl = Regex.Replace(strTpl, "#INITVIEW#", strInitView);
            Debug.Log(strInitView);

            //拷贝到剪贴板
            TextEditor te = new TextEditor();

            te.text = strInitView;
            te.SelectAll();
            te.Copy();

            Debug.Log("拷贝View代码到剪贴板完成!");
        }