Skip to content

Commit 0a372a5

Browse files
committed
apply button
1 parent 76fa8a4 commit 0a372a5

File tree

5 files changed

+241
-41
lines changed

5 files changed

+241
-41
lines changed

Core/AvatarChooseMenu.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ void ShowChooseMaterialControl(IList<string> children, string child, Material[]
726726
EditorGUILayout.BeginHorizontal();
727727
var newValue = EditorGUILayout.ObjectField(ChooseName(j), value, typeof(Material), false) as Material;
728728
MaterialPickerButton(child, i, ref newValue);
729+
MaterialApplyButton(child, i, newValue);
729730
EditorGUILayout.EndHorizontal();
730731
if (value != newValue)
731732
{
@@ -904,6 +905,7 @@ void ShowChooseBlendShapeControl(
904905
EditorGUILayout.BeginHorizontal();
905906
var newValue = EditorGUILayout.FloatField(ChooseName(i), value);
906907
BlendShapeLikePickerButton(isBlendShape, child, name.Name, ref newValue);
908+
BlendShapeLikeApplyButton(isBlendShape, child, name.Name, newValue);
907909
EditorGUILayout.EndHorizontal();
908910

909911
if (value != newValue)
@@ -1012,6 +1014,7 @@ IEnumerable<INameAndDescription> names
10121014
EditorGUILayout.BeginHorizontal();
10131015
var newValue = EditorGUILayout.Vector4Field(ChooseName(i), value);
10141016
ShaderVectorParameterPickerButton(child, name.Name, ref newValue);
1017+
ShaderVectorParameterApplyButton(child, name.Name, newValue);
10151018
EditorGUILayout.EndHorizontal();
10161019

10171020
if (value != newValue)
@@ -1115,23 +1118,27 @@ IEnumerable<TypeMember> members
11151118
{
11161119
newValue = EditorGUILayout.FloatField(ChooseName(i), (float)value);
11171120
ValuePickerButton(child, member, p => newValue = p.floatValue);
1121+
ValueApplyButton(child, member, p => p.floatValue = (float)newValue);
11181122
}
11191123
else if (member.MemberType == typeof(int))
11201124
{
11211125
newValue = EditorGUILayout.IntField(ChooseName(i), (int)value);
11221126
ValuePickerButton(child, member, p => newValue = p.intValue);
1127+
ValueApplyButton(child, member, p => p.intValue = (int)newValue);
11231128
}
11241129
else if (member.MemberType == typeof(bool))
11251130
{
11261131
newValue = EditorGUILayout.Toggle(ChooseName(i), (bool)value);
11271132
ValuePickerButton(child, member, p => newValue = p.boolValue);
1133+
ValueApplyButton(child, member, p => p.boolValue = (bool)newValue);
11281134
}
11291135
else if (member.MemberType.IsSubclassOf(typeof(Enum)))
11301136
{
11311137
var enumNames = member.MemberType.GetEnumNamesCached();
11321138
var enumValues = member.MemberType.GetEnumValuesCached();
11331139
newValue = EditorGUILayout.IntPopup(ChooseName(i), (int)value, enumNames, enumValues);
11341140
ValuePickerButton(child, member, p => newValue = enumValues[p.enumValueIndex]);
1141+
ValueApplyButton(child, member, p => p.enumValueIndex = Array.IndexOf(enumValues, (int)newValue));
11351142
}
11361143
else if (member.MemberType == typeof(VRCPhysBoneBase.PermissionFilter))
11371144
{
@@ -1151,6 +1158,7 @@ IEnumerable<TypeMember> members
11511158
EditorGUIUtility.wideMode = true;
11521159
newValue = EditorGUILayout.Vector3Field(ChooseName(i), (Vector3)value);
11531160
ValuePickerButton(child, member, p => newValue = p.vector3Value);
1161+
ValueApplyButton(child, member, p => p.vector3Value = (Vector3)newValue);
11541162
EditorGUIUtility.wideMode = widemode;
11551163
}
11561164
else if (member.MemberType == typeof(Quaternion))
@@ -1159,6 +1167,7 @@ IEnumerable<TypeMember> members
11591167
EditorGUIUtility.wideMode = true;
11601168
newValue = EditorGUILayout.Vector4Field(ChooseName(i), ((Quaternion)value).ToVector4()).ToQuaternion();
11611169
ValuePickerButton(child, member, p => newValue = p.quaternionValue);
1170+
ValueApplyButton(child, member, p => p.quaternionValue = (Quaternion)newValue);
11621171
EditorGUIUtility.wideMode = widemode;
11631172
}
11641173
else if (member.MemberType == typeof(Color))
@@ -1167,6 +1176,7 @@ IEnumerable<TypeMember> members
11671176
EditorGUIUtility.wideMode = true;
11681177
newValue = EditorGUILayout.ColorField(ChooseName(i), (Color)value);
11691178
ValuePickerButton(child, member, p => newValue = p.colorValue);
1179+
ValueApplyButton(child, member, p => p.colorValue = (Color)newValue);
11701180
EditorGUIUtility.wideMode = widemode;
11711181
}
11721182
EditorGUILayout.EndHorizontal();
@@ -1263,6 +1273,7 @@ void ShowTransformComponentControl(IList<string> children, string child, ChooseV
12631273
EditorGUILayout.BeginHorizontal();
12641274
var newValue = EditorGUILayout.Vector3Field(ChooseName(i), value);
12651275
TransformPickerButton(child, title, ref newValue);
1276+
TransformApplyButton(child, title, newValue);
12661277
EditorGUILayout.EndHorizontal();
12671278

12681279
if (value != newValue)
@@ -1384,6 +1395,15 @@ protected override Material[] GetMaterialSlots(string child)
13841395
if (slots == null) slots = ChooseMaterials.MaterialSlots(child);
13851396
return slots;
13861397
}
1398+
1399+
protected override void SetMaterialSlot(string child, int index, Material mat)
1400+
{
1401+
var go = GetGameObject(child);
1402+
if (go != null)
1403+
{
1404+
go.SetMaterialSlot(index, mat);
1405+
}
1406+
}
13871407
#endif
13881408
}
13891409
}

Core/AvatarMenuBase.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55
using net.narazaka.avatarmenucreator.collections.instance;
6+
using Codice.Client.BaseCommands;
7+
68

79
#if UNITY_EDITOR
810
using UnityEditor;
@@ -558,6 +560,8 @@ protected Texture2D TextureField(Rect rect, Texture2D texture2D)
558560

559561
// for MaterialPickerButton
560562
protected virtual Material[] GetMaterialSlots(string child) { throw new NotImplementedException(); }
563+
// for MaterualApplyButton
564+
protected virtual void SetMaterialSlot(string child, int index, Material mat) { throw new NotImplementedException(); }
561565

562566
protected bool PickerButton()
563567
{
@@ -651,6 +655,91 @@ protected void ValuePickerButton(string child, TypeMember member, Action<Seriali
651655
setValue(new SerializedObject(go.GetComponent(member.Type)).FindProperty(member.AnimationMemberName));
652656
}
653657

658+
protected bool ApplyButton()
659+
{
660+
return GUILayout.Button(EditorGUIUtility.IconContent("PlayButton"), GUILayout.Width(20), GUILayout.Height(18));
661+
}
662+
663+
protected void MaterialApplyButton(string child, int index, Material value)
664+
{
665+
var go = GetGameObject(child);
666+
if (go == null || !ApplyButton()) return;
667+
SetMaterialSlot(child, index, value);
668+
}
669+
670+
protected void BlendShapeLikeApplyButton(bool isBlendShape, string child, string name, float value)
671+
{
672+
if (isBlendShape)
673+
{
674+
BlendShapeApplyButton(child, name, value);
675+
}
676+
else
677+
{
678+
ShaderParameterApplyButton(child, name, value);
679+
}
680+
}
681+
682+
void BlendShapeApplyButton(string child, string name, float value)
683+
{
684+
var go = GetGameObject(child);
685+
if (go == null || !ApplyButton()) return;
686+
var mesh = go.GetComponent<SkinnedMeshRenderer>();
687+
Undo.RecordObject(mesh, "AvatarMenuCreator Apply");
688+
mesh.SetBlendShapeWeight(mesh.sharedMesh.GetBlendShapeIndex(name), value);
689+
}
690+
691+
void ShaderParameterApplyButton(string child, string name, float value)
692+
{
693+
var go = GetGameObject(child);
694+
if (go == null || !ApplyButton()) return;
695+
var mesh = go.GetComponent<Renderer>();
696+
foreach (var mat in mesh.sharedMaterials)
697+
{
698+
if (mat.shader.FindPropertyIndex(name) != -1)
699+
{
700+
Undo.RecordObject(mat, "AvatarMenuCreator Apply");
701+
mat.SetFloat(name, value);
702+
return;
703+
}
704+
}
705+
}
706+
707+
protected void ShaderVectorParameterApplyButton(string child, string name, Vector4 value)
708+
{
709+
var go = GetGameObject(child);
710+
if (go == null || !ApplyButton()) return;
711+
var mesh = go.GetComponent<Renderer>();
712+
foreach (var mat in mesh.sharedMaterials)
713+
{
714+
if (mat.shader.FindPropertyIndex(name) != -1)
715+
{
716+
Undo.RecordObject(mat, "AvatarMenuCreator Apply");
717+
mat.SetColor(name, value);
718+
return;
719+
}
720+
}
721+
}
722+
723+
protected void TransformApplyButton(string child, string transformComponentName, Vector3 value)
724+
{
725+
var go = GetGameObject(child);
726+
if (go == null || !ApplyButton()) return;
727+
var component = go.GetComponent<Transform>();
728+
var memberInfo = typeof(Transform).GetProperty(TransformPropertyName(transformComponentName), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
729+
if (memberInfo is System.Reflection.PropertyInfo propertyInfo)
730+
{
731+
Undo.RecordObject(component, "AvatarMenuCreator Apply");
732+
propertyInfo.SetValue(component, value);
733+
}
734+
}
735+
736+
protected void ValueApplyButton(string child, TypeMember member, Action<SerializedProperty> setProperty)
737+
{
738+
var go = GetGameObject(child);
739+
if (go == null || !ApplyButton()) return;
740+
setProperty(new SerializedObject(go.GetComponent(member.Type)).FindProperty(member.AnimationMemberName));
741+
}
742+
654743
protected Rect HorizontalLine()
655744
{
656745
var c = GUI.color;

0 commit comments

Comments
 (0)