You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity编辑器运行正常但打包版本报错,请求技术支持

Root Cause

The error originates from System.Configuration.ConfigurationSettings.GetConfig being called during the static initialization of System.Net.HttpWebRequest. Unity's packaged builds don’t include the System.Configuration assembly by default—while the editor has access to the full .NET Framework, this assembly is excluded in compiled builds, leading to the NullReferenceException that cascades into RestSharp’s client initialization failure.

Solutions

1. Adjust RestSharp Handler Configuration

RestSharp’s default setup relies on HttpClientHandler, which depends on the problematic HttpWebRequest initialization. Explicitly configure RestSharp to use a simplified handler that avoids this dependency:

// When initializing RestClient in ChatGPTAPI.cs and DonationManager.cs
var clientOptions = new RestClientOptions("your_api_base_url") {
    ConfigureMessageHandler = () => new HttpClientHandler()
};
var restClient = new RestClient(clientOptions);

This bypasses the default handler logic that triggers the ConfigurationSettings call.

2. Replace RestSharp with UnityWebRequest

Switch to Unity’s built-in UnityWebRequest, which is optimized for Unity’s runtime and doesn’t depend on .NET Framework-specific assemblies like System.Configuration. Example implementation:

IEnumerator SendApiRequest(string url, string jsonBody) {
    using (UnityWebRequest request = UnityWebRequest.PostWwwForm(url, jsonBody)) {
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.Success) {
            string responseData = request.downloadHandler.text;
            // Parse response with Newtonsoft.Json as before
        } else {
            Debug.LogError($"Request failed: {request.error}");
        }
    }
}

This eliminates the entire problematic dependency chain.

3. Tweak Unity Player Settings

  • Navigate to Edit > Project Settings > Player > Other Settings > Configuration
  • Set Api Compatibility Level to .NET Framework (not .NET Standard)
  • Enable Include All Assemblies (if available) to force Unity to include additional .NET assemblies in the build. Note this may increase build size, so test thoroughly.

4. Add Missing Assembly (Last Resort)

Copy System.Configuration.dll from your .NET Framework installation directory (typically C:\Windows\Microsoft.NET\Framework\v4.0.30319) into your project’s Assets/Plugins folder. This forces Unity to include the assembly in the build, but may cause conflicts on certain platforms—test all target builds after adding it.

内容的提问来源于stack exchange,提问作者Lok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 15:01:03