NLDClient-yudde/ProjectNLD/Assets/Editor/Tools/UIComponentTool/UIReGenTool.cs

73 lines
2.2 KiB
C#

using System;
using System.IO;
using System.Text;
using BehaviorDesigner.Runtime.Tasks;
using UnityEditor;
using UnityEngine;
using Directory = UnityEngine.Windows.Directory;
//重新刷新生成的文本内容
public class UIReGenTool
{
public static string UI_CTRLLER_PATH = Application.dataPath + "/Code/Scripts/Gameplay/UI";
public static string CONST_START = "///Auto Gen Start///";
public static string CONST_END = "///Auto Gen End///";
public static void ReWriteDefine(string newContent, string fileName, string filePath)
{
var raplaceStr = fileName + "Controller.cs";
filePath = filePath.Replace('\\', '/');
var curPath = filePath.Replace(raplaceStr, "");
var strL = filePath.Split('/');
string relativePath = "";
int startIdx = -1;
for (int i = 0; i < strL.Length; i++)
{
if (startIdx >= 0)
{
relativePath += "\\";
relativePath += strL[i];
}
if (strL[i].Equals("Assets"))
{
startIdx = i;
relativePath += strL[i];
}
}
if (File.Exists(filePath))
{
var newName = "UIGenOldTemp.cs";
curPath += newName;
AssetDatabase.RenameAsset(relativePath, newName);
AssetDatabase.Refresh();
bool isReWriting = false;
using (StreamWriter writer = File.CreateText(filePath))
{
foreach(var line in File.ReadLines(curPath))
{
if (!isReWriting)
{
writer.WriteLine(line);
}
if (line.Contains(CONST_START))
{
isReWriting = true;
//writer.WriteLine(line);
}
else if(line.Contains(CONST_END))
{
isReWriting = false;
writer.WriteLine('\t'+ newContent);
writer.WriteLine(line);
}
}
}
File.Delete(curPath);
}
}
}