-
Notifications
You must be signed in to change notification settings - Fork 33
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
Read and Modify a SceneReference field in a custom Editor through SerializedProperty #108
Comments
using serializedObject.ApplyModifiedProperties(); It throws an error saying "Unsupported type SceneReference" :( |
Hi @lucypero. I am in the process of writing a readme section with examples for how to access and manipulate a SceneReference through editor code (both custom editors and editor windows). It can take me a couple of days. In the meantime however, to get you started quickly, I will share a summary here: If you have a field that is directly accessible in the editor script (public or internal with The reason If you are on a version that does not have private SerializedProperty _privateSceneRefProp;
private FieldInfo _privateSceneRefField;
private SceneReference PrivateSceneRef
{
get => (SceneReference)_privateSceneRefField.GetValue(target);
set => _privateSceneRefField.SetValue(target, value);
}
private void OnEnable()
{
_privateSceneRefProp = serializedObject.FindProperty("privateSceneRef");
_privateSceneRefField = target.GetType().GetField(_privateSceneRefProp.name, BindingFlags.Instance | BindingFlags.NonPublic);
} Afterwards you can use |
Thank you for the response.
Yeah, that's what I'm doing now. Doing something like this works Undo.RecordObject(target, "Modify Destination Scene");
theTargetComponent.DestinationScene = destinationSceneRef; // setting the SceneReference
Scene currentScene = theTargetComponent.gameObject.scene;
EditorSceneManager.MarkSceneDirty(currentScene); I was wondering if I could also do it through
As i said here, I tried with boxedValue but the serialized object refuses to apply the changes. It throws an error saying "Unsupported type SceneReference". I don't know if what you said after this addresses this. I don't think so. Correct me if I'm wrong. |
Is this an insurmountable limitation of Unity's SerializedObject? It just seems odd that it wouldn't support SceneReference, as it's a competely serializable type. I don't understand why it has to be a Unity |
I share the same frustration. Unity's editor code has many 'gotcha' moments where it just doesn't support things. The problem here stems from how It seems
I will experiment with |
@lucypero Thank you for the updates. I saw other people reporting this error on the Unity forums and they reached the same conclusion. I don't think fixing outdated serialization is within our scope. It would be a very invasive piece of editor code as we would need to operate on the asset containing the I will keep this issue open to track the documentation change. Let me know if there are any other problems with your use case. |
Ok. To wrap it up, this is what works for sure: // `theTargetComponent` being the component that holds a SceneReference field that you want to modify and serialize
Undo.RecordObject(theTargetComponent, "Modify Destination Scene");
theTargetComponent.DestinationScene = destinationSceneRef; // setting the SceneReference
EditorUtility.SetDirty(theTargetComponent); |
Usage in Editor Code section has been added to the README file. It will be included in the next release. |
Is it possible to read and modify a SceneReference field in a custom Editor through SerializedProperty, without wrapping the SceneReference in a ScriptableObject?
Related: #68
We've been wrapping scene references in scriptable objects just to do this, but I don't think it should be necessary
Maybe I don't know how to use
SerializedProperty
well.The text was updated successfully, but these errors were encountered: