我创建了一个简单的游戏,它会生成从屏幕一侧移动到另一侧的错误。它们的类型、大小、速度和顺序各不相同。如果玩家点击屏幕上的虫子,它会爆炸并消失(被摧毁)。当它到达屏幕的另一边时,它也会消失(被破坏)。我想添加统计数据以了解根据其属性窃听了哪些漏洞。我添加了一个 UnityWebRequest,但现在出现内存泄漏错误。我已经阅读了许多不同的示例和问题/答案,但无法弄清楚。没有统计数据,游戏运行良好。
我有一个脚本附加到每个名为“TapToKill”的预制错误:
void OnMouseUp() {
bug = transform.parent.gameObject.GetComponent<Bug>();
if (bug.isDead) return;
bug.isDead = true;
saveSpeed = bug.speed;
bug.speed = 0f;
GameObject bomb = Instantiate(explosion, bug.transform.position, bug.transform.rotation);
Destroy(bomb, 0.5f);
SoundManager.Instance.PlaySound(killedSound);
BugsMain.score += 10;
--BugsMain.count;
BugsMain.bugIsActive[bug.bip] = false;
// Used to destroy the gameobject here, but now hide it and post stats
bug.rend.enabled = false;
StartCoroutine(PostRequest());
}
这是我的帖子:
IEnumerator PostRequest() {
const string url = "http://catstats.local/catstats.php";
const string game = "bugs";
WWWForm form = new WWWForm();
form.AddField("game", game);
form.AddField("type", bug.bugType);
form.AddField("scale", bug.scale.ToString());
form.AddField("speed", saveSpeed.ToString());
form.AddField("spawnNumber", bug.spawnNumber.ToString());
form.AddField("dirX", bug.dirX.ToString());
form.AddField("x", bug.x.ToString());
form.AddField("y", bug.y.ToString());
UnityWebRequest uwr = UnityWebRequest.Post(url, form);
yield return uwr.SendWebRequest();
if (uwr.result == UnityWebRequest.Result.Success) {
Debug.Log("Data Sent: " + uwr.downloadHandler.text);
EndItAll();
} else {
Debug.Log("Error sending: " + uwr.error);
}
最后:
private void EndItAll() {
StopCoroutine(PostRequest());
Destroy(gameObject);
}
实际错误是:
Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator)
UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[])
UnityEngine.Networking.UnityWebRequest:SetupPost(UnityWebRequest, WWWForm)
关于如何摆脱内存泄漏的任何想法?谢谢!