I got a tiny script that creates a text file in Resources folder before building that needs to be included in the build. So I wrote this script using Unity's IPreprocessBuildWithReport:
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
class MyCustomBuildProcessor : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
File.WriteAllText(
Application.dataPath + "/Resources/version.txt",
string.Format("{0}", PlayerSettings.Android.bundleVersionCode));
AssetDatabase.Refresh();
}
}
Note the AssetDatabase.Refresh();
statement at the end. It makes sure Unity becomes aware of the change and includes the updated file in the build. Now, whenever time I change the bundleVersionCode
and press build, Unity updates the file as expected but the build fails with this pretty generic error:
However, if I try to build the second time with the same bundleVersionCode
than no file changes occur and the build succeeds.
So I guess the AssetDatabase.Refresh()
doesn't really work in OnPreprocessBuild()
, or am I doing something obviously stupid again? Can anyone suggest a workaround?
EDIT:
Please ignore the first two errors, just Unity doesn't like me when I'm excluding files from build by the ~
postfix.