5

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:

enter image description here

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.

Nika Kasradze
  • 2,834
  • 3
  • 25
  • 48
  • I can't tell why there are these down-votes. The questions is clear enough and with MCVE. Did you try res-tarting the Editor? Sometimes when I get an error simple restarting is enough to fix it – Programmer May 11 '18 at 11:55
  • yep, I mean this error has been here for a while now, I just build twice out of habit already :D – Nika Kasradze May 11 '18 at 11:57
  • If you remove `AssetDatabase.Refresh();`, do you still get that error? If yes, you may not even need `AssetDatabase.Refresh();` as the file will still be included in the build. Check the Build and see if the updated file is included in the build without `AssetDatabase.Refresh();` – Programmer May 11 '18 at 12:04
  • when I remove the `AssetDatabase.Refresh();` the build includes the old version of the file instead of the new one. Then after the build Unity refreshes the AssetDatabase by itself so on the next build the updated file is included. – Nika Kasradze May 11 '18 at 12:09
  • But do you see the error without `AssetDatabase.Refresh()`? – Programmer May 11 '18 at 13:02
  • no, no error without `AssetDatabase.Refresh()`. But updated file does not go with the build. – Nika Kasradze May 13 '18 at 06:50
  • I hope this is your actual code because right now, there is an extra `)` in your `File.WriteAllText` function which means that won't compile. Your code is for Unity 2018. I use 2017 and modified it for 2017 and tested it. It works fine. There are two possible issues: 1.A problem with Unity(bug). 2. A problem with the special folders. Can you remove all the special folders? Move them out of the project temporary and see if this issue still exist. – Programmer May 13 '18 at 13:49
  • Thanks for noting ) character, yes I minified the code before posting and didn't notice. I've just realized that I'm using Cache Server and that might be an issue. After re-import the file is uploaded to the server and most probably it's interfering with a build process. – Nika Kasradze May 14 '18 at 17:01

0 Answers0