Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] use JsonUtility to serialise and then deserialise into the new type #37

Closed
Mudloop opened this issue Nov 7, 2023 · 3 comments · Fixed by #41
Closed

[IMPROVE] use JsonUtility to serialise and then deserialise into the new type #37

Mudloop opened this issue Nov 7, 2023 · 3 comments · Fixed by #41
Assignees
Labels
enhancement New feature or request

Comments

@Mudloop
Copy link

Mudloop commented Nov 7, 2023

Improvement destription

When changing a type, it would be good to serialise the current value, and then deserialise into the new object instead of using the activator. That way, any properties they share will be retained.

I would also add a “make unique” button that just clones the object (also using jsonutility), useful after a “duplicate array” operation. This could be a little button next to the selector (or a … button that opens a menu).

@Mudloop Mudloop added the enhancement New feature or request label Nov 7, 2023
@Mudloop
Copy link
Author

Mudloop commented Nov 7, 2023

Here's an example of the first suggestion :

public static object SetManagedReference (this SerializedProperty property,Type type) {
	if(property.managedReferenceValue != null)
	{
		var json = UnityEngine.JsonUtility.ToJson(property.managedReferenceValue);
		var clone = UnityEngine.JsonUtility.FromJson(json, type);
		property.managedReferenceValue = clone;
		return clone;
	}
	object obj = (type != null) ? Activator.CreateInstance(type) : null;
	property.managedReferenceValue = obj;
	return obj;
}

And here's something for the second suggestion :

	if(GUI.Button(new(position.x + 20, position.yMax - 20, position.width, 20), "Make unique")) {
		Undo.RecordObject(property.serializedObject.targetObject, "Make unique");
		SerializedObject individualObject = new(property.serializedObject.targetObjects);
		SerializedProperty individualProperty = individualObject.FindProperty(property.propertyPath);
		individualProperty.managedReferenceValue = property.managedReferenceValue.Clone();
		individualObject.ApplyModifiedProperties();
		individualObject.Update();
		EditorUtility.SetDirty(property.serializedObject.targetObject);
	}

@jlaprof
Copy link

jlaprof commented Dec 29, 2023

The asset is excellent, and both are nice suggestions; the first one is highly appreciated if we use a lot of inheritance, and the second one, you usually won't want a duplicate reference. An easy way to make it unique seems logical... not sure how to implement the UI/UX part...

@mackysoft
Copy link
Owner

Hi @Mudloop
Sorry for the delay.

The latest release supports restoring values from JSON.
https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases/tag/1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants