AssetBundle Failing to Build on ASP.NET

Hello,

Long time reader, first time poster.
I have written a Unity project where a user can upload an asset to an endpoint and the endpoint will generate an AssetBundle. Right now it is just running on my computer.

The idea is the endpoint computer runs the command prompt Unity editor code and bundles it with:

AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles("Assets/AssetBundles", buildMap, BuildAssetBundleOptions.None, BuildTarget.WSAPlayer);

Which works fine and great in the Unity GUI
Works fine and great on the Command Prompt
Crashes on the ASP.NET site.

The site is running my user so I don’t believe it is a permissions issue, but seems to be an issue with the project.lock.json and project.json files.

Here are somethings I observed:

  • Running in GUI created the project.json and project.lock.json files
  • Running in Command Prompt re-used the files
  • Running on the .NET site DELETES the files
  • Building a Windows Standalone AssetBundle works fine on .NET, but WSAPlayer fails

I did take the liberty of saving the log files

Command Prompt log file
Platform assembly: C:\program files\Unity\Editor\Data\Managed\Unity.CecilTools.dll (this message is harmless)
Restoring NuGet packages from ‘C:\uploadServer\Unity\commandPromptCode\UWP\project.json’.
DisplayProgressbar: Building Player
NuGet packages successfully restored.
*There are also cases where it reuses the project.json if available

.NET log file
Platform assembly: C:\program files\Unity\Editor\Data\Managed\Unity.CecilTools.dll (this message is harmless)
Restoring NuGet packages from ‘C:\uploadServer\Unity\commandPromptCode\UWP\project.json’.
DisplayProgressbar: Building Player
Project lock file validation failed.
DisplayProgressNotification: Build Failed
Error building Player: Failed to restore NuGet packages.
(Filename: Line: -1)

I am not entirely sure if this has to do with my IIS settings or it is something locally. But it is truly strange that it deletes the project.json and project.lock.json files

Environment:
Unity 5.6.1p4
Visual Basic 2015

Any input appreciated,
JJones

Other people have very recently reported similar issues:

However, we were never able to reproduce this locally. Would you be willing to give us access to (perhaps stripped down) ASP.NET program so we could figure out what’s going on?

The ASP.NET code takes an FBX and puts it in the Test/Models folder, from then on it just lets Unity take care of the rest

string filepath = Server.MapPath("Unity//commandPromptCode//Assets//Editor//Test//Models") + "//" + fuTest.FileName;
            fuTest.SaveAs(filepath);
            try {
                System.Diagnostics.Process p = new System.Diagnostics.Process();

                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.FileName = "\"C:\\program files\\Unity\\Editor\\Unity.exe\"";
                //p.StartInfo.Arguments = "-quit -batchmode  -projectPath \"C:\\Users\\UserName\\Documents\\Visual Studio 2015\\Projects\\FBXUploader\\FBXUploader\\Unity\\commandPromptCode\" -executeMethod ImportTestManager.convertFiles";
                p.StartInfo.Arguments = "-quit -batchmode -logFile \"C:\\uploadServer\\Unity\\commandPromptCode\\logFile.txt\" -projectPath \"C:\\uploadServer\\Unity\\commandPromptCode\" -executeMethod ImportTestManager.convertFiles";

                p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((s, error) => {
                    System.Diagnostics.Trace.Write(error.Data);
                    // This can be logged but building an asset bundle cannot go in here
                    // NOTE: Asset bundles can only be built on the main thread

                });

                p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler((s, error) => {
                    System.Diagnostics.Trace.Write(error.Data);
                    //lblMessage.Text = "Error";
                });

                p.Start();
                p.BeginOutputReadLine();
                p.WaitForExit();
                int ExitCode = p.ExitCode;
                System.Diagnostics.Trace.Write("Exit Code: " + ExitCode);

Thank you for the response, I have read through that thread several times to try to get things working but to no avail.

Can you fill a bug report with details so our QA could look into it and it wouldn’t get lost? Forum is not the best place for tracking issues like these.

Will do, thank you

Hello,
any luck with the solution? I’m facing the same problem right now.
BuildPipeline.BuildAssetBundles works fine when running from editor, cmd or powershell, but crashes when called by ASP.NET Web API.
Many thanks for any answer.

You’re running Unity from your ASP.NET program right? Rather than call the API directly?

I’ll try to describe how my solution works:
I have Web API that is called by Angular app from browser.
When I upload file, Web API copies the file to Resources of Unity project, and runs Unity in batch mode:

Process process = new Process();
process.StartInfo.FileName = "C:\Program Files\Unity 2017.4.4f1\Editor\Unity.exe";
process.StartInfo.Arguments = " -batchmode -quit -projectPath "D:\Workspace\AssetBundlesBuilder" -executeMethod CreateAssetBundles.BuildwsaAssetBundles -logfile D:\log\log-AssetBundlesBuild.txt"
process.Start();
process.WaitForExit();

Method BuildwsaAssetBundles adds colliders and makes some optimizations and finally there is:

PrefabUtility.CreatePrefab(relSavePath, gameObject);   AssetImporter.GetAtPath(relSavePath).SetAssetBundleNameAndVariant(bundleName, "bundletest");
BuildPipeline.BuildAssetBundles(assetBundlePath, BuildAssetBundleOptions.StrictMode, BuildTarget.WSAPlayer);

Asset budndles are not built

In IIS Application Pool i set the user to “me” (my user account) - admin account, to assure the permissions.

My findings:

  • When I run debug I can see that while executing BuildPipeline.BuildAssetBundles, files project.json and project.lock.json are deleted from UWP folder in Unity project.
  • In log file there is an error:
Restoring NuGet packages from 'D:\Workspace\ProjectPath\AssetBundlesBuilder\UWP\project.json'.
DisplayProgressbar: Building Player
Project lock file validation failed.
DisplayProgressNotification: Build Failed
Error building Player: Failed to restore NuGet packages.
  • When I run Unity from Powershell with command: & "C:\Program Files\Unity 2017.4.4f1\Editor\Unity.exe" -batchmode -quit -projectPath "D:\Workspace\ProjectPath\AssetBundlesBuilder" -executeMethod CreateAssetBundles.BuildwsaAssetBundles -logfile D:\log\log-AssetBundlesBuild.txt, asset bundles are built correctly
  • Also when I run method from Unity (I added it to menu [MenuItem("Assets/Build WSA")]) everything works fine.
  • I checked and logged users and they are “me” in all cases.

Can you record an ETW trace (Recording a trace on PC) with CPU Usage and File I/O providers enabled and PM it to me?

We managed to solve it via PM. Basically, Unity and NuGet were launched with a different set of environment variables. The solution was to force NuGet cache directories to match between Unity and NuGet by setting an environment variable before building your bundles:

var cacheDir = @"D:\NuGet";
Directory.CreateDirectory(cacheDir);
Environment.SetEnvironmentVariable("NUGET_PACKAGES", cacheDir);
1 Like