A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to ge

A  Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

Package manager中添加 com.unity.entities

添加完成后上方的Menu中会有Jobs按钮, Jobs -> Leak Detection -> Full Stack Traces (Expensive)就可以打开了.

原文链接:No information on error with full stack traces enabled - Unity Answers

至于为啥出现这个问题是在于频繁使用post请求导致内存泄漏.

这是因为在使用完UnityWebRequest对象后未能调用Dispose方法释放

解决方法

在UnityWebRequest外添加一层using(){},这样在离开代码块时会自动调用dispose()方法

public static IEnumerator UnityWebRequestPost(string url, string json, Action<string> acticn)
    {
        using (UnityWebRequest www = UnityWebRequest.Post(url, json))
        {

            www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
            byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
            www.uploadHandler = new UploadHandlerRaw(bodyRaw);
            yield return www.SendWebRequest();
            if (www.result == UnityWebRequest.Result.ProtocolError || www.result == UnityWebRequest.Result.ConnectionError)
            {
                Debug.LogError($"发起网络请求失败: 确认过闸接口 -{www.error}");
            }
            else
            {
                Debug.Log(www.downloadHandler.text);
                acticn?.Invoke(www.downloadHandler.text);
            }
        }
    }