/// <summary> /// Modifies the public fields values, of a target instance, with the values of another instance of the same type. /// Allows to indicate (true) whether the values must be compared in order to apply the change if them are different, /// else (false) the change will be forced for all. /// Returns whether any change -forced or not- was applied. /// </summary> public static bool AlterInstanceFields <TType>(ref TType Target, TType Source, bool ComparePriorToChange) { bool ChangeWasApplied = false; object TargetValue, SourceValue; if (ComparePriorToChange) { foreach (FieldInfo FieldData in typeof(TType).GetFields()) { TargetValue = FieldData.GetValue(Target); SourceValue = FieldData.GetValue(Source); if ((TargetValue != null && SourceValue != null && !TargetValue.Equals(SourceValue)) || (TargetValue == null && SourceValue != null) || (TargetValue != null && SourceValue == null)) { FieldData.SetValue(Target, SourceValue); ChangeWasApplied = true; } } } else { foreach (FieldInfo FieldData in typeof(TType).GetFields()) { FieldData.SetValue(Target, FieldData.GetValue(Source)); } ChangeWasApplied = true; } return(ChangeWasApplied); }
/// <summary> /// Returns a dictionary with the public fields with their FieldInfo and value for the supplied object. /// </summary> public static Dictionary <FieldInfo, object> ExtractFieldInfoAndValueFromInstance(object Datum) { Dictionary <FieldInfo, object> Fields = new Dictionary <FieldInfo, object>(); foreach (FieldInfo FieldData in Datum.GetType().GetFields()) { Fields.Add(FieldData, FieldData.GetValue(Datum)); } return(Fields); }
/// <summary> /// Returns public fields with their name and value for the supplied object. /// </summary> public static Dictionary <string, object> ExtractFieldNameAndValueFromInstance(object Source) { Dictionary <string, object> Fields = new Dictionary <string, object>(); foreach (FieldInfo FieldData in Source.GetType().GetFields()) { Fields.Add(FieldData.Name, FieldData.GetValue(Source)); } return(Fields); }
public float DrawWindow(float offset, List <SerializedProperty> properties, string title) { var spacing = 24; var padding = 1 * spacing; var elementHeight = 24; var startPoint = offset * spacing + padding; var endPoint = properties.Count + padding / spacing; GUILayout.Space((elementHeight * endPoint) + endPoint); GUILayout.BeginArea(new Rect(0, startPoint - padding, Screen.width, elementHeight), titleStyle); GUILayout.Label(title, titleTextStyle, GUILayout.Width(Screen.width)); GUILayout.EndArea(); serializedObject.Update(); for (int i = 0; i < properties.Count; i++) { var property = properties[i]; if (property == null) { continue; } var tooltip = FieldData.GetValue(property.name, typeof(GameConfigSO), FieldData.ValueType.Tooltip); GUILayout.BeginArea(new Rect(0, startPoint + i * spacing, Screen.width, elementHeight), new GUIContent("", tooltip), EditorStyles.helpBox); GUILayout.BeginHorizontal(); var name = FieldData.GetValue(property.name, typeof(GameConfigSO), FieldData.ValueType.Description); if (name == null) { name = property.displayName; } //if (property.type == "bool") if (property.propertyType == SerializedPropertyType.Boolean) { DrawLabel(name, property.type); EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.Width(74)); } //else if (property.type == "int") else if (property.propertyType == SerializedPropertyType.Integer) { //EditorGUILayout.LabelField(name); DrawLabel(name, property.type); EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.Width(74)); } //else if (property.type == "string") else if (property.propertyType == SerializedPropertyType.String) { //EditorGUILayout.LabelField(name, GUILayout.Width(230)); DrawLabel(name, property.type); //EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.Width(Screen.width - 250)); EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.Width(334)); } else if (property.propertyType == SerializedPropertyType.ObjectReference) { DrawLabel(name, property.type); EditorGUILayout.PropertyField(property, GUIContent.none, true, GUILayout.Width(184)); } GUILayout.EndHorizontal(); GUILayout.EndArea(); } serializedObject.ApplyModifiedProperties(); return(endPoint); }