日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UNITY引擎变量调用产生不必要内存分配

發布時間:2025/3/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UNITY引擎变量调用产生不必要内存分配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

UNITY引擎變量調用產生不必要內存分配

https://unity3d.com/de/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games?playlist=44069

Unity function calls

It’s important to be aware that whenever we call code that we didn’t write ourselves, whether that’s in Unity itself or in a plugin, we could be generating garbage. Some Unity function calls create heap allocations, and so should be used with care to avoid generating unnecessary garbage.

There is no list of functions that we should avoid. Every function can be useful in some situations and less useful in others. As ever, it’s best to profile our game carefully, identify where garbage is being created and think carefully about how to handle it. In some cases, it may be wise to cache the results of the function; in other cases, it may be wise to call the function less frequently; in other cases, it may be best to refactor our code to use a different function. Having said that, let’s look at a couple of common examples of Unity functions that cause heap allocations and consider how best to handle them.

Every time we access a Unity function that returns an array, a new array is created and passed to us as the return value. This behaviour isn’t always obvious or expected, especially when the function is an?accessor?(for example,?Mesh.normals).

In the following code, a new array is created for each iteration of the loop.

void ExampleFunction() {for (int i = 0; i < myMesh.normals.Length; i++){Vector3 normal = myMesh.normals[i];} }

It’s easy to reduce allocations in cases like this: we can simply cache a reference to the array. When we do this, only one array is created and the amount of garbage created is reduced accordingly.

The following code demonstrates this. In this case, we call?Mesh.normals?before the loop runs and cache the reference so that only one array is created.

void ExampleFunction() {Vector3[] meshNormals = myMesh.normals;for (int i = 0; i < meshNormals.Length; i++){Vector3 normal = meshNormals[i];} }

Another unexpected cause of heap allocations can be found in the functions?GameObject.name?orGameObject.tag. Both of these are accessors that return new strings, which means that calling these functions will generate garbage. Caching the value may be useful, but in this case there is a related Unity function that we can use instead. To check a GameObject’s tag against a value without generating garbage, we can use?GameObject.CompareTag().

In the following example code, garbage is created by the call to?GameObject.tag:

private string playerTag = "Player";void OnTriggerEnter(Collider other) {bool isPlayer = other.gameObject.tag == playerTag; }

If we use?GameObject.CompareTag(), this function no longer generates any garbage:

private string playerTag = "Player";void OnTriggerEnter(Collider other) {bool isPlayer = other.gameObject.CompareTag(playerTag); }

GameObject.CompareTag?isn’t unique; many Unity function calls have alternative versions that cause no heap allocations. For example, we could use?Input.GetTouch()?and?Input.touchCount?in place of?Input.touches, or?Physics.SphereCastNonAlloc()?in place of?Physics.SphereCastAll().

?

posted on 2017-08-23 17:57 時空觀察者9號 閱讀(...) 評論(...) 編輯 收藏

總結

以上是生活随笔為你收集整理的UNITY引擎变量调用产生不必要内存分配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。