Add validation for menu icons. (#130)

This commit is contained in:
らいちちゃん 2022-12-03 13:32:40 +09:00 committed by GitHub
parent 118c2bd961
commit 19a6923051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 4 deletions

View File

@ -5,6 +5,7 @@ using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDK3.Avatars.ScriptableObjects;
using static nadena.dev.modular_avatar.core.editor.Localization;
using static nadena.dev.modular_avatar.core.editor.Util;
namespace nadena.dev.modular_avatar.core.editor
{
@ -118,13 +119,26 @@ namespace nadena.dev.modular_avatar.core.editor
}
_devFoldout = EditorGUILayout.Foldout(_devFoldout, G("menuinstall.devoptions"));
if (_devFoldout)
if (_devFoldout)
{
SerializedProperty menuToAppendProperty = serializedObject.FindProperty(nameof(ModularAvatarMenuInstaller.menuToAppend));
switch (ValidateExpressionMenuIcon((VRCExpressionsMenu)menuToAppendProperty.objectReferenceValue))
{
case ValidateExpressionMenuIconResult.Success:
break;
case ValidateExpressionMenuIconResult.TooLarge:
EditorGUILayout.HelpBox(S("menuinstall.menu_icon_too_large"), MessageType.Error);
break;
case ValidateExpressionMenuIconResult.Uncompressed:
EditorGUILayout.HelpBox(S("menuinstall.menu_icon_uncompressed"), MessageType.Error);
break;
default:
throw new ArgumentOutOfRangeException();
}
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(
serializedObject.FindProperty(nameof(ModularAvatarMenuInstaller.menuToAppend)),
new GUIContent(G("menuinstall.srcmenu"))
);
menuToAppendProperty, new GUIContent(G("menuinstall.srcmenu")));
EditorGUI.indentLevel--;
}
@ -190,5 +204,31 @@ namespace nadena.dev.modular_avatar.core.editor
{
return _avatarMenus == null || _avatarMenus.Contains(menu);
}
private static ValidateExpressionMenuIconResult ValidateExpressionMenuIcon(VRCExpressionsMenu menu)
{
if (menu == null) return ValidateExpressionMenuIconResult.Success;
foreach (VRCExpressionsMenu.Control control in menu.controls)
{
// Control
ValidateExpressionMenuIconResult result = Util.ValidateExpressionMenuIcon(control.icon);
if (result != ValidateExpressionMenuIconResult.Success) return result;
// Labels
foreach (VRCExpressionsMenu.Control.Label label in control.labels)
{
ValidateExpressionMenuIconResult labelResult = Util.ValidateExpressionMenuIcon(label.icon);
if (labelResult != ValidateExpressionMenuIconResult.Success) return labelResult;
}
// SubMenu
if (control.type != VRCExpressionsMenu.Control.ControlType.SubMenu) continue;
ValidateExpressionMenuIconResult subMenuResult = ValidateExpressionMenuIcon(control.subMenu);
if (subMenuResult != ValidateExpressionMenuIconResult.Success) return subMenuResult;
}
return ValidateExpressionMenuIconResult.Success;
}
}
}

View File

@ -9,6 +9,8 @@
"menuinstall.showcontents": "Show menu contents",
"menuinstall.showcontents.notselected": "No menu selected",
"menuinstall.devoptions": "Prefab Developer Options",
"menuinstall.menu_icon_too_large" : "The icon set in the menu is too large than 256 pixels.",
"menuinstall.menu_icon_uncompressed" : "The icon set in the menu is not set for compression.",
"menuinstall.srcmenu": "Menu to Install",
"params.autodetect_header": " Autodetected Parameters ",
"params.internal": "Internal",

View File

@ -9,6 +9,8 @@
"menuinstall.showcontents": "メニュー内容を表示",
"menuinstall.showcontents.notselected": "メニューが選択されていません",
"menuinstall.devoptions": "プレハブ開発者向け設定",
"menuinstall.menu_icon_too_large" : "メニューに設定されているアイコンが256ピクセルより大きすぎます。",
"menuinstall.menu_icon_uncompressed" : "メニューに設定されているアイコンが圧縮設定されていません。",
"menuinstall.srcmenu": "インストールされるメニュー",
"params.autodetect_header": " 自動検出されたパラメーター ",
"params.internal": "内部値",

View File

@ -27,6 +27,7 @@ using System.Reflection;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using VRC.SDKBase.Editor.BuildPipeline;
using Object = UnityEngine.Object;
@ -138,5 +139,33 @@ namespace nadena.dev.modular_avatar.core.editor
return avatarValidation;
}
private const int MAX_EXPRESSION_TEXTURE_SIZE = 256;
public enum ValidateExpressionMenuIconResult
{
Success,
TooLarge,
Uncompressed
}
public static ValidateExpressionMenuIconResult ValidateExpressionMenuIcon(Texture2D icon)
{
string path = AssetDatabase.GetAssetPath(icon);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer == null) return ValidateExpressionMenuIconResult.Success;
TextureImporterPlatformSettings settings = importer.GetDefaultPlatformTextureSettings();
// Max texture size;
if ((icon.width > MAX_EXPRESSION_TEXTURE_SIZE || icon.height > MAX_EXPRESSION_TEXTURE_SIZE) &&
settings.maxTextureSize > MAX_EXPRESSION_TEXTURE_SIZE) return ValidateExpressionMenuIconResult.TooLarge;
// Compression
if (settings.textureCompression == TextureImporterCompression.Uncompressed) return ValidateExpressionMenuIconResult.Uncompressed;
return ValidateExpressionMenuIconResult.Success;
}
}
}