Search Unity

AssetImporter.userData not writing to the meta file in 4.6?

Discussion in 'UGUI & TextMesh Pro' started by webbut, Sep 9, 2014.

  1. webbut

    webbut

    Joined:
    Jun 25, 2014
    Posts:
    20
    To my understanding getting an asset importer for an object and setting the userData would set the "userData:" section of the meta file for that asset but, that doesn't seem to be working.

    I have a simple editor script that gets the filepath of the selected object then gets the AssetImporter for it and sets the userData. If i display the userData from the accessor in the AssetImporter the output is correct however the meta file is never changed so if I close unity and start it up again and try to output the userData from AssetImporter's userData accessor it is empty.
     
  2. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    AssetDatabase.SaveAssets()?
     
    webbut likes this.
  3. webbut

    webbut

    Joined:
    Jun 25, 2014
    Posts:
    20
    I tried using AssetDatabase.SaveAssets() right after modifying the userData and it still didn't work. I even made sure that the SaveAssets was being called by making a AssetModificationProcessor and making a OnWillSaveAssets that just wrote a debug log to say that it was about to save assets and the log was happening but the meta files were still not being updated.
     
  4. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    The YouTube video "Hack Spectrum" (part of Unite 2014) had a guy who did exactly what you're doing, so maybe go look up that video and compare your code to his. They omitted the video from the Unite 2014 archive on this site, but it's on the official Unity channel with all the other Unite 2014 videos.

     
    npatch and webbut like this.
  5. webbut

    webbut

    Joined:
    Jun 25, 2014
    Posts:
    20
    hahah I was at that presentation, it's the entire reason I am trying this. I'm pretty sure the guy who presented the talk wasn't using the 4.6 beta and i was worried it might be a bug in the 4.6 beta problem. I don't think he put up the source code for that example and is explanation made it sound like all you had to do was get the AssetImporter and get/set the userData.

    I couldn't find any other save functions and I was just going to use normal C# file IO to open the file and append the userData myself but it my code couldn't open the file while unity was running.
     
  6. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    I assume you also tried SetDirty?
     
    webbut likes this.
  7. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,578
    @webbut I'm having the same problem in 4.5.3.

    It was noted here that if you delete the .meta files outside of Unity and then go back to Unity it gets created again, but with the user data.
     
  8. webbut

    webbut

    Joined:
    Jun 25, 2014
    Posts:
    20
    So what I had to do was modify the AssetImporter.UserData then call EditorUtility.SetDirty() on the UnityEngine.Object I had the AssetImporter for and finally call AssetDatabase.SaveAssets()

    Thanks for all the help guys.
     
  9. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,578
    I couldn't get this to work either in 4.5.3.
     
  10. webbut

    webbut

    Joined:
    Jun 25, 2014
    Posts:
    20
    I cut out what I'm doing into a C# function that can be dropped in essentially any Unity editor script just to test if it's working. As long as the passed in UnityEngine.Object is something that has a meta file associated with it this code should work to edit the userData right away.

    public void EditMeta(UnityEngine.Object obj)
    {
    string path = AssetDatabase.GetAssetPath(obj);
    if (path == String.Empty)
    {
    Debug.Log("EditMeta: Can't find a path for " + obj.name);
    }
    else
    {
    AssetImporter import = AssetImporter.GetAtPath(path);
    import.userData += "Edited!";
    EditorUtility.SetDirty(obj);
    AssetDatabase.SaveAssets();
    Debug.Log("EditMeta: Success!");
    }
    }
     
  11. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    What Object *doesn't* have a .meta associated with it? Even directories have .meta attached.
     
  12. Coldcalifornian

    Coldcalifornian

    Joined:
    Feb 11, 2014
    Posts:
    14
    Many thanks webbut! I was also at that presentation, and am just now getting around to implementing the userData trick.
     
  13. entheh

    entheh

    Joined:
    Jan 3, 2015
    Posts:
    1
    Have you guys tried AssetDatabase.WriteImportSettingsIfDirty(string path)? We used that at work and I'm sure this problem went away then. It should be more efficient than SetDirty() and SaveAssets() because it doesn't force the object itself to be loaded and re-saved.

    First post by the way - be gentle :)

    [EDIT]
    Actually, I think this problem was more subtle, because if we had the offending object selected when we tested for the problem, then the problem didn't show up. It's possible our custom editor was buggy and was marking the object dirty by accident, but my memory is that simply having the object loaded (which happens if it's selected) would cause Unity to write the .meta file out on the next SaveAssets. In any case, rigorously using WriteImportSettingsIfDirty definitely solved the problem for us.
     
    Last edited: Jan 7, 2015
    Seromu and DavidButtressLkwd like this.
  14. Unreal-Vision

    Unreal-Vision

    Joined:
    May 6, 2013
    Posts:
    58
    Thanks entheh for the trick. It works for us!
     
  15. Jason-Michael

    Jason-Michael

    Joined:
    Jul 6, 2015
    Posts:
    18
    AssetImporter.SaveAndReimport() will do the work.
     
unityunity