Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug FileNotFoundException when reading asset contents in OnWillCreateAsset

Discussion in 'Editor & General Support' started by tomfulghum, Feb 14, 2023.

  1. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    50
    We've successfully been using the AssetModificationProcessor.OnWillCreateAsset event to replace some keywords in our script files when they are created. However, since we moved from Unity 2021.3.14 to 2022.2.3 for a new project, we're getting a FileNotFoundException when trying to read the contents of the asset corresponding to the meta file we get as parameter.

    I've removed some validation and the actual processing logic, but this is the code that triggers the exception:
    Code (CSharp):
    1. public static void OnWillCreateAsset(string metaFilePath)
    2. {
    3.     string assetPath = AssetDatabase.GetAssetPathFromTextMetaFilePath(metaFilePath);
    4.     string originalContent = File.ReadAllText(assetPath); // Throws FileNotFoundException
    5.  
    6.     // Process file
    7. }
    Has anything changed in how OnWillCreateAsset works that we don't know about?

    Edit: We submitted a bug report (case IN-32147) but any insight is still appreciated!
     
    Last edited: Feb 14, 2023
  2. ropemonkey

    ropemonkey

    Joined:
    Apr 28, 2014
    Posts:
    6
    We ran into this as well. We noted that OnWillCreateAsset was being called for both the assetPath and the metaFilePath in 2022.2.X, whereas previous to our editor upgrade it was only being called for the meta file. Once we hardened our code to early out if File.Exists(assetPath) didn't return true, things went back to working for us.
     
    Hinuko likes this.
  3. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    50
    Huh, I hadn't noticed that. Will try, thanks!