2023-11-10 14:37:56 +08:00
|
|
|
|
#if MA_VRCSDK3_AVATARS
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-11-25 10:00:18 +08:00
|
|
|
|
using System.Collections.Generic;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
using System.Linq;
|
2023-02-25 15:45:24 +08:00
|
|
|
|
using nadena.dev.modular_avatar.core.editor.menu;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using VRC.SDK3.Avatars.Components;
|
|
|
|
|
using VRC.SDK3.Avatars.ScriptableObjects;
|
|
|
|
|
|
2023-01-02 15:42:54 +08:00
|
|
|
|
|
2022-11-11 12:39:58 +08:00
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
2022-11-28 05:51:39 +08:00
|
|
|
|
internal class MenuInstallHook
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
2022-11-25 11:22:23 +08:00
|
|
|
|
private static Texture2D _moreIcon = AssetDatabase.LoadAssetAtPath<Texture2D>(
|
|
|
|
|
"Packages/nadena.dev.modular-avatar/Runtime/Icons/Icon_More_A.png"
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-05 20:30:01 +08:00
|
|
|
|
private BuildContext _context;
|
|
|
|
|
|
2022-11-07 11:33:48 +08:00
|
|
|
|
private VRCExpressionsMenu _rootMenu;
|
|
|
|
|
|
2023-01-02 15:42:54 +08:00
|
|
|
|
private Stack<ModularAvatarMenuInstaller> _visitedInstallerStack;
|
2023-01-05 20:30:01 +08:00
|
|
|
|
|
|
|
|
|
public void OnPreprocessAvatar(GameObject avatarRoot, BuildContext context)
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
2023-01-05 20:30:01 +08:00
|
|
|
|
_context = context;
|
|
|
|
|
|
|
|
|
|
ModularAvatarMenuInstaller[] menuInstallers = avatarRoot
|
|
|
|
|
.GetComponentsInChildren<ModularAvatarMenuInstaller>(true)
|
2023-01-02 15:42:54 +08:00
|
|
|
|
.Where(menuInstaller => menuInstaller.enabled)
|
2022-11-07 11:33:48 +08:00
|
|
|
|
.ToArray();
|
2022-10-16 10:18:58 +08:00
|
|
|
|
if (menuInstallers.Length == 0) return;
|
2023-01-05 20:30:01 +08:00
|
|
|
|
|
2023-01-02 15:42:54 +08:00
|
|
|
|
_visitedInstallerStack = new Stack<ModularAvatarMenuInstaller>();
|
2023-01-05 20:30:01 +08:00
|
|
|
|
|
2023-01-02 15:42:54 +08:00
|
|
|
|
VRCAvatarDescriptor avatar = avatarRoot.GetComponent<VRCAvatarDescriptor>();
|
2022-10-16 10:18:58 +08:00
|
|
|
|
|
2023-01-05 20:30:01 +08:00
|
|
|
|
if (avatar.expressionsMenu == null)
|
2022-11-07 11:33:48 +08:00
|
|
|
|
{
|
|
|
|
|
var menu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
|
2023-01-05 20:30:01 +08:00
|
|
|
|
_context.SaveAsset(menu);
|
2022-11-07 11:33:48 +08:00
|
|
|
|
avatar.expressionsMenu = menu;
|
2023-02-25 15:45:24 +08:00
|
|
|
|
context.ClonedMenus[menu] = menu;
|
2022-11-07 11:33:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rootMenu = avatar.expressionsMenu;
|
2023-05-11 20:08:33 +08:00
|
|
|
|
var virtualMenu = VirtualMenu.ForAvatar(avatar, context);
|
2023-02-25 15:45:24 +08:00
|
|
|
|
avatar.expressionsMenu = virtualMenu.SerializeMenu(asset =>
|
2023-01-02 15:42:54 +08:00
|
|
|
|
{
|
2023-02-25 15:45:24 +08:00
|
|
|
|
context.SaveAsset(asset);
|
|
|
|
|
if (asset is VRCExpressionsMenu menu) SplitMenu(menu);
|
|
|
|
|
});
|
2022-10-16 10:18:58 +08:00
|
|
|
|
}
|
2023-01-05 20:30:01 +08:00
|
|
|
|
|
2023-02-25 15:45:24 +08:00
|
|
|
|
private void SplitMenu(VRCExpressionsMenu targetMenu)
|
2023-01-02 15:42:54 +08:00
|
|
|
|
{
|
2023-01-05 20:30:01 +08:00
|
|
|
|
while (targetMenu.controls.Count > VRCExpressionsMenu.MAX_CONTROLS)
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
|
|
|
|
// Split target menu
|
|
|
|
|
var newMenu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
|
2023-01-05 20:30:01 +08:00
|
|
|
|
_context.SaveAsset(newMenu);
|
2023-01-02 15:42:54 +08:00
|
|
|
|
const int keepCount = VRCExpressionsMenu.MAX_CONTROLS - 1;
|
2022-10-16 10:18:58 +08:00
|
|
|
|
newMenu.controls.AddRange(targetMenu.controls.Skip(keepCount));
|
|
|
|
|
targetMenu.controls.RemoveRange(keepCount,
|
|
|
|
|
targetMenu.controls.Count - keepCount
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-02 15:42:54 +08:00
|
|
|
|
targetMenu.controls.Add(new VRCExpressionsMenu.Control
|
2022-10-16 10:18:58 +08:00
|
|
|
|
{
|
|
|
|
|
name = "More",
|
|
|
|
|
type = VRCExpressionsMenu.Control.ControlType.SubMenu,
|
2022-11-25 10:00:18 +08:00
|
|
|
|
subMenu = newMenu,
|
2023-01-02 15:42:54 +08:00
|
|
|
|
parameter = new VRCExpressionsMenu.Control.Parameter
|
2022-11-25 10:00:18 +08:00
|
|
|
|
{
|
|
|
|
|
name = ""
|
|
|
|
|
},
|
|
|
|
|
subParameters = Array.Empty<VRCExpressionsMenu.Control.Parameter>(),
|
2022-11-25 11:22:23 +08:00
|
|
|
|
icon = _moreIcon,
|
|
|
|
|
labels = Array.Empty<VRCExpressionsMenu.Control.Label>()
|
2022-10-16 10:18:58 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
targetMenu = newMenu;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-10 14:37:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|