112 lines
5.6 KiB
C#
112 lines
5.6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using VRM;
|
|
|
|
namespace VRMTools
|
|
{
|
|
public class CleanObjectsEditorWindow : EditorWindow
|
|
{
|
|
[MenuItem("VRMTools/VRMSpringBoneTool\u200B\u2215\u200BVRMSkirtTool Clean クリーン (VRM 0.x)", false, 9)]
|
|
static void Init()
|
|
{
|
|
CleanObjectsEditorWindow window = GetWindow<CleanObjectsEditorWindow>();
|
|
window.titleContent = new GUIContent("Clean");
|
|
}
|
|
|
|
GameObject avatar = null;
|
|
|
|
void OnGUI()
|
|
{
|
|
avatar = EditorGUILayout.ObjectField("Avatar アバター", avatar, typeof(GameObject), true) as GameObject;
|
|
if (avatar == null)
|
|
{
|
|
if (GUILayout.Button("Auto Detect 自動認識"))
|
|
{
|
|
avatar = FindObjectsOfType<Animator>().FirstOrDefault(x => x.isHuman)?.gameObject;
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (PrefabUtility.GetPrefabAssetType(avatar) != PrefabAssetType.NotAPrefab)
|
|
{
|
|
EditorGUILayout.HelpBox("Unpack the Prefab instance with the button below.\n下のボタンを押してPrefabを展開してください。", MessageType.Warning);
|
|
if (GUILayout.Button("Unpack Prefab 展開"))
|
|
{
|
|
PrefabUtility.UnpackPrefabInstance(avatar, PrefabUnpackMode.Completely, InteractionMode.UserAction);
|
|
}
|
|
return;
|
|
}
|
|
|
|
EditorGUILayout.HelpBox("Remove components and unused GameObjects for VRMSpringBoneTool/VRMSkirtTool.\nVRMSpringBoneTool/VRMSkirtTool用のコンポーネントや使用されていないGameObjectを削除します。", MessageType.Info);
|
|
|
|
if (GUILayout.Button("Clean クリーン"))
|
|
{
|
|
VRMSpringBone[] vrmSpringBones = avatar.GetComponentsInChildren<VRMSpringBone>(true);
|
|
|
|
foreach (Transform transform in avatar.GetComponentsInChildren<Transform>().Where(x => x.childCount == 0))
|
|
{
|
|
if (transform != null && transform.gameObject != null)
|
|
{
|
|
//Transform parent = transform.parent;
|
|
if ((/*transform.gameObject.name.StartsWith("VST_") || */
|
|
transform.gameObject.name.StartsWith("VSB_") ||
|
|
transform.gameObject.name.StartsWith("VSK_") ||
|
|
transform.gameObject.name.StartsWith("VRMSpringBoneTool") ||
|
|
transform.gameObject.name.StartsWith("VRMSkirtTool")))
|
|
{
|
|
foreach (MonoBehaviour component in transform.gameObject.GetComponents<MonoBehaviour>().ToArray())
|
|
{
|
|
Type type = component.GetType();
|
|
if (type.FullName == "VRMTools.DoNotStoreData" ||
|
|
type.FullName == "VRMTools.VRMSpringBoneToolSetting" ||
|
|
type.FullName == "VRMTools.VRMSkirtToolSetting")
|
|
{
|
|
Undo.DestroyObjectImmediate(component);
|
|
}
|
|
}
|
|
|
|
Component[] components = transform.gameObject.GetComponents<Component>();
|
|
|
|
if (components.Length <= 1)
|
|
{
|
|
Undo.DestroyObjectImmediate(transform.gameObject);
|
|
}
|
|
else if ((transform.gameObject.name.StartsWith("VSB_") || transform.gameObject.name.StartsWith("VSK_")) &&
|
|
components.Length == 2 &&
|
|
transform.localPosition == Vector3.zero &&
|
|
transform.localRotation == Quaternion.identity &&
|
|
transform.localScale == Vector3.one)
|
|
{
|
|
VRMSpringBoneColliderGroup dst = transform.parent.GetComponent<VRMSpringBoneColliderGroup>();
|
|
if (dst == null)
|
|
{
|
|
foreach (VRMSpringBoneColliderGroup src in transform.gameObject.GetComponents<VRMSpringBoneColliderGroup>().ToArray())
|
|
{
|
|
dst = Undo.AddComponent<VRMSpringBoneColliderGroup>(transform.parent.gameObject);
|
|
dst.Colliders = src.Colliders.ToArray();
|
|
|
|
foreach (VRMSpringBone vrmSpringBone in vrmSpringBones)
|
|
{
|
|
for (int i = 0; i < vrmSpringBone.ColliderGroups.Length; i++)
|
|
{
|
|
if (vrmSpringBone.ColliderGroups[i] == src)
|
|
{
|
|
Undo.RecordObject(vrmSpringBone, "MoveVRMSpringBoneColliderGroup");
|
|
vrmSpringBone.ColliderGroups[i] = dst;
|
|
}
|
|
}
|
|
}
|
|
|
|
Undo.DestroyObjectImmediate(transform.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |