No way to rename Sub Assets properly

I’m trying to rename a Scriptable Object, which is a sub asset within another Scriptable Object.

The eventual goal is to create a localization index which contains a list of keys. I want each key to become a referenceable Scriptable Object under the index SO automatically.

The name of the sub assets must become the key that was entered in the list on the main asset. I have this working by doing this:

for (int o = 0; o < keyObjects.arraySize; o++) {
  SerializedObject keyObj = new SerializedObject(keyObjects.GetArrayElementAtIndex(o).objectReferenceValue);
  SerializedProperty objKey = keyObj.FindProperty("key");
  SerializedProperty objIndex = keyObj.FindProperty("index");
  SerializedProperty objName = keyObj.FindProperty("m_Name");

  string useKey = keys.GetArrayElementAtIndex(objIndex.intValue).stringValue;
  objKey.stringValue = useKey;
  objName.stringValue = objIndex.intValue + "-" + useKey;

  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(keyObj.targetObject), ImportAssetOptions.ForceUpdate);

The main problem is that the Project View is not updated properly. The sub assets are renamed, but it doesn’t show until you collapse and re-expand the main SO object. I’ve tried many different methods to try getting it to work, with no success. Reimporting the asset doesn’t work, refreshing the AssetDatabase doesn’t work, repainting the Project View doesn’t work.

The method AssetDatabase.Rename works for the main asset. If I use it for the sub asset, everything breaks. I’ve tried setting the sub asset as the Main Asset temporarily, rename it, then make the original main asset the main asset again, but this just breaks down entirely with weird errors.

Is there any legit way to rename Sub Assets? And if not, what went wrong in the development of these features? This sounds like a relatively easy feature to add.

There is a way. Using the above method to rename the sub asset, you then need to:

  • Add a new sub asset
  • AssetDatabase.Refresh();
  • Remove the newly added sub asset.

That is the only way to force a repaint of the project view. Which, to be honest, is ridiculous.

So you can rename the sub-asset by using its .name property.

This WILL work internally, but its result won’t show in the Project Windows until you forcibly repaint them.

They don’t have an easy way to do this, so I used the following silly code that works for me in Unity 2020.3.32f1:

/// <summary>
/// Forces Unity to repaint the project windows. This seems to be the only solution that works after renaming a sub-asset.
/// The following do NOT work:
/// <list type="bullet">
/// <item><see cref="AssetDatabase.Refresh"/></item>
/// <item><see cref="AssetDatabase.SaveAssets"/></item>
/// <item><see cref="UnityEditor.Editor.Repaint"/></item>
/// </list>
/// </summary>
private static void ForceUpdateProjectWindows() {
	string tempAssetPath = "Assets/temp-987654321.anim";
	AssetDatabase.CreateAsset(new AnimationClip(), tempAssetPath);