Search Unity

AssetBundle Failing to Build on ASP.NET

Discussion in 'Windows' started by JJ_the_Jet, Jun 22, 2017.

  1. JJ_the_Jet

    JJ_the_Jet

    Joined:
    Mar 24, 2017
    Posts:
    4
    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:
    Code (CSharp):
    1. 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
     
    Last edited: Jun 22, 2017
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  3. JJ_the_Jet

    JJ_the_Jet

    Joined:
    Mar 24, 2017
    Posts:
    4
    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

    Code (CSharp):
    1. string filepath = Server.MapPath("Unity//commandPromptCode//Assets//Editor//Test//Models") + "//" + fuTest.FileName;
    2.             fuTest.SaveAs(filepath);
    3.             try {
    4.                 System.Diagnostics.Process p = new System.Diagnostics.Process();
    5.  
    6.                 p.StartInfo.RedirectStandardError = true;
    7.                 p.StartInfo.RedirectStandardOutput = true;
    8.                 p.StartInfo.UseShellExecute = false;
    9.                 p.StartInfo.CreateNoWindow = true;
    10.                 p.StartInfo.FileName = "\"C:\\program files\\Unity\\Editor\\Unity.exe\"";
    11.                 //p.StartInfo.Arguments = "-quit -batchmode  -projectPath \"C:\\Users\\UserName\\Documents\\Visual Studio 2015\\Projects\\FBXUploader\\FBXUploader\\Unity\\commandPromptCode\" -executeMethod ImportTestManager.convertFiles";
    12.                 p.StartInfo.Arguments = "-quit -batchmode -logFile \"C:\\uploadServer\\Unity\\commandPromptCode\\logFile.txt\" -projectPath \"C:\\uploadServer\\Unity\\commandPromptCode\" -executeMethod ImportTestManager.convertFiles";
    13.  
    14.                 p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((s, error) => {
    15.                     System.Diagnostics.Trace.Write(error.Data);
    16.                     // This can be logged but building an asset bundle cannot go in here
    17.                     // NOTE: Asset bundles can only be built on the main thread
    18.  
    19.                 });
    20.  
    21.                 p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler((s, error) => {
    22.                     System.Diagnostics.Trace.Write(error.Data);
    23.                     //lblMessage.Text = "Error";
    24.                 });
    25.  
    26.                 p.Start();
    27.                 p.BeginOutputReadLine();
    28.                 p.WaitForExit();
    29.                 int ExitCode = p.ExitCode;
    30.                 System.Diagnostics.Trace.Write("Exit Code: " + ExitCode);
     
  4. JJ_the_Jet

    JJ_the_Jet

    Joined:
    Mar 24, 2017
    Posts:
    4
    Thank you for the response, I have read through that thread several times to try to get things working but to no avail.
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    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.
     
  6. JJ_the_Jet

    JJ_the_Jet

    Joined:
    Mar 24, 2017
    Posts:
    4
    Will do, thank you
     
  7. michalgajdo

    michalgajdo

    Joined:
    Apr 4, 2018
    Posts:
    2
    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.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    You're running Unity from your ASP.NET program right? Rather than call the API directly?
     
  9. michalgajdo

    michalgajdo

    Joined:
    Apr 4, 2018
    Posts:
    2
    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:
    Code (CSharp):
    1. Process process = new Process();
    2. process.StartInfo.FileName = "C:\Program Files\Unity 2017.4.4f1\Editor\Unity.exe";
    3. process.StartInfo.Arguments = " -batchmode -quit -projectPath "D:\Workspace\AssetBundlesBuilder" -executeMethod CreateAssetBundles.BuildwsaAssetBundles -logfile D:\log\log-AssetBundlesBuild.txt"
    4. process.Start();
    5. process.WaitForExit();
    Method
    BuildwsaAssetBundles
    adds colliders and makes some optimizations and finally there is:
    Code (CSharp):
    1. PrefabUtility.CreatePrefab(relSavePath, gameObject);   AssetImporter.GetAtPath(relSavePath).SetAssetBundleNameAndVariant(bundleName, "bundletest");
    2. 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:
      Code (CSharp):
      1. Restoring NuGet packages from 'D:\Workspace\ProjectPath\AssetBundlesBuilder\UWP\project.json'.
      2. DisplayProgressbar: Building Player
      3. Project lock file validation failed.
      4. DisplayProgressNotification: Build Failed
      5. 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.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    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:

    Code (csharp):
    1. var cacheDir = @"D:\NuGet";
    2. Directory.CreateDirectory(cacheDir);
    3. Environment.SetEnvironmentVariable("NUGET_PACKAGES", cacheDir);
     
    michalgajdo likes this.