I am creating a property field in the following manner inside of an EditorWindow.
var serializedComponent = new SerializedObject(component);
SerializedProperty property = serializedComponent.FindProperty(outerProperty);
var propertyField = new PropertyField(property);
propertyField.BindProperty(property);
When i unload the scene, the serialized component loses reference to the original serializable object and the property field stops displaying the original values.
I want the UI to remain even after the object is destroyed. I don’t want to draw it out manually for complex types using visual elements myself. Can I do this by stopping the property field from listening to serialized property field change events? I looked into cloning visual elements and storing them but that doesn’t seem to be an option.
I just want to display information about the component attached to a gameobject in the scene even after the scene is unloaded. I will not be editing this via the property field. I’m not sure if using scriptable objects is a good idea because i don’t know what fields my component will have before hand - i want a more generic way of storing data associated w the component.
It would be beneficial, if you posted more code to the issues you are having. As you already got one clear pointer, you need take care what you are modifying, if you want changes to persist. I.e. an instance vs. the original. With most ‘regular’ serialisation stuff Unity performs flawlessly out of the box, thus your case requires further explanation. What exactly are you trying to achieve?
Shouldn’t you try get the information before the scene is unloaded? All dynamic stuff will be gone with the scene unloaded. That’s why you need scriptable objects, so do have a look at them, as advised above, they are suitable to hold information across scenes. Noticeably you should consider scriptable objects can be modified in design time, when you add items to them (e.g. to a list or something, you modify the contents) or if you need additional fields (you modify their structure). Nevertheless, in runtime, you may add contents only temporarily for the duration of the application session. It’s also ambiguous, are you looking to register the name/type of components or are you looking to register the very fields of various component?!? At any rate, you could always resort to writing a custom log of whatever has been going on with the game objects and components of your concern.
Since you are working with serialisation, caution is advised. In our experience you may declare a class serialisable and Unity might even not object, yet custom classes might require custom serialisation.
That’s not going to be possible via a serialized object. When you unload the scene, the object is destroyed, and the serialized object is pointing to a fake-null.
You would have to get this data in a non-serialized form before the unload. Otherwise, you might need to change your approach for whatever you’re doing here.
I did end up using scriptable objects. It is a good solution to store persistent data. However, it’s not very good at serializing entire gameobject data because it only serializes the reference to the gameobject. When my property field references a gameobject and the gameobject’s reference is lost when the scene is unloaded, the scriptable object doesn’t work as well anymore. However, it’s perfect for visualizing primitive data without relying on the gameobject. I decided to switch my approach to not display the property field when the gameobject reference is lost like @spiney199 said.