Search Unity

AssetDatabase.SaveAssets not saving

Discussion in 'Asset Database' started by Languard, Apr 17, 2020.

  1. Languard

    Languard

    Joined:
    Nov 2, 2009
    Posts:
    8
    I'm trying to gain an understanding of how AssetDatabase works by loading a Texture2D with LoadAssetAtPath, changing a value, and saving the change back. The loading part is working just fine, the changing part is working just fine, but the saving part isn't working at all. Here is the relevant code:

    Code (CSharp):
    1.         private void OnGUI()
    2.         {
    3.             EditorGUILayout.BeginVertical();
    4.             if (icon != null)
    5.             {
    6.                 EditorGUILayout.LabelField(icon.name);
    7.                 icon.anisoLevel = (int)EditorGUILayout.Slider(icon.anisoLevel, 0, 15);
    8.                 if (EditorUtility.IsDirty(icon.GetInstanceID()))
    9.                 {
    10.                     EditorGUILayout.LabelField("Is dirty!");
    11.                     AssetDatabase.SaveAssets();                          
    12.                 }
    13.             }
    14.             else EditorGUILayout.LabelField("Asset not found");
    15.             EditorGUILayout.EndVertical();
    16.         }
    I should also mention that this is taking place in an EditorWindow. When I move the slider the "Is Dirty!" message pops up, but then never goes away and when looking at the asset on disk in the inspector the property is still the default value, 1. I have tried putting in EditorUtility.SetDirty, but it didn't do anything. What am I not understanding here? How do I get this to save the change to disk?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,065
    There's a difference between imported assets and native assets. Imported assets are all that are brought into Unity from other applications and that Unity needs to convert to its own representation (images, audio files, models, scripts, etc). Native assets are ones created in Unity that are already in the representation that Unity needs (prefabs, scriptable objects, materials etc).

    Native assets are edited directly, if you load them with the asset database and change them (properly marking them dirty where necessary), the changes will be saved back to disk.

    Imported assets, on the other hand, are read-only. The original file is the source and the imported asset will be re-created from the original file and any changes you've made overwritten. You'll need to edit the original file if you want to make permanent changes. If you want to change the import settings, you'll need to use the AssetImporter API and them reimport the asset.
     
    Languard likes this.
  3. Languard

    Languard

    Joined:
    Nov 2, 2009
    Posts:
    8
    That is a very subtle difference, but it makes sense. Thank you for clearing that up for me.