Search Unity

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:
    78
    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
    Kokowolo likes this.
  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:
    78
    Huh, I hadn't noticed that. Will try, thanks!
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @tomfulghum , I bumped into the same bug but am not able to find it in the issue tracker using the ID you provided. Do you have a direct link to the issue?
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    In the meantime here's a full example with the solution I came up with:

    Code (CSharp):
    1. using UnityEditor;
    2. using System.IO;
    3.  
    4. namespace Ariokan
    5. {
    6.     /// <summary>
    7.     /// Replaces keywords in a Script file with useful data defined by you.
    8.     /// Needs to be in an "Editor" folder to work.
    9.     /// </summary>
    10.     public class KeywordReplace : AssetModificationProcessor
    11.     {
    12.         public static void OnWillCreateAsset(string path)
    13.         {
    14.             if (!path.EndsWith(".cs.meta"))
    15.             {
    16.                 return;
    17.             }
    18.  
    19.             string originalFilePath = AssetDatabase.GetAssetPathFromTextMetaFilePath(path);
    20.             string file = File.ReadAllText(originalFilePath);
    21.  
    22.             //change whatever you want (you can add stuff below, just be sure to add the tags in the script template too!)
    23.             file = file.Replace("#CREATIONDATE#", System.DateTime.Now + "");
    24.             file = file.Replace("#PROJECTNAME#", PlayerSettings.productName);
    25.             file = file.Replace("#SMARTDEVELOPERS#", PlayerSettings.companyName);
    26.  
    27.             //Write the changes in the new script
    28.             File.WriteAllText(originalFilePath, file);
    29.             AssetDatabase.Refresh();
    30.         }
    31.     }
    32. }
    I hope this helps!
     
    Kokowolo likes this.
  6. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    78
    The issue was apparently closed for not being reproducible. I don't have a link at the moment.
    Thanks for the solution!
     
    RikuTheFuffs-U likes this.
  7. brandall

    brandall

    Joined:
    Oct 23, 2016
    Posts:
    1
    FWIW @RikuTheFuffs-U snippet

    Code (CSharp):
    1. if (!path.EndsWith(".cs.meta"))
    2.             {
    3.                 return;
    4.             }
    5.  
    works perfectly to sort out this issue for my keyword replacer as well. There's no way this is not reproducible... :)
     
  8. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60
    Still an issue on 2022.3.15f1 :(
    Thankfully, the solution provided by @RikuTheFuffs-U still works.
     
  9. opsgamestudio

    opsgamestudio

    Joined:
    Dec 4, 2018
    Posts:
    1
    Still an issue on 2022.3.18f1 and the referenced solution still works.