无法通过脚本构建Unity WebGL项目

问题描述

我在WebGL目标上构建Unity项目时遇到问题。这仅在我尝试从脚本进行构建时出现。当我从Unity项目构建它时,它构建良好。 我认为这也很重要,我正在通过Powershell脚本运行此方法

PS脚本:

$unityPath = "C:\Program Files\Unity\Editor\Unity.exe"
$unityParameters = "-batchmode -projectPath `"$($repo)\`" -username USERNAME -password PASS -executeMethod `"BuildScript.PerformBuild`""
$buildProcess = start-process -FilePath $unityPath -ArgumentList $unityParameters -Wait -Passthru

我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Reporting;

public class BuildScript
{
     public static void PerformBuild()
     {
         var projectName = "scene_1";
         Debug.Log("### BUILDING ###");
         EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.WebGL,BuildTarget.WebGL);
         var report = BuildPipeline.Buildplayer(
             new[] {$"Assets/Scenes/{projectName}.unity"},$"Build/Win/{projectName}.exe",BuildTarget.WebGL,BuildOptions.None);
         
         Debug.Log("###   DONE   ###");
 
         Debug.Log(report);
         EditorApplication.Exit(1);
     }
}

我收到报告错误

切换到WebGL:WebGLSupport已禁用

...

displayProgressNotification:构建播放器时生成错误

由于不支持构建目标而导致构建播放器错误

解决方法

请检查您是否在以下位置安装了WebGL支持:C:\Program Files\Unity\2020.1.5f1\Editor\Data\PlaybackEngines\WebGLSupport

此错误Switching to WebGL:WebGLSupport is disabled表示Unity安装中可能缺少WebGLSupport。

请仔细检查您在脚本和编辑器中是否使用相同的Unity安装。您可以通过Show in Explorer选项在UnityHub中检查路径 enter image description here