Streamingle_URP/Assets/Scripts/YAMO_Scripts/RenameHumanoidBones.cs
2025-07-26 09:06:46 +09:00

128 lines
4.0 KiB
C#

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class RenameHumanoidBones : EditorWindow
{
private Animator selectedAnimator;
private Dictionary<HumanBodyBones, Transform> humanoidBones = new Dictionary<HumanBodyBones, Transform>();
private Dictionary<HumanBodyBones, string> newBoneNames = new Dictionary<HumanBodyBones, string>();
[MenuItem("Tools/Humanoid Bone Renamer")]
public static void ShowWindow()
{
GetWindow<RenameHumanoidBones>("Humanoid Bone Renamer");
}
private Vector2 scrollPosition;
private void OnGUI()
{
GUILayout.Label("Humanoid Bone Renamer", EditorStyles.boldLabel);
if (GUILayout.Button("Find Humanoid Bones"))
{
FindHumanoidBones();
}
if (selectedAnimator == null)
{
GUILayout.Label("No valid Humanoid Animator selected.", EditorStyles.helpBox);
return;
}
GUILayout.Label("Selected GameObject: " + selectedAnimator.gameObject.name, EditorStyles.boldLabel);
if (humanoidBones.Count > 0)
{
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(400));
EditorGUILayout.BeginVertical("box");
GUILayout.Label("Humanoid Bones", EditorStyles.boldLabel);
foreach (var bone in humanoidBones)
{
EditorGUILayout.BeginHorizontal();
// First column: Unity's Humanoid bone name
GUILayout.Label(bone.Key.ToString(), GUILayout.Width(150));
// Second column: Current bone name
GUILayout.Label(bone.Value != null ? bone.Value.name : "None", GUILayout.Width(150));
// Third column: Input field for new name (defaulting to the first column's name)
if (!newBoneNames.ContainsKey(bone.Key))
{
newBoneNames[bone.Key] = bone.Key.ToString();
}
newBoneNames[bone.Key] = EditorGUILayout.TextField(newBoneNames[bone.Key], GUILayout.Width(150));
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
if (GUILayout.Button("Change Humanoid Bones Name"))
{
ChangeHumanoidBoneNames();
}
}
private void FindHumanoidBones()
{
selectedAnimator = null;
humanoidBones.Clear();
newBoneNames.Clear();
if (Selection.activeGameObject == null)
{
Debug.LogError("No GameObject selected. Please select a GameObject with an Animator component.");
return;
}
selectedAnimator = Selection.activeGameObject.GetComponent<Animator>();
if (selectedAnimator == null || selectedAnimator.avatar == null || !selectedAnimator.avatar.isValid || !selectedAnimator.avatar.isHuman)
{
Debug.LogError("Selected GameObject does not have a valid Humanoid Avatar.");
return;
}
for (int i = 0; i < HumanTrait.BoneCount; i++)
{
HumanBodyBones bone = (HumanBodyBones)i;
Transform boneTransform = selectedAnimator.GetBoneTransform(bone);
if (boneTransform != null)
{
humanoidBones[bone] = boneTransform;
}
}
Debug.Log("Humanoid bones found and ready for renaming.");
}
private void ChangeHumanoidBoneNames()
{
if (selectedAnimator == null)
{
Debug.LogError("No valid Humanoid Animator selected.");
return;
}
foreach (var bone in humanoidBones)
{
if (bone.Value != null && !string.IsNullOrWhiteSpace(newBoneNames[bone.Key]))
{
string newName = newBoneNames[bone.Key];
Debug.Log($"Renaming {bone.Value.name} to {newName}");
bone.Value.name = newName;
}
}
Debug.Log("Bone renaming completed.");
}
}