public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.name == "data" && property.depth > 0) { EditorGUI.PropertyField(position, property, label); return; } var fieldType = property.serializedObject.targetObject.GetType().GetField(property.name).FieldType; var source = UTUtils.GetValueThroughAttribute(property, methodName, out var sourceValType); if (sourceType == PopupSource.Method && fieldType != sourceValType || property.type != "string") { EditorGUI.PropertyField(position, property, label); return; } if (sourceType == PopupSource.Animator) { options = UTUtils.GetAnimatorTriggers(source as Animator).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.UdonBehaviour) { options = UTUtils.GetUdonEvents(source as UdonSharpBehaviour).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.Shader) { options = UTUtils.GetShaderPropertiesByType(source, shaderPropType).Select(o => new GUIContent(o)).ToArray(); } else { options = ((string[])source).Select(o => new GUIContent(o)).ToArray(); } selectedIndex = options.ToList().FindIndex(i => i.text == property.stringValue); if (selectedIndex >= options.Length || selectedIndex < 0) { selectedIndex = 0; } var finalLabel = hideLabel ? new GUIContent() : label; selectedIndex = EditorGUI.Popup(position, finalLabel, selectedIndex, options); property.stringValue = options[selectedIndex].text; }
public override void OnGUI(SerializedProperty property) { var source = UTUtils.GetValueThroughAttribute(property, methodName, out var sourceValType); if (property.name == "data" && sourceType == PopupSource.Method && property.type != "string" && property.type != "int") { EditorGUILayout.PropertyField(property, new GUIContent(property.displayName)); return; } if (property.name != "data") { var fieldType = property.serializedObject.targetObject.GetType().GetField(property.name).FieldType; if (sourceType == PopupSource.Method && fieldType != sourceValType && property.type != "string" && property.type != "int") { EditorGUILayout.PropertyField(property, new GUIContent(property.displayName)); return; } } var numericPopup = property.type == "int" && sourceValType == typeof(int) || property.type == "float" && sourceValType == typeof(float); if (sourceType == PopupSource.AnimatorTrigger) { options = UTUtils.GetAnimatorTriggers(source as Animator).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.AnimatorBool) { options = UTUtils.GetAnimatorBools(source as Animator).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.AnimatorFloat) { options = UTUtils.GetAnimatorFloats(source as Animator).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.AnimatorInt) { options = UTUtils.GetAnimatorInts(source as Animator).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.UdonBehaviour) { options = UTUtils.GetUdonEvents(source as UdonSharpBehaviour).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.UdonProgramVariable) { options = UTUtils.GetUdonVariables(source as UdonSharpBehaviour).Select(o => new GUIContent(o)).ToArray(); } else if (sourceType == PopupSource.Shader) { if (shaderPropType == ShaderPropType.All) { options = UTUtils.GetAllShaderProperties(source).Select(o => new GUIContent(o)).ToArray(); } else { options = UTUtils.GetShaderPropertiesByType(source, shaderPropType).Select(o => new GUIContent(o)).ToArray(); } } else { if (sourceValType == typeof(int)) { options = ((int[])source).Select(i => new GUIContent(i.ToString())).ToArray(); } else if (sourceValType == typeof(float)) { options = ((float[])source).Select(i => new GUIContent(i.ToString())).ToArray(); } else { options = ((string[])source).Select(o => new GUIContent(o)).ToArray(); } } // we want to still support int[] -> int[] and float[] -> float[] mapping if (property.type == "int" && !numericPopup) { selectedIndex = property.intValue; } else if (numericPopup) { if (property.type == "int") { selectedIndex = options.ToList().FindIndex(i => i.text == property.intValue.ToString()); } else { selectedIndex = options.ToList().FindIndex(i => i.text == property.floatValue.ToString()); } if (selectedIndex >= options.Length || selectedIndex < 0) { selectedIndex = 0; } } else { selectedIndex = options.ToList().FindIndex(i => i.text == property.stringValue); if (selectedIndex >= options.Length || selectedIndex < 0) { selectedIndex = 0; } } var finalLabel = hideLabel || property.name == "data" ? new GUIContent() : new GUIContent(property.displayName); selectedIndex = EditorGUILayout.Popup(finalLabel, selectedIndex, options); if (property.type == "int" && !numericPopup) { property.intValue = selectedIndex; } else if (numericPopup) { if (property.type == "int") { property.intValue = Convert.ToInt32(options[selectedIndex].text); } else { property.floatValue = Convert.ToSingle(options[selectedIndex].text); } } else { property.stringValue = options[selectedIndex].text; } }