2024-10-20 09:58:41 +08:00
|
|
|
|
#if MA_VRCSDK3_AVATARS
|
|
|
|
|
using nadena.dev.modular_avatar.ui;
|
2024-08-11 10:16:57 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.ScriptableObjects;
|
|
|
|
|
|
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
|
|
|
|
{
|
|
|
|
|
internal static class ToggleCreatorShortcut
|
|
|
|
|
{
|
2024-12-21 05:18:37 +08:00
|
|
|
|
[MenuItem(UnityMenuItems.GameObject_SetupToggle, false, UnityMenuItems.GameObject_SetupToggleOrder)]
|
2024-12-21 05:40:23 +08:00
|
|
|
|
private static void SetupToggle() => CreateToggles(true);
|
2024-12-21 05:18:37 +08:00
|
|
|
|
|
2024-08-11 10:16:57 +08:00
|
|
|
|
[MenuItem(UnityMenuItems.GameObject_CreateToggle, false, UnityMenuItems.GameObject_CreateToggleOrder)]
|
2024-12-21 05:40:23 +08:00
|
|
|
|
private static void CreateToggle() => CreateToggles(false);
|
|
|
|
|
|
|
|
|
|
private static void CreateToggles(bool setup)
|
|
|
|
|
{
|
|
|
|
|
var selections = Selection.objects;
|
|
|
|
|
foreach (var selection in selections) {
|
|
|
|
|
if (selection == null) continue;
|
|
|
|
|
CreateToggleImpl(selection as GameObject, setup);
|
|
|
|
|
}
|
|
|
|
|
Selection.objects = null;
|
|
|
|
|
}
|
2024-12-21 05:18:37 +08:00
|
|
|
|
|
2024-12-21 05:40:23 +08:00
|
|
|
|
private static void CreateToggleImpl(GameObject selected, bool setup)
|
2024-08-11 10:16:57 +08:00
|
|
|
|
{
|
|
|
|
|
var avatarRoot = RuntimeUtil.FindAvatarTransformInParents(selected.transform);
|
|
|
|
|
if (avatarRoot == null) return;
|
|
|
|
|
|
|
|
|
|
bool createInstaller = true;
|
|
|
|
|
Transform parent = avatarRoot;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var selectedMenuItem = selected.GetComponent<ModularAvatarMenuItem>();
|
|
|
|
|
if (selectedMenuItem?.Control?.type == VRCExpressionsMenu.Control.ControlType.SubMenu
|
|
|
|
|
&& selectedMenuItem.MenuSource == SubmenuSource.Children
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
parent = selected.transform;
|
|
|
|
|
createInstaller = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-28 10:24:19 +08:00
|
|
|
|
catch (MissingComponentException)
|
2024-08-11 10:16:57 +08:00
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2024-12-21 05:18:37 +08:00
|
|
|
|
|
|
|
|
|
var name = setup ? selected.name + " Toggle" : "New Toggle";
|
2024-08-11 10:16:57 +08:00
|
|
|
|
|
2024-12-21 05:18:37 +08:00
|
|
|
|
var toggle = new GameObject(name);
|
2024-08-11 10:16:57 +08:00
|
|
|
|
|
|
|
|
|
var objToggle = toggle.AddComponent<ModularAvatarObjectToggle>();
|
2024-12-21 05:18:37 +08:00
|
|
|
|
if (setup)
|
|
|
|
|
{
|
|
|
|
|
var path = RuntimeUtil.RelativePath(avatarRoot.gameObject, selected);
|
|
|
|
|
objToggle.Objects.Add(new ToggledObject
|
|
|
|
|
{
|
|
|
|
|
Object = new AvatarObjectReference(){ referencePath = path },
|
2024-12-21 11:51:58 +08:00
|
|
|
|
Active = !selected.activeSelf
|
2024-12-21 05:18:37 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 10:16:57 +08:00
|
|
|
|
|
|
|
|
|
toggle.transform.SetParent(parent, false);
|
2024-09-14 09:57:03 +08:00
|
|
|
|
|
|
|
|
|
var mami = toggle.AddComponent<ModularAvatarMenuItem>();
|
|
|
|
|
mami.InitSettings();
|
|
|
|
|
mami.Control = new VRCExpressionsMenu.Control
|
2024-08-11 10:16:57 +08:00
|
|
|
|
{
|
|
|
|
|
type = VRCExpressionsMenu.Control.ControlType.Toggle,
|
2024-12-21 05:18:37 +08:00
|
|
|
|
name = name,
|
2024-08-11 10:16:57 +08:00
|
|
|
|
value = 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (createInstaller)
|
|
|
|
|
{
|
|
|
|
|
toggle.AddComponent<ModularAvatarMenuInstaller>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Selection.activeGameObject = toggle;
|
|
|
|
|
EditorGUIUtility.PingObject(objToggle);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Undo.RegisterCreatedObjectUndo(toggle, "Create Toggle");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-20 09:58:41 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|