Updating renamed bound elements.

Heya, I’m having trouble getting bound elements to update their name, nothing I do seems to matter.

I threw together a script to do… well basically anything I could think of to get the name to update, however it will not update the name until I close the window and open it again, despite the asset being successfully renamed in the editor.

            renamer.RegisterCallback<BlurEvent>(evnt =>
            {
                if (property.objectReferenceValue)
                {
                    AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue), renamer.value);
                    AssetDatabase.Refresh();
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue));
                    AssetDatabase.SaveAssets();
                    property.objectReferenceValue.name = renamer.value;
                    property.serializedObject.ApplyModifiedProperties();
                    property.serializedObject.Update();
                    scriptableObjectField.MarkDirtyRepaint();
                    rootElement.MarkDirtyRepaint();
                }
            });

(Full code if it’s helpful hatebin )

Thanks for your time.

Since the actual value of “property” does not change, the bound ObjectField is not updated. We seem to not be tracking the “name” when we check for changes in our bindings code.

A couple of things to note:

  1. verify that the asset reference you get back from your ImportAsset() is still the exact same instance (via instanceId) as your current property.objectReferenceValue
  2. all the MarkDirtyRepaint(), Update() and ApplyModifiedProperties() calls are redundant, because you haven’t actually changed the serializedObject in any way. You’ve changed a property on an object the serializedObject is referencing (one step removed)
  3. try just changing the name on the objectReferenceValue without the re-import and asset db change
  4. worst case, call Unbind() and Bind() on the container element containing your object PropertyFields (or ObjectFields)

Thanks for the reply, here’s what I’ve found.

                    AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue), renamer.value);
                    AssetDatabase.Refresh();
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue));
                    AssetDatabase.SaveAssets();
                   
                    property.objectReferenceValue.name = renamer.value;
                    Debug.Log(property.objectReferenceValue.GetInstanceID());

The reference id’s do match up after the rename (In my case, my instance ID was -2770 and the renamed asset still had an instance ID of -2770)

Good to know, I’ll remove these. Was essentially just trying things at random ;p.

Just doing

property.objectReferenceValue.name = renamer.value;

Behaves essentially the same way, except of course the asset file’s name stays the same. It does reflect the name change in the property, but only after clicking on a different object and letting the inspector rebuild.

Bind and unbind also behave the same way, using these as trials they behave exactly the same as above.

                    AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue), renamer.value);
                    AssetDatabase.Refresh();
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(property.objectReferenceValue));
                    AssetDatabase.SaveAssets();
                    property.objectReferenceValue.name = renamer.value;
                    scriptableObjectField.Unbind();
                    scriptableObjectField.BindProperty(property);

or

                    property.objectReferenceValue.name = renamer.value;
                    scriptableObjectField.Unbind();
                    scriptableObjectField.BindProperty(property);

I’ve even tried forcing CreatePropertyGUI to run again, but it still does not update

                    property.objectReferenceValue.name = renamer.value;
                    rootElement = CreatePropertyGUI(property);

Silly issue but it’s sure not an easy fix!

Thanks again for your time =]

I got this working by simply changing the name first then calling SaveAssets() and finally calling RenameAssets()+Refresh()