|
using System; |
|
using System.Collections.Generic; |
|
using nadena.dev.modular_avatar.core; |
|
using UnityEditor; |
|
using UnityEngine; |
|
|
|
namespace Autch.Editor.PopulateScaleAdjusters |
|
{ |
|
public class PSAMainWindow : EditorWindow |
|
{ |
|
[@MenuItem("Tools/Autch/PopulateScaleAdjusters")] |
|
public static void OpenWindow() |
|
{ |
|
var w = GetWindow<PSAMainWindow>(); |
|
w.titleContent = new GUIContent("PopulateScaleAdjusters"); |
|
w.Show(); |
|
} |
|
|
|
|
|
private GameObject _avatarObject; |
|
private Animator _animator; |
|
private Transform _hips; |
|
private bool _createIfMissing = true; |
|
private bool _updateExisting = true; |
|
|
|
private Vector2 _scrollPosition = Vector2.zero; |
|
private bool _showAdjusters = true, _showChildAdjusters = true, _showChildrenToPopulate = true; |
|
|
|
private List<GameObject> _parentAdjusters = new(); |
|
private readonly List<(string, GameObject)> _childAdjusters = new(); // (path, bone having SA) |
|
private readonly List<(string, GameObject)> _childrenToPopulate = new(); // (path, bone without SA) |
|
|
|
public void OnGUI() |
|
{ |
|
EditorGUILayout.Space(); |
|
GUILayout.Label("Populate Scale Adjusters", new GUIStyle() |
|
{ |
|
fontSize = 16, |
|
fontStyle = FontStyle.Bold, |
|
alignment = TextAnchor.MiddleCenter, |
|
normal = |
|
{ |
|
textColor = EditorGUIUtility.isProSkin ? Color.white : Color.black |
|
} |
|
}); |
|
EditorGUILayout.Space(); |
|
|
|
EditorGUILayout.HelpBox( |
|
"アバターの GameObject を指定して、その Armature 以下にある MA ScaleAdjuster を、 MA Merge Armature が置かれた Armature 以下の同名のボーンにコピーします", |
|
MessageType.None, true); |
|
|
|
var changed = false; |
|
EditorGUILayout.BeginHorizontal(); |
|
var newAvatar = |
|
EditorGUILayout.ObjectField("アバターのGameObject", _avatarObject, typeof(GameObject), true) as GameObject; |
|
if (newAvatar != _avatarObject) |
|
{ |
|
changed = true; |
|
_avatarObject = newAvatar; |
|
} |
|
|
|
if (GUILayout.Button(EditorGUIUtility.TrIconContent("Refresh", "Reload"), GUILayout.Width(30))) |
|
{ |
|
// ボタンが押された時の処理 |
|
changed = true; |
|
} |
|
|
|
EditorGUILayout.EndHorizontal(); |
|
|
|
if (changed) |
|
{ |
|
if (_avatarObject != null) |
|
{ |
|
_animator = _avatarObject.GetComponent<Animator>(); |
|
if (_animator != null) |
|
{ |
|
_hips = _animator.GetBoneTransform(HumanBodyBones.Hips); |
|
if (_hips != null) |
|
_parentAdjusters = PSAUtility.EnumerateScaleAdjusters(_hips); |
|
} |
|
} |
|
else |
|
{ |
|
_parentAdjusters.Clear(); |
|
} |
|
} |
|
|
|
if (_animator == null) |
|
{ |
|
EditorGUILayout.HelpBox("Animatorを含むアバターのGameObjectを指定してください", MessageType.Error); |
|
_parentAdjusters.Clear(); |
|
} |
|
|
|
if (_hips == null) |
|
{ |
|
EditorGUILayout.HelpBox("アバターにHipsボーンが見つかりませんでした", MessageType.Error); |
|
_parentAdjusters.Clear(); |
|
} |
|
|
|
EditorGUILayout.Space(); |
|
|
|
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition); |
|
if (_parentAdjusters.Count > 0) |
|
{ |
|
_showAdjusters = EditorGUILayout.BeginFoldoutHeaderGroup(_showAdjusters, |
|
$"このアバターのArmature直下のScaleAdjuster ({_parentAdjusters.Count})"); |
|
if (_showAdjusters) |
|
{ |
|
EditorGUI.BeginDisabledGroup(true); |
|
foreach (var t in _parentAdjusters) |
|
{ |
|
EditorGUILayout.ObjectField(t, typeof(GameObject), true); |
|
} |
|
|
|
EditorGUI.EndDisabledGroup(); |
|
} |
|
|
|
EditorGUILayout.EndFoldoutHeaderGroup(); |
|
} |
|
else if (_animator != null && _hips != null) |
|
{ |
|
EditorGUILayout.HelpBox("このアバターのArmature直下にScaleAdjusterは見つかりませんでした", MessageType.Info); |
|
} |
|
|
|
EditorGUILayout.Space(); |
|
|
|
if (_avatarObject != null && _animator != null && _hips != null && |
|
_avatarObject.GetComponentsInChildren<ModularAvatarMergeArmature>(true).Length == 0) |
|
{ |
|
EditorGUILayout.HelpBox("このアバターにはMA Merge Armatureが設定されていません", MessageType.Info); |
|
} |
|
|
|
if (_parentAdjusters.Count > 0) |
|
{ |
|
if (changed) |
|
PSAUtility.EnumerateChildAdjusters(_avatarObject, _childAdjusters, _childrenToPopulate); |
|
|
|
_showChildAdjusters = ListChildAdjusters(_showChildAdjusters, _childAdjusters, |
|
"このアバターのアイテムのScaleAdjuster ({0})", "アイテムにScaleAdjusterは見つかりませんでした"); |
|
EditorGUILayout.Space(); |
|
_showChildrenToPopulate = ListChildAdjusters(_showChildrenToPopulate, _childrenToPopulate, |
|
"このアバターのアイテムのScaleAdjuster未適用のボーン ({0})", "アイテムにScaleAdjuster未適用のボーンは見つかりませんでした"); |
|
} |
|
|
|
EditorGUILayout.EndScrollView(); |
|
EditorGUILayout.Space(); |
|
|
|
EditorGUILayout.LabelField("オプション", EditorStyles.boldLabel); |
|
_createIfMissing = EditorGUILayout.Toggle("存在しない場合は作成する", _createIfMissing); |
|
_updateExisting = EditorGUILayout.Toggle("既存のものはScaleを更新する", _updateExisting); |
|
|
|
GUI.enabled = _avatarObject != null && _parentAdjusters.Count > 0 && |
|
(_childAdjusters.Count > 0 || _childrenToPopulate.Count > 0); |
|
|
|
if (GUILayout.Button("コピーする", GUILayout.Height(30))) |
|
{ |
|
PSAUtility.PopulateScaleAdjusters(_avatarObject, _createIfMissing, _updateExisting); |
|
} |
|
|
|
GUI.enabled = true; |
|
} |
|
|
|
private bool ListChildAdjusters(bool toggle, List<(string, GameObject)> childAdjusters, string labelTitle, |
|
string labelIfNone) |
|
{ |
|
var t = EditorGUILayout.BeginFoldoutHeaderGroup(toggle, string.Format(labelTitle, childAdjusters.Count)); |
|
if (t) |
|
{ |
|
if (childAdjusters.Count > 0) |
|
{ |
|
EditorGUI.BeginDisabledGroup(true); |
|
foreach (var c in childAdjusters) |
|
{ |
|
using (new EditorGUILayout.HorizontalScope()) |
|
{ |
|
EditorGUILayout.LabelField(c.Item1); |
|
EditorGUILayout.ObjectField(c.Item2, typeof(GameObject), true); |
|
} |
|
} |
|
|
|
EditorGUI.EndDisabledGroup(); |
|
} |
|
else |
|
{ |
|
EditorGUILayout.LabelField(labelIfNone); |
|
} |
|
} |
|
|
|
EditorGUILayout.EndFoldoutHeaderGroup(); |
|
|
|
return t; |
|
} |
|
} |
|
} |