public static void CopyViewCodeToClipbord() { GameObject goSelected = Selection.activeGameObject; if (goSelected == null) { Debug.LogError("没有选中Prefab!"); return; } if (!File.Exists(EditorDefine.S_UIEdirorViewTempPath)) { Debug.LogError("模板文件不存在!" + EditorDefine.S_UIEdirorViewTempPath); 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 + UIExpandUtility.FormatName(go.name); UIExpandUtility.UniqueName(ref propertyNames, ref propertyName); string path = go.transform.GetPath(goSelected.transform); string value = string.Format(tagObj.tplText, objName, propertyName, path); sbInitView.Append(value); } } string strInitView = sbInitView.ToString(); StreamReader sr = new StreamReader(EditorDefine.S_UIEdirorViewTempPath, 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代码到剪贴板完成!"); }
/// <summary> /// 对于选择 的预制体文件 根据模板文件创建脚本 /// </summary> /// <param name="goSelected"></param> /// <param name="templateFilePath"></param> private static void CreateCodeFileFromTemplate(GameObject goSelected, string templateFilePath) { string className = goSelected.name; if (System.IO.Directory.Exists(EditorDefine.S_UGUISpritePath) == false) { System.IO.Directory.CreateDirectory(EditorDefine.S_UGUISpritePath); } string filePath = EditorUtility.SaveFilePanel("Generate Code", EditorDefine.S_UGUISpritePath, 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 + UIExpandUtility.FormatName(go.name); UIExpandUtility.UniqueName(ref propertyNames, ref propertyName); string path = go.transform.GetPath(goSelected.transform); string value = string.Format(tagObj.tplText, objName, propertyName, path); sbInitView.Append(value); uiParameter.Append("private " + objName + " m_" + propertyName + " ;\n"); viewTouiParameter.Append("m_" + propertyName + "=" + propertyName + ";\n"); } } string strInitView = sbInitView.ToString(); StreamReader sr = new StreamReader(templateFilePath, 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代码完成!"); }