相同逻辑的游戏可以直接复制已有的代码,可是手动更改名字太麻烦了 所以写了个小工具直接重命名.
直接上代码
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System;
using LitJson;
public class RenamScript {
static EditorWindow window;
[MenuItem("Assets/重命名脚本")]
public static void RenamSprite()
{
window = EditorWindow.GetWindow<AddBuildMapUtility>();
window.title = "重命名脚本";
window.maxSize = new Vector2(280, 200);
}
public static void GoRenamScript(string oldStr = "", string newStr = "")
{
string path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
Debug.LogError(path);
DirectoryInfo direction = new DirectoryInfo(path);
FileInfo[] files = direction.GetFiles();
List<MonoScript> scripts = new List<MonoScript>();
foreach (FileInfo file in files) {
if (file.Name.EndsWith(".cs") || file.Name.EndsWith(".CS")) {
string assetPath = file.FullName.Substring(file.FullName.IndexOf("Assets"));
MonoScript sp = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);
if (sp.name.StartsWith(oldStr)) {
scripts.Add(sp);
string newName = sp.name.Replace(oldStr, newStr);
AssetDatabase.RenameAsset(assetPath, newName);
}
}
else {
Debug.LogWarning(file.Name + " 没有找到文件");
}
}
window.Close();
Debug.Log("打开更改完毕的文件 共: " + scripts.Count);
for (int i = 0; i < scripts.Count; i++) {
AssetDatabase.OpenAsset(scripts[i]);
}
}
public class AddBuildMapUtility : EditorWindow {
public string oldName, newName;
private void OnEnable()
{
Get();
}
Color color = new Color(1, 0, 0);
private void OnGUI()
{
EditorGUILayout.LabelField("");
EditorGUILayout.LabelField(" 提示:脚本编译器关闭无关脚本可以更方便更改 ");
EditorGUILayout.LabelField("");
oldName = EditorGUILayout.TextField("旧名", oldName);
EditorGUILayout.LabelField("");
newName = EditorGUILayout.TextField("新名", newName);
EditorGUILayout.LabelField("");
EditorGUILayout.LabelField("");
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("执行更改")) {
GoRenamScript(oldName, newName);
}
if (GUILayout.Button("翻转更改")) {
GoRenamScript(newName, oldName);
}
if (GUILayout.Button("保存名字")) {
Save(oldName, newName);
}
if (GUILayout.Button("清除名字")) {
Delete();
}
}
void Save(string oldStr = "", string newStr = "")
{
PlayerPrefs.SetString("oldStr", oldStr);
PlayerPrefs.SetString("newStr", newStr);
PlayerPrefs.Save();
}
void Get()
{
oldName = PlayerPrefs.GetString("oldStr");
newName = PlayerPrefs.GetString("newStr");
}
void Delete()
{
oldName = "";
newName = "";
PlayerPrefs.SetString("oldStr", oldName);
PlayerPrefs.SetString("newStr", newName);
PlayerPrefs.Save();
}
}
}
?使用工具前把编译器里的代码文件全部关闭? 更改后的代码会在编译器里打开 然后使用编译器的功能替换所有以打开的文件
到此大功告成?
|