public void addElement(Type element) { Undo.RegisterCompleteObjectUndo(inspectorTarget, "SaveableComponent added"); ///since an actual instance can not be used as a prefab value the type name ///is stored, and the instance is created at runtime. SaveableComponent addedComponent = (SaveableComponent)inspectorTarget.gameObject.AddComponent(element); if (element.BaseType.IsGenericType) { Type type = element.BaseType.GetGenericArguments()[0]; Component component = inspectorTarget.gameObject.GetComponent(type); if (component == null) { // DialogWindow window = new DialogWindow(this); // PopupWindow.Show(new Rect(new Vector2(0, 350), new Vector2(250, 150)), window); component = inspectorTarget.gameObject.AddComponent(type); } addedComponent.BaseComponent = component; } ///Unity documentation disadvises to use this function in this situation, ///but no other solution worked for saving the new list element in the scene, ///without losing the reference after entering and leaving play mode. EditorUtility.SetDirty(inspectorTarget); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var targetObject = property.serializedObject.targetObject; var targetObjectClassType = targetObject.GetType(); var field = targetObjectClassType.GetField(property.propertyPath); if (field != null) { object value = field.GetValue(targetObject); string fieldName = firstToUpper(field.Name); component = (SaveableComponent)value; Object choosenObject = EditorGUI.ObjectField(position, fieldName, component.BaseComponent, component.getGenericType(), true); if (component.BaseComponent != choosenObject) { Undo.RegisterCompleteObjectUndo(targetObject, "changed " + fieldName); Component choosenComponent = (choosenObject as Component); component.BaseComponent = choosenComponent; ///Unity documentation disadvises to use this function in this situation, ///but no other solution worked for saving the new list element in the scene, ///without losing the reference after entering and leaving play mode EditorUtility.SetDirty(targetObject); } } }
/// <summary> /// stores all fieldValues which are Serializable into the serialization info /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void transferComponentSaving(SaveableComponent source, Dictionary <string, object> target) { FieldInfo[] targetFields = getFieldsFromSaveableComponent(source.GetType()); foreach (FieldInfo f in targetFields) { target.Add(f.Name, getValue(f.GetValue(source))); } }
private void OnValidate() { // Set a new save identification if it is not a prefab at the moment. if (this.gameObject.scene.name != null) { if (saveIdentification.UseConstant && string.IsNullOrEmpty(saveIdentification.ConstantValue)) { saveIdentification.ConstantValue = (System.Guid.NewGuid().ToString()); } } else { // If the prefab has a string of length 32, we can assume it is a GUID if (saveIdentification.ConstantValue.Length == 36) { saveIdentification.ConstantValue = string.Empty; EditorUtility.SetDirty(this.gameObject); } } ISaveable[] saveables = GetComponentsInChildren <ISaveable>(true); if (saveables.Length != saveableComponents.Count) { if (saveableComponents.Count > saveables.Length) { for (int i = saveableComponents.Count - 1; i >= saveables.Length; i--) { saveableComponents.RemoveAt(i); } } saveableComponents.RemoveAll(s => s.monoBehaviour == null); ISaveable[] cachedSaveables = new ISaveable[saveableComponents.Count]; for (int i = 0; i < cachedSaveables.Length; i++) { cachedSaveables[i] = saveableComponents[i].monoBehaviour as ISaveable; } ISaveable[] missingElements = saveables.Except(cachedSaveables).ToArray(); for (int i = 0; i < missingElements.Length; i++) { SaveableComponent newSaveableComponent = new SaveableComponent() { monoBehaviour = missingElements[i] as MonoBehaviour }; if (newSaveableComponent.identifier.UseConstant == true) { string typeString = newSaveableComponent.monoBehaviour.GetType().ToString(); string guidString = System.Guid.NewGuid().ToString().Substring(0, 5); newSaveableComponent.identifier.ConstantValue = $"{typeString} {guidString}"; } saveableComponents.Add(newSaveableComponent); } } }