unity 脚本小工具
2021.12.10 又是沉迷编辑器工具的一天小工具 A
起因:
项目比较大了以后,编辑器就很卡。此时不小心拖拽了一个文件夹,而且还松手了,瞬间移动到了另一个目录下。顿时想死的心都有了,好在高版本提供了对文件夹移动的undo回滚。but,but,but,一但移动的是脚本文件,那可就惨咯。直接编译,如果报错还好。不用卡很久。要是编译通过。可以打开网站冲浪了。
最近发现一个类 UnityEditor.AssetModificationProcessor ,只用到其中一个函数。
OnWillMoveAsset Unity 即将在磁盘上移动资源时会调用此方法。大致功能为,移动 cs脚本时,目标路径不一致,弹出提示。
public class CustomAssetModificationProcessor : UnityEditor.AssetModificationProcessor
{
private static AssetMoveResult OnWillMoveAsset(string sourcePath, string destinationPath)
{
AssetMoveResult assetMoveResult = AssetMoveResult.DidNotMove;
bool isTip = false;
if (AssetDatabase.IsValidFolder(sourcePath))
{
isTip = AssetDatabase.FindAssets("t:Script", new string[] { sourcePath }).Length != 0;
}
else
{
if (sourcePath.EndsWith(".cs"))
{
//只是改了文件名
if (Directory.GetParent(sourcePath).FullName != Directory.GetParent(destinationPath).FullName)
{
isTip = true;
}
}
}
if (isTip)
{
if (EditorUtility.DisplayDialog("Move Scirpts Tip", $"Source path exist Scripts. Do you want move?\n\nSource path: {sourcePath}\nDestination path: {destinationPath}", "Yes Move", "No"
, DialogOptOutDecisionType.ForThisSession, "Move Scirpts Tip") == false)
{
assetMoveResult = AssetMoveResult.FailedMove;
}
}
return assetMoveResult;
}
}
小工具 B
强迫症,属实受不了默认创建的Mono脚本末尾有个换行。写个工具,替换引擎安装路径下的 .cs.txt 模板文件,
也提供还原功能,破坏和拆解我全都要。
public static void RemoveScriptTemplatesEndWrap()
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
string path = Directory.GetParent(Process.GetCurrentProcess().MainModule.FileName).FullName + @"\Data\Resources\ScriptTemplates";
var files = Directory.GetFiles(path, "*.txt");
var sourceBackupPath = path + "/SourceBackup";
var crlf = "\n";
foreach (var file in files)
{
var content = File.ReadAllText(file);
if (content.EndsWith(crlf))
{
if (Directory.Exists(sourceBackupPath) == false)
{
UnityEngine.Debug.Log("创建源备份文件夹");
Directory.CreateDirectory(sourceBackupPath);
}
File.Move(file, sourceBackupPath + "/" + Path.GetFileName(file));
content = content.Remove(content.Length - 4, 4);
File.WriteAllText(file, content);
UnityEngine.Debug.Log("已移除脚本模板末尾的换行符,路径:" + file);
}
}
}
}
public static void ReductionRemoveScriptTemplatesEndWrap()
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
string path = Directory.GetParent(Process.GetCurrentProcess().MainModule.FileName).FullName + @"\Data\Resources\ScriptTemplates";
var sourceBackupPath = path + "/SourceBackup";
if (Directory.Exists(sourceBackupPath))
{
var files = Directory.GetFiles(sourceBackupPath, "*.txt");
foreach (var file in files)
{
var lastPath = path + "/" + Path.GetFileName(file);
File.Delete(lastPath);
File.Move(file, lastPath);
UnityEngine.Debug.Log("已还原脚本模板末尾的换行符,路径:" + file);
}
if (Directory.GetFiles(sourceBackupPath).Length == 0)
{
Directory.Delete(sourceBackupPath);
UnityEngine.Debug.Log("删除源备份文件夹");
}
}
}
} 谢谢分享
页:
[1]