Skip to content

Commit f695c28

Browse files
committed
apply button
1 parent 76fa8a4 commit f695c28

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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ protected Texture2D TextureField(Rect rect, Texture2D texture2D)
558558

559559
// for MaterialPickerButton
560560
protected virtual Material[] GetMaterialSlots(string child) { throw new NotImplementedException(); }
561+
// for MaterualApplyButton
562+
protected virtual void SetMaterialSlot(string child, int index, Material mat) { throw new NotImplementedException(); }
561563

562564
protected bool PickerButton()
563565
{
@@ -651,6 +653,91 @@ protected void ValuePickerButton(string child, TypeMember member, Action<Seriali
651653
setValue(new SerializedObject(go.GetComponent(member.Type)).FindProperty(member.AnimationMemberName));
652654
}
653655

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

0 commit comments

Comments
 (0)