示例#1
0
        private static Dictionary <string, string> GetReplaceMap(ExampleTemplate exampleTemplate)
        {
            ReplaceMap["$EXAMPLE_NAME$"]        = exampleTemplate.Name;
            ReplaceMap["$EXAMPLE_DESCRIPTION$"] = exampleTemplate.Description;
            ReplaceMap["$EXAMPLE_CATEGORY$"]    = exampleTemplate.Category;
            ReplaceMap["$CODE$"]       = exampleTemplate.Code;
            ReplaceMap["$CODE_PATH$"]  = exampleTemplate.CodePath;
            ReplaceMap["$PIC_PATH$"]   = exampleTemplate.PicPath;
            ReplaceMap["$VIDEO_PATH$"] = exampleTemplate.VideoPath;

            return(ReplaceMap);
        }
示例#2
0
        public static void CreateOverViewExampleFromTemplate(ExampleTemplate exampleTemplate)
        {
            if (exampleTemplate != null)
            {
                string templateContent = AssetDatabase.LoadAssetAtPath <TextAsset>(c_TemplatePath).text;

                string temp          = templateContent;
                string finalFileName = $"{c_DesPath}/Example_{exampleTemplate.Name}.cs";

                exampleTemplate.Code = CodeEscape(exampleTemplate.Code);

                Dictionary <string, string> replaceMap = GetReplaceMap(exampleTemplate);

                foreach (var kParam in replaceMap)
                {
                    temp = temp.Replace(kParam.Key, kParam.Value);
                }

                if (!Directory.Exists(c_DesPath))
                {
                    Directory.CreateDirectory(c_DesPath);
                }

                while (File.Exists(finalFileName))
                {
                    finalFileName = finalFileName.Replace(".cs", $"_1.cs");
                }

                //将文件信息读入流中
                //初始化System.IO.FileStream类的新实例与指定路径和创建模式
                using (var fs = new FileStream(finalFileName, FileMode.OpenOrCreate))
                {
                    if (!fs.CanWrite)
                    {
                        throw new System.Security.SecurityException("文件fileName=" + finalFileName + "是只读文件不能写入!");
                    }

                    var sw = new StreamWriter(fs);
                    sw.WriteLine(temp);
                    sw.Dispose();
                    sw.Close();
                }
            }
        }