Hi,
I have updated to Unity 6 and I can’t get one of my editor scripts to work as it did in older versions of Unity (for example 2022.3.16f1). I have a Editor script that uses the “AssetModificationProcessor.OnWillCreateAsset” method to intercept when a new script is created and adds some custom code/updates to the script file. When I do this now in Unity 6 I get a loop of error messages,
Message: Build asset version error: assets/scripts/MyCustomScript.cs in SourceAssetDB has modification time of '2025-01-04T08:58:47.0647183Z' while content on disk has modification time of '2025-01-04T08:58:47.0763592Z'
This messages continue until I refresh the assets. This did not happen in older versions then the file was updated and everything seems to work fine. I have tried both with and without using the “AssetDatabase.Refresh();” call.
Here is a simple version of my code that does nothing except for reading the .cs file and writing to it again (e.g. the modification time will be changed on the file),
namespace Assets.Editor
{
public class AddCustomText : AssetModificationProcessor
{
public static void OnWillCreateAsset(string path)
{
try
{
if (!path.EndsWith(".cs.meta"))
{
return;
}
path = path.Replace(".meta", "");
var index = path.LastIndexOf(".");
if (index < 0)
{
return;
}
index = Application.dataPath.LastIndexOf("Assets");
path = Application.dataPath.Substring(0, index) + path;
if (!File.Exists(path))
{
Debug.Log("Unable to update file, file not created");
return;
}
var lines = File.ReadAllLines(path);
File.WriteAllLines(path, lines);
AssetDatabase.Refresh();
}
catch (Exception ex)
{
Debug.LogWarning($"Unable to update custom code in script file: '{ex.Message}'");
}
}
}
}
While searching for a solution I have found suggestions to use “AssetPostprocessor.OnPostprocessAllAssets” insted and tried it but I get the same error then.
Am I missing something here or how can I update the script and still make Unity happy?
/Viktor