public void DrawFields(Type componentType, object component, ECSContext ecsContext, string searchTerm, FieldInfo[] fields) { foreach (FieldInfo field in fields) { string fieldName = field.Name; if (!SearchMatches(searchTerm, fieldName)) { // Does not match search term, skip it continue; } Type fieldType = field.FieldType; VariableAttributes variableAttributes = VariableAttributes.None; // See https://stackoverflow.com/a/10261848 if (field.IsLiteral && !field.IsInitOnly) { variableAttributes = VariableAttributes.Constant; // Prevent SetValue as it will result in a FieldAccessException variableAttributes |= VariableAttributes.ReadOnly; } if (field.IsStatic) { variableAttributes |= VariableAttributes.Static; } string tooltip = TypeUtility.GetTooltip(field, variableAttributes); if ((variableAttributes & VariableAttributes.ReadOnly) != 0) { GUI.enabled = false; } DrawVariable(fieldType, fieldName, component != null ? field.GetValue(component) : null, tooltip, variableAttributes, field.GetCustomAttributes(), true, componentType, newValue => { if ((variableAttributes & VariableAttributes.ReadOnly) == 0) { field.SetValue(component, newValue); #if ECS_EXISTS if (ecsContext != null) { ECSAccess.SetComponentData(ecsContext.EntityManager, ecsContext.Entity, ecsContext.ComponentType, component); } #endif } }); if ((variableAttributes & VariableAttributes.ReadOnly) != 0) { GUI.enabled = true; } } }
public void DrawProperties(Type componentType, object component, string searchTerm, PropertyInfo[] properties) { foreach (var property in properties) { if (property.DeclaringType == typeof(Component) || property.DeclaringType == typeof(UnityEngine.Object)) { continue; } if (!SearchMatches(searchTerm, property.Name)) { // Does not match search term, skip it continue; } if (property.GetIndexParameters().Length != 0) { // Indexer, show it in Methods instead as it takes parameters continue; } MethodInfo getMethod = property.GetGetMethod(true); MethodInfo setMethod = property.GetSetMethod(true); object[] attributes = property.GetCustomAttributes(false); VariableAttributes variableAttributes = VariableAttributes.None; if (getMethod != null && getMethod.IsStatic || setMethod != null && setMethod.IsStatic) { variableAttributes |= VariableAttributes.Static; } if (setMethod == null) { variableAttributes |= VariableAttributes.ReadOnly; } if (getMethod == null) { variableAttributes |= VariableAttributes.WriteOnly; } string tooltip = TypeUtility.GetTooltip(property, variableAttributes); if (getMethod == null) { EditorGUILayout.LabelField(new GUIContent(property.Name, tooltip), new GUIContent("No get method", SidekickEditorGUI.ErrorIconSmall)); } else if (InspectionExclusions.IsPropertyExcluded(componentType, property)) { EditorGUILayout.LabelField(new GUIContent(property.Name, tooltip), new GUIContent("Excluded due to rule", SidekickEditorGUI.ErrorIconSmall, "See InspectionExclusions.cs")); } else if (AttributeHelper.IsObsoleteWithError(attributes)) { // Don't try to get the value of properties that error on access EditorGUILayout.LabelField(new GUIContent(property.Name, tooltip), new GUIContent("[Obsolete] error", SidekickEditorGUI.ErrorIconSmall)); } else { object oldValue = null; Exception error = null; try { oldValue = getMethod.Invoke(component, null); } catch (Exception e) { error = e; } if (error != null) { EditorGUILayout.LabelField(new GUIContent(property.Name, tooltip), new GUIContent(error.GetType().Name, SidekickEditorGUI.ErrorIconSmall)); } else { DrawVariable(property.PropertyType, property.Name, oldValue, tooltip, variableAttributes, property.GetCustomAttributes(), true, componentType, newValue => { setMethod?.Invoke(component, new[] { newValue }); }); } } } }