mirror of
https://github.com/bdunderscore/modular-avatar.git
synced 2025-04-04 19:49:02 +08:00
chore: refactor and Ignore GameObjects with submenu for CreateToggleForSelection
This commit is contained in:
parent
9510c56f7a
commit
43efcdd70f
@ -1,4 +1,5 @@
|
|||||||
#if MA_VRCSDK3_AVATARS
|
#if MA_VRCSDK3_AVATARS
|
||||||
|
using System.Linq;
|
||||||
using nadena.dev.modular_avatar.ui;
|
using nadena.dev.modular_avatar.ui;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -9,39 +10,80 @@ namespace nadena.dev.modular_avatar.core.editor
|
|||||||
internal static class ToggleCreatorShortcut
|
internal static class ToggleCreatorShortcut
|
||||||
{
|
{
|
||||||
[MenuItem(UnityMenuItems.GameObject_CreateToggleForSelection, false, UnityMenuItems.GameObject_CreateToggleForSelectionOrder)]
|
[MenuItem(UnityMenuItems.GameObject_CreateToggleForSelection, false, UnityMenuItems.GameObject_CreateToggleForSelectionOrder)]
|
||||||
private static void CreateToggleForSelection(MenuCommand command) => CreateToggleImpl(command, true);
|
private static void CreateToggleForSelection()
|
||||||
|
{
|
||||||
|
var forSelection = true;
|
||||||
|
|
||||||
|
var selections = Selection.objects.OfType<GameObject>();
|
||||||
|
// Ignore GameObjects with submenu in the context of CreateToggleForSelection.
|
||||||
|
selections = selections.Where(s => !TryGetChildrenSourceSubmenu(s, out var _));
|
||||||
|
if (selections.Count() == 0) return;
|
||||||
|
|
||||||
|
foreach (var selected in selections)
|
||||||
|
{
|
||||||
|
var avatarRoot = RuntimeUtil.FindAvatarTransformInParents(selected.transform);
|
||||||
|
if (avatarRoot == null) return;
|
||||||
|
|
||||||
|
var parent = selected.transform.parent?.gameObject;
|
||||||
|
if (parent == null) continue;
|
||||||
|
|
||||||
|
CreateToggleImpl(selected, parent, selected.name + " Toggle", forSelection, createInstaller:true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection.objects = null;
|
||||||
|
}
|
||||||
|
|
||||||
[MenuItem(UnityMenuItems.GameObject_CreateToggle, false, UnityMenuItems.GameObject_CreateToggleOrder)]
|
[MenuItem(UnityMenuItems.GameObject_CreateToggle, false, UnityMenuItems.GameObject_CreateToggleOrder)]
|
||||||
private static void CreateToggle(MenuCommand command) => CreateToggleImpl(command, false);
|
private static void CreateToggle()
|
||||||
|
|
||||||
private static void CreateToggleImpl(MenuCommand command, bool forSelection)
|
|
||||||
{
|
{
|
||||||
var selected = command.context as GameObject;
|
var selections = Selection.objects.OfType<GameObject>();
|
||||||
if (selected == null) return;
|
if (selections.Count() == 0) return;
|
||||||
|
|
||||||
var avatarRoot = RuntimeUtil.FindAvatarTransformInParents(selected.transform);
|
foreach (var selected in selections)
|
||||||
if (avatarRoot == null) return;
|
{
|
||||||
|
var avatarRoot = RuntimeUtil.FindAvatarTransformInParents(selected.transform);
|
||||||
|
if (avatarRoot == null) return;
|
||||||
|
|
||||||
bool createInstaller = true;
|
var parent = avatarRoot.gameObject;
|
||||||
Transform parent = avatarRoot;
|
var createInstaller = true;
|
||||||
|
|
||||||
|
if (TryGetChildrenSourceSubmenu(selected, out var _))
|
||||||
|
{
|
||||||
|
parent = selected;
|
||||||
|
createInstaller = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
CreateToggleImpl(selected, parent, "New Toggle", createInstaller:createInstaller);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection.objects = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryGetChildrenSourceSubmenu(GameObject target, out ModularAvatarMenuItem subMenu)
|
||||||
|
{
|
||||||
|
subMenu = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var selectedMenuItem = selected.GetComponent<ModularAvatarMenuItem>();
|
var mami = target.GetComponent<ModularAvatarMenuItem>();
|
||||||
if (selectedMenuItem?.Control?.type == VRCExpressionsMenu.Control.ControlType.SubMenu
|
if (mami?.Control?.type == VRCExpressionsMenu.Control.ControlType.SubMenu
|
||||||
&& selectedMenuItem.MenuSource == SubmenuSource.Children
|
&& mami.MenuSource == SubmenuSource.Children
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent = selected.transform;
|
subMenu = mami;
|
||||||
createInstaller = false;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (MissingComponentException)
|
catch (MissingComponentException)
|
||||||
{
|
{
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var name = forSelection ? selected.name + " Toggle" : "New Toggle";
|
private static void CreateToggleImpl(GameObject selected, GameObject parent, string name, bool forSelection = false, bool createInstaller = true)
|
||||||
|
{
|
||||||
|
var avatarRoot = RuntimeUtil.FindAvatarTransformInParents(selected.transform);
|
||||||
|
if (avatarRoot == null) return;
|
||||||
|
|
||||||
var toggle = new GameObject(name);
|
var toggle = new GameObject(name);
|
||||||
|
|
||||||
@ -57,7 +99,7 @@ namespace nadena.dev.modular_avatar.core.editor
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
toggle.transform.SetParent(parent, false);
|
toggle.transform.SetParent(parent.transform, false);
|
||||||
|
|
||||||
var mami = toggle.AddComponent<ModularAvatarMenuItem>();
|
var mami = toggle.AddComponent<ModularAvatarMenuItem>();
|
||||||
mami.InitSettings();
|
mami.InitSettings();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user