using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
/// <summary>
/// 对Project下选中的物体重命名
/// </summary>
public class RenameAssets : EditorWindow {
private string BaseName = "";
private string Prefix = "";
private string Suffix = "";
private string LabelBase = "Base"; //提示标签
private string LabelPrefix = "Prefix"; //提示标签
private string LabelSuffix = "Suffix"; //提示标签
private bool Numberd = false; //是否添加数字后缀
private Object[] selList = null; //物体是否选中
private string NewName = "";
/// <summary>
/// 创建显示窗体
/// </summary>
[@MenuItem("Custom/Effect/RenameAssets")]
static void Init()
{
RenameAssets window = (RenameAssets)EditorWindow.GetWindow(typeof(RenameAssets));
//win.ShowNotification(new GUIContent("注意:!"));
window.Show ();
}
private void OnGUI()
{
/// <summary>
/// 添加界面
/// </summary>
GUI.Label (new Rect (20, 20, 160, 18), LabelBase);
BaseName = GUI.TextField(new Rect(100,20,120,18), BaseName, 40);
GUI.Label (new Rect (20, 60, 160, 18), LabelPrefix);
Prefix = GUI.TextField(new Rect(100,60,120,18), Prefix, 40);
GUI.Label (new Rect (20, 100, 160, 18), LabelSuffix);
Suffix = GUI.TextField(new Rect(100,100,120,18), Suffix, 40);
Numberd = GUI.Toggle (new Rect (20, 140, 120, 40), Numberd, " Numbered");
if (GUI.Button(new Rect(120f, 135f, 100f, 20f), "RENAME"))
{
RenameObject(BaseName, Prefix, Suffix, Numberd);
}
}
private void RenameObject(string BaseName, string Prefix, string Suffix, bool Numberd)
{
selList = Selection.objects;
if (selList.Length != 0)
{
if ("" == Prefix.Trim() )
{
Prefix = Prefix;
}
else
{
Prefix = Prefix + "_";
}
if ("" == Suffix.Trim())
{
Suffix = Suffix;
}
else
{
Suffix = "_" + Suffix;
}
if(selList.Length != 0 && BaseName.Trim() != "" )
{
if(Numberd == false) //不添加编号
{
for (int i = 0; i < selList.Length; i++)
{
string oldName = AssetDatabase.GetAssetPath(selList[i]); //Assets/Art/Characters/Ailuen/Effects/Resources/Ailuen01_FBX
string newName = selList[i].name.Replace( selList[i].name, (Prefix + BaseName + Suffix) );
AssetDatabase.RenameAsset(oldName, newName);
}
}
else //添加编号
{
for (int i = 0; i < selList.Length; i++)
{
string oldName = AssetDatabase.GetAssetPath(selList[i]); //Assets/Art/Characters/Ailuen/Effects/Resources/Ailuen01_FBX
string newName = selList[i].name.Replace( selList[i].name, (Prefix + BaseName + Suffix + "_0" + (i + 1) ) );
AssetDatabase.RenameAsset(oldName, newName);
}
}
}
else if(selList.Length != 0 && BaseName.Trim() == "" )
{
if(Numberd == false) //不添加编号
{
for (int i = 0; i < selList.Length; i++)
{
string oldName = AssetDatabase.GetAssetPath(selList[i]); //Assets/Art/Characters/Ailuen/Effects/Resources/Ailuen01_FBX
string newName = selList[i].name.Replace( selList[i].name, (Prefix + selList[i].name + Suffix + "") );
AssetDatabase.RenameAsset(oldName, newName);
}
}
else //添加编号
{
for (int i = 0; i < selList.Length; i++)
{
string oldName = AssetDatabase.GetAssetPath(selList[i]); //Assets/Art/Characters/Ailuen/Effects/Resources/Ailuen01_FBX
string newName = selList[i].name.Replace( selList[i].name, (Prefix + selList[i].name + Suffix + "_0" + (i + 1) ) );
AssetDatabase.RenameAsset(oldName, newName);
}
}
}
}
else
{
this.ShowNotification (new GUIContent("Project没有物体选中"));
}
}
}