private void RenderHelpBox(SerializedProperty prop) { var helpBoxAttr = UTUtils.GetPropertyAttribute <HelpBoxAttribute>(prop); if (helpBoxAttr != null) { UTStyles.RenderNote(helpBoxAttr.text); } }
private void RenderStackedArray(string name, SerializedProperty prop, SerializedProperty otherProp, PopupAttribute leftPopup, PopupAttribute rightPopup, string addMethod, string addText, string changedCallback) { prop.isExpanded = UTStyles.FoldoutHeader($"{name} [{prop.arraySize}]", prop.isExpanded); if (!prop.isExpanded) { return; } for (int i = 0; i < prop.arraySize; i++) { EditorGUILayout.BeginHorizontal(); if (RenderPositionControls(i, new[] { prop, otherProp })) { break; } // this code is very similar to PopupAttribute itself // but we have to handle it here directly because we are connecting two different props together // should probably refactor at some point EditorGUI.BeginChangeCheck(); // Left Field if (leftPopup == null) { EditorGUILayout.PropertyField(prop.GetArrayElementAtIndex(i), new GUIContent()); } else { string[] options; var sourceType = leftPopup.sourceType; var source = prop.GetArrayElementAtIndex(i); // Right Field if (sourceType == PopupAttribute.PopupSource.Animator) { options = UTUtils.GetAnimatorTriggers(source.objectReferenceValue as Animator); } else if (sourceType == PopupAttribute.PopupSource.UdonBehaviour) { options = UTUtils.GetUdonEvents(source.objectReferenceValue as UdonBehaviour); } else if (sourceType == PopupAttribute.PopupSource.Shader) { var propsSource = UTUtils.GetValueThroughAttribute(source, leftPopup.methodName, out _); options = UTUtils.GetShaderPropertiesByType(propsSource as Shader, leftPopup.shaderPropType); } else { options = (string[])UTUtils.GetValueThroughAttribute(prop, leftPopup.methodName, out _); } var selectedIndex = options.ToList().IndexOf(prop.GetArrayElementAtIndex(i).stringValue); if (selectedIndex >= options.Length || selectedIndex == -1) { selectedIndex = 0; } selectedIndex = EditorGUILayout.Popup(selectedIndex, options); prop.GetArrayElementAtIndex(i).stringValue = options[selectedIndex]; } if (rightPopup == null) { EditorGUILayout.PropertyField(otherProp.GetArrayElementAtIndex(i), new GUIContent()); } else { string[] options; var sourceType = rightPopup.sourceType; var source = prop.GetArrayElementAtIndex(i); // Right Field if (sourceType == PopupAttribute.PopupSource.Animator) { options = UTUtils.GetAnimatorTriggers(source.objectReferenceValue as Animator); } else if (sourceType == PopupAttribute.PopupSource.UdonBehaviour) { options = UTUtils.GetUdonEvents(source.objectReferenceValue as UdonBehaviour); } else if (sourceType == PopupAttribute.PopupSource.Shader) { var propsSource = UTUtils.GetValueThroughAttribute(source, rightPopup.methodName, out _); options = UTUtils.GetShaderPropertiesByType(propsSource as Shader, rightPopup.shaderPropType); } else { options = (string[])UTUtils.GetValueThroughAttribute(otherProp, rightPopup.methodName, out _); } var selectedIndex = options.ToList().IndexOf(otherProp.GetArrayElementAtIndex(i).stringValue); if (selectedIndex >= options.Length || selectedIndex == -1) { selectedIndex = 0; } selectedIndex = EditorGUILayout.Popup(selectedIndex, options); otherProp.GetArrayElementAtIndex(i).stringValue = options[selectedIndex]; } if (EditorGUI.EndChangeCheck()) { if (changedCallback != null) { var m = t.GetType().GetMethod(changedCallback); if (m != null) { m.Invoke(t, m.GetParameters().Length > 2 ? new object[] { prop.GetArrayElementAtIndex(i), otherProp.GetArrayElementAtIndex(i), i } : new object[] { prop, otherProp }); } } } if (RenderRemoveControls(i, new[] { prop, otherProp })) { if (changedCallback != null) { var m = t.GetType().GetMethod(changedCallback); if (m != null) { m.Invoke(t, m.GetParameters().Length > 2 ? new object[] { null, null, i } : new object[] { prop, otherProp }); } } break; } EditorGUILayout.EndHorizontal(); } if (RenderAddControls(new[] { prop, otherProp }, addText, addMethod)) { if (changedCallback != null) { var m = t.GetType().GetMethod(changedCallback); if (m != null) { m.Invoke(t, m.GetParameters().Length > 2 ? new object[] { prop.GetArrayElementAtIndex(prop.arraySize), otherProp.GetArrayElementAtIndex(prop.arraySize), prop.arraySize } : new object[] { prop, otherProp }); } } } }
private void HandleArray(SerializedProperty prop) { var attrs = UTUtils.GetPropertyAttributes(prop); var modifierAttrs = UTUtils.GetPropertyAttributes <Attribute>(prop); var isGroup = attrs.Where(a => a is ListViewAttribute).ToArray().Length > 0; var groupAttribute = attrs.Select(a => a as ListViewAttribute).ToArray(); var onValueChangedAttribute = UTUtils.GetPropertyAttribute <OnValueChangedAttribute>(prop); // hideIf attribute var hideIfAttribute = modifierAttrs.OfType <HideIfAttribute>().ToArray(); var isHidden = hideIfAttribute.Length > 0 && UTUtils.GetVisibleThroughAttribute(prop, hideIfAttribute[0].methodName, false); // handling for regular arrays, make nicer down the line if (!isGroup) { if (isHidden) { return; } RenderArray(prop, onValueChangedAttribute?.methodName); RenderHelpBox(prop); return; } var groupName = groupAttribute[0].name; var items = t.GetType().GetFields().Where(f => f.GetAttribute <ListViewAttribute>() != null && f.GetAttribute <ListViewAttribute>().name == groupName) .ToList(); // fast exit on 1 element with list view if (items.Count < 2) { if (isHidden) { return; } RenderArray(prop, onValueChangedAttribute?.methodName); RenderHelpBox(prop); return; } var index = items.FindIndex(a => a.Name == prop.name); if (index > 0) { return; } var sectionHeaderAttribute = UTUtils.GetPropertyAttribute <SectionHeaderAttribute>(prop); if (sectionHeaderAttribute != null) { UTStyles.RenderSectionHeader(sectionHeaderAttribute.text); } if (isHidden) { return; } var otherProp = serializedObject.FindProperty(items[1].Name); var leftPopupAttribute = UTUtils.GetPropertyAttribute <PopupAttribute>(prop); var rightPopupAttribute = UTUtils.GetPropertyAttribute <PopupAttribute>(otherProp); if (rightPopupAttribute != null || leftPopupAttribute != null) { RenderStackedArray(groupAttribute[0].name, prop, otherProp, leftPopupAttribute, rightPopupAttribute, groupAttribute[0].addMethodName, groupAttribute[0].addButtonText, onValueChangedAttribute?.methodName); RenderHelpBox(prop); return; } RenderStackedArray(groupAttribute[0].name, prop, otherProp, groupAttribute[0].addMethodName, groupAttribute[0].addButtonText, onValueChangedAttribute?.methodName); RenderHelpBox(prop); }