unity编辑器扩展替换选择场景中物体名字中的内容还有直接给自己的资源更改名字

下边附上我自己的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class ReplaceNameInScene : ScriptableWizard
{
    
    
    // Start is called before the first frame update
    [MenuItem("小黄之更改场景物体/选中Scene物体重命名")]
    static void CreateWizard()
    {
    
    
        DisplayWizard<ReplaceNameInScene>("替换Scene场景中名称", "确定");
    }

    [Header("物体中包含的内容")]
    public string originalName = "需要替换的内容";
    public string replaceContent = "替换的内容";

    private void OnWizardCreate()
    {
    
    
        GameObject[] Prefabs = Selection.gameObjects;
                  
        
        foreach (GameObject go in Prefabs)
        {
    
    
            if (go.name.Contains(originalName))
            {
    
    
                Debug.Log(go.name);
                go.name = go.name.Replace(originalName, replaceContent);
            }
            Transform[] m_childTransformArray = go.GetComponentsInChildren<Transform>();
            if (m_childTransformArray.Length >= 1)
            {
    
    
                for (int i = 0; i < m_childTransformArray.Length; i++)
                {
    
    
                    if (m_childTransformArray[i].name.Contains(originalName))
                    {
    
    
                        m_childTransformArray[i].name = m_childTransformArray[i].name.Replace(originalName, replaceContent);
                    }
                }
            }

        }

    }

    private void OnWizardUpdate()
    {
    
    
        helpString = "";
        errorString = "";
        string Name = "";
        if (Selection.gameObjects.Length > 0)
        {
    
    
            GameObject[] Prefabs = Selection.gameObjects;

            foreach (GameObject go in Prefabs)
            {
    
    
                Name += go.name + "、";
            }
            helpString = "当前选择了" + Name + "物体";
        }
        else
        {
    
    
            errorString = "请选择物体";
        }

    }

    //选择的物体改变的时候调用
    private void OnSelectionChange()
    {
    
    
        OnWizardUpdate();
    }

}
public class ReplaceNameInResource : ScriptableWizard
{
    
    
    [MenuItem("小黄之更改资源名字/选中相关的Image重命名")]
    static void CreateWizard()
    {
    
    
        DisplayWizard<ReplaceNameInResource>("更改资源中图片的名字", "确定");
        
    }

    [Header("资源中要添加的后缀名称")]
    public string AddName = "";
    [Header("当前文件夹的路径")]
    public string FileName = "";
    public List<string> myList;
    private List<string> firstList;

    private void OnWizardCreate()
    {
    
    
        myList = new List<string>();
        firstList = new List<string>();
        DirectoryInfo files = new DirectoryInfo(FileName);
        
        foreach(FileInfo file in files.GetFiles())
        {
    
    
            if(file.Name.EndsWith(".png"))
            {
    
    
                if(!myList.Contains(file.FullName))
                myList.Add(file.FullName);
            }
        }


        Debug.Log("AddName:" + AddName);

        if(myList.Count>0)
        {
    
    
            firstList.AddRange(myList);
            for(int i=0;i<myList.Count;i++)
            {
    
    
               string temp=myList[i].Insert(myList[i].LastIndexOf(".png"),AddName);
                Debug.Log("Temp:" + temp);
                myList[i] = temp;

            }
        }

        for(int i=0;i<firstList.Count;i++)
        {
    
    
            Debug.Log("Index:" + firstList[i]);
        }
        for (int i = 0; i < myList.Count; i++)
        {
    
    
            Debug.Log("IndexIndex:" + myList[i]);
        }



        for (int i=0;i<myList.Count;i++)
        {
    
    
            int index = i;
            if(File.Exists(firstList[index]))
            {
    
    
                File.Move(firstList[index], myList[index]);
            }
        }
        Debug.Log("Create");
       


    }


}


猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/124123794