首页 > 其他分享 >Unity Editor批量重命名GameObject和Prefab

Unity Editor批量重命名GameObject和Prefab

时间:2022-12-20 18:46:17浏览次数:44  
标签:Prefab firstObj objs return name int GameObject Unity

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class RenameEidtor : Editor
{
    static readonly char SpaceChar = '_';

    [MenuItem("GameObject/Tools/ReNameSort", false , 11)]
    public static void FnmRenameObjs()
    {
        GameObject[] objs = Selection.gameObjects;

        if (objs.Length < 2) return;

        GameObject firstObj = FnmGetMinNum(objs, out int currentNum);

        if (!IsFormatCorrect(firstObj)) firstObj.name += "_0";

        string baseName = firstObj.name.Substring(0, firstObj.name.LastIndexOf(SpaceChar) + 1);

        Transform parent = firstObj.transform.parent;

        int count = FnmClassify(parent, objs);

        int num = FnmGetNum(firstObj);

        //Debug.Log(firstObj.name + ", " + count + ", " + currentNum + ", " + num);
        for (int i = 1; i < count; i++)
        {
            parent.GetChild(i + currentNum).name = baseName + (num + i);
        }
    }

    static int FnmClassify(Transform templateP, GameObject[] objs)
    {
        int count = 0;

        for (int i = 0; i < objs.Length; i++)
        {
            if(objs[i].transform.parent == templateP)
            {
                count++;
            }
        }

        return count;
    }

    static GameObject FnmGetMinNum(GameObject[] objs, out int min)
    {
        min = objs[0].transform.GetSiblingIndex();
        GameObject minObj = objs[0];
        int num;

        for (int i = 1; i < objs.Length; i++)
        {
            num = objs[i].transform.GetSiblingIndex();
            if (num < min)
            {
                min = objs[i].transform.GetSiblingIndex();
                minObj = objs[i];
            }
        }

        return minObj;
    }

    static int FnmGetNum(GameObject obj)
    {
        if (!obj.name.Contains(SpaceChar.ToString())) return 10000;
        string baseName = obj.name.Substring(0, obj.name.LastIndexOf(SpaceChar) + 1);
        return int.Parse(obj.name.Replace(baseName, ""));
    }

    static bool IsFormatCorrect(GameObject obj)
    {
        string objName = obj.name;

        if (!objName.Contains(SpaceChar.ToString())) return false;

        string baseName = objName.Substring(0, objName.LastIndexOf(SpaceChar) + 1);

        return int.TryParse(objName.Replace(baseName, ""), out _);
    }
}

 

标签:Prefab,firstObj,objs,return,name,int,GameObject,Unity
From: https://www.cnblogs.com/txfd/p/16994872.html

相关文章

  • Unity Editor中文字体去重
    [MenuItem("Tools/GenerateZhCharacter")]publicstaticvoidGenerateZhCharacter(){vardataPath=Application.dataPath;varpath=Path.Combine(dataPa......
  • Unity-C# 协程 IEnumerator 用法梳理
    引用协程为了使用协程,必须在文件头部引入:usingSystem.Collections;启动协程使用StartCoroutine(IEnumerator)来启动一个协程停止协程使用StartCoroutine(IEnumer......
  • Unity shader cube纹理采样
    使用cube进行纹理采样,可以很方便的预览全景图,可以用立方体去显示全景图,而不必非得用球甚至还可以用更复杂的网格去贴全景图,只要保证网格的形状和全景图里的内容能对应上就......
  • Unity判断线和平面的交点
    //线和平面的交点privateVector3GetIntersectWithLineAndPlane(Vector3point,Vector3direct,Vector3planeNormal,Vector3planePoint){float......
  • How to mute all audio sound in unity? Unity中怎么样关闭所有音效
    AudioListener.pause=true;//orAudioListener.volume=0;Ref:https://answers.unity.com/questions/52109/how-do-i-mute-all-audio-sound.html......
  • Unity实现无缝大世界--地形
    大世界最重要的毫无疑问是地形了,地形也是一项比较古老,且一直在迭代更新的图形学技术。地形系统主体技术要点,一般围绕着LOD来展开。最近一些年,随着DrawInstance和GPUPipeli......
  • Unity获取手机本地应用以及调起apk安装、启动app、安装完成回调
    1、获取手机本地应用。创建AppInfo类和AppUtils类,用于接收获取到的应用列表。Android层代码AppInfo:publicclassAppInfo{privateDrawableimage;private......
  • Unity中实现AnimatorController
    前言:我们无法监听Animator是否播放完一个动画,我所知的办法就是将监听方法设置为Public,并且挂在带有Animator的物体上,并且还要在Clip文件内新增AnimEvent。于是我自己写了一......
  • unity 局域网内传送照片
    发送的电脑usingUnityEngine;usingSystem.Collections;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.IO;usingSystem;publicclassSendPhoto:Mon......
  • CommunityToolkit.Mvvm
    publicclassViewModel:ObservableObject{publicViewModel(){ShowCommand=newRelayCommand<string>(Show);}......