码迷,mamicode.com
首页 > 其他好文 > 详细

RenameAssets

时间:2015-06-08 19:28:09      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

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没有物体选中"));
  }
 }
}

 

RenameAssets

标签:

原文地址:http://www.cnblogs.com/DragonXin/p/4561655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!