Last active
September 4, 2023 14:32
-
-
Save TORISOUP/d46b1379887b0a771674348610a512aa to your computer and use it in GitHub Desktop.
アバター内に含まれている重複したGameObject名を修正するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
namespace TORISOUP.Editor.AvatarBoneNameFixer | |
{ | |
public class AvatarBoneNameFixer : EditorWindow | |
{ | |
[MenuItem("GameObject/AvatarBoneNameFixer", false, 0)] | |
public static void Process() | |
{ | |
var o = Selection.gameObjects.FirstOrDefault(); | |
if (o == null) return; | |
var clone = Clone(o); | |
var boneNameCount = new Dictionary<string, int>(); | |
RecursiveTransformSearch(boneNameCount, clone.transform); | |
} | |
private static void RecursiveTransformSearch(IDictionary<string, int> dic, Transform t) | |
{ | |
if(dic.ContainsKey(t.name)) | |
{ | |
var origin = t.name; | |
// すでにある | |
t.name = $"{origin}_{dic[t.name]}"; | |
dic[origin]++; | |
} | |
else | |
{ | |
// 初 | |
dic.Add(t.name, 1); | |
} | |
if (t.childCount == 0) return; | |
foreach (Transform i in t) | |
{ | |
RecursiveTransformSearch(dic, i); | |
} | |
} | |
private static GameObject Clone(GameObject oigin) | |
{ | |
var clone = Instantiate(oigin); | |
var parent = oigin.transform.parent; | |
clone.transform.SetParent(parent); | |
clone.transform.localPosition = oigin.transform.localPosition; | |
clone.transform.localRotation = oigin.transform.localRotation; | |
clone.transform.localScale = oigin.transform.localScale; | |
clone.transform.SetAsLastSibling(); | |
clone.name = oigin.name; | |
clone.name = GameObjectUtility.GetUniqueNameForSibling(parent, clone.name); | |
return clone; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment