Skip to content

Commit

Permalink
子をカスタム可能に
Browse files Browse the repository at this point in the history
  • Loading branch information
Narazaka committed Feb 6, 2024
1 parent 807fff5 commit 455a34e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 12 deletions.
46 changes: 36 additions & 10 deletions Editor/AvatarParametersPresetPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,46 @@ void StoreAssets(AvatarParametersSaverPresetGroup presets, GameObject go, string
internalParameter = string.IsNullOrEmpty(presets.parameterName),
},
};
var redirect = new GameObject("__AvatarParametersPresets__Redirect__");
redirect.transform.parent = go.transform;
var redirectCount = 0;
for (var i = 0; i < presets.presets.Count; i++)
{
var preset = presets.presets[i];
var menu = new GameObject(preset.menuName);
menu.transform.parent = go.transform;
var menuItem = menu.GetOrAddComponent<ModularAvatarMenuItem>();
menuItem.Control = new VRCExpressionsMenu.Control
var menuTransform = go.transform.Find(preset.menuName);
GameObject menu;
if (menuTransform == null)
{
name = preset.menuName,
parameter = new VRCExpressionsMenu.Control.Parameter { name = parameterName },
value = presets.GetPresetParameterValue(i),
type = VRCExpressionsMenu.Control.ControlType.Button,
icon = preset.icon,
};
menu = new GameObject(preset.menuName);
menu.transform.parent = go.transform;
}
else
{
menu = menuTransform.gameObject;
if (menu.GetComponent<ModularAvatarMenuInstaller>() != null)
{
// 直下にあると子も出来てしまう
menuTransform.parent = redirect.transform;
redirectCount++;
}
}
var menuItem = menu.GetOrAddComponent<ModularAvatarMenuItem>();
menuItem.Control = new VRCExpressionsMenu.Control
{
name = preset.menuName,
parameter = new VRCExpressionsMenu.Control.Parameter { name = parameterName },
value = presets.GetPresetParameterValue(i),
type = VRCExpressionsMenu.Control.ControlType.Button,
icon = preset.icon,
};
}
if (redirectCount == 0)
{
Object.DestroyImmediate(redirect);
}
if (redirectCount == presets.presets.Count)
{
Object.DestroyImmediate(parentMenu);
}
}

Expand Down
66 changes: 64 additions & 2 deletions Editor/AvatarParametersSaverPresetGroupDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using nadena.dev.modular_avatar.core;

namespace net.narazaka.vrchat.avatar_parameters_saver.editor
{
Expand All @@ -15,10 +16,15 @@ public class AvatarParametersSaverPresetGroupDrawer : PropertyDrawer
SerializedProperty IndexOffset;
ReorderableList PresetsList;
bool ShowAdvanced;
bool ShowCustomize;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = OnBaseGUI(position, property, label);
if (ShowCreatePresetGUI(property))
{
position = OnCreatePresetGUI(position, property, label);
}
if (ShowPresetContents)
{
OnPresetGUI(position, property, label);
Expand Down Expand Up @@ -61,18 +67,60 @@ public Rect OnBaseGUI(Rect position, SerializedProperty property, GUIContent lab
return position;
}

public Rect OnCreatePresetGUI(Rect position, SerializedProperty property, GUIContent label)
{
UpdatePropertiesIfNeeded(property);
if (ShowCreatePresetGUI(property))
{
EditorGUI.indentLevel++;
ShowCustomize = EditorGUI.Foldout(SingleLineRect(position), ShowCustomize, new GUIContent("Customize"));
position.yMin += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

if (ShowCustomize)
{
var rect = HeightRect(position, EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing);
rect.width -= 60;
EditorGUI.HelpBox(rect, "プリセットと同名の子オブジェクトに MA Menu Installer を付けるとインストール先を個別に選択できます。", MessageType.Info);
rect.x += rect.width;
rect.width = 60;
var menuName = CurrentPreset.FindPropertyRelative(nameof(AvatarParametersSaverPreset.menuName)).stringValue;
var parentTransform = (property.serializedObject.targetObject as Component).transform;
var menuTransform = parentTransform.Find(menuName);
if (menuTransform == null)
{
if (GUI.Button(rect, "Create"))
{
var go = new GameObject(menuName, typeof(ModularAvatarMenuInstaller));
go.transform.SetParent(parentTransform);
EditorGUIUtility.PingObject(go);
}
}
else
{
if (GUI.Button(rect, "Ping"))
{
EditorGUIUtility.PingObject(menuTransform.gameObject);
}
}
position.yMin += (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 2;
}
EditorGUI.indentLevel--;
}
return position;
}

public void OnPresetGUI(Rect position, SerializedProperty property, GUIContent label)
{
UpdatePropertiesIfNeeded(property);
if (ShowPresetContents)
{
EditorGUI.PropertyField(position, CurrentPreset);
EditorGUI.PropertyField(position, CurrentPreset, new GUIContent(CurrentPreset.FindPropertyRelative(nameof(AvatarParametersSaverPreset.menuName)).stringValue));
}
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return GetBasePropertyHeight(property, label) + GetPresetPropertyHeight(property, label);
return GetBasePropertyHeight(property, label) + GetCreatePresetPropertyHeight(property, label) + GetPresetPropertyHeight(property, label);
}

public float GetBasePropertyHeight(SerializedProperty property, GUIContent label = null)
Expand All @@ -82,12 +130,26 @@ public float GetBasePropertyHeight(SerializedProperty property, GUIContent label
return EditorGUIUtility.singleLineHeight * lines + EditorGUIUtility.standardVerticalSpacing * (lines + 2) + PresetsList.GetHeight();
}

public float GetCreatePresetPropertyHeight(SerializedProperty property, GUIContent label = null)
{
UpdatePropertiesIfNeeded(property);
if (!ShowCreatePresetGUI(property)) return 0;
var lines = ShowCustomize ? 3 : 2;
return EditorGUIUtility.singleLineHeight * lines + EditorGUIUtility.standardVerticalSpacing * (lines + 1);
}

public float GetPresetPropertyHeight(SerializedProperty property, GUIContent label = null)
{
UpdatePropertiesIfNeeded(property);
return ShowPresetContents ? EditorGUI.GetPropertyHeight(CurrentPreset) + EditorGUIUtility.standardVerticalSpacing : 0;
}

bool ShowCreatePresetGUI(SerializedProperty property)
{
UpdatePropertiesIfNeeded(property);
return ShowPresetContents && !EditorApplication.isPlaying && (property.serializedObject.targetObject as Component) != null;
}

Rect SingleLineRect(Rect position)
{
return HeightRect(position, EditorGUIUtility.singleLineHeight);
Expand Down

0 comments on commit 455a34e

Please sign in to comment.