「Unity/負荷軽減/GameObjectFind」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→関連) |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 行1: | 行1: | ||
| − | ==GameObject. | + | ==GameObject.FindとGetComponentの負荷検証== |
| − | 実際に測ったらGameObject. | + | 実際に測ったらGameObject.Findはやばいけど、GetComponent側は差がなかった。Updateに40個ほど用意したもので検証。 |
==GC Allocの数値比較== | ==GC Allocの数値比較== | ||
| − | *GameObject. | + | *GameObject.FindとGetComponentの両方:4.1KB |
| − | * | + | *GetComponentのみ:1.4KB |
*両方なし:1.4KB | *両方なし:1.4KB | ||
| − | GameObject. | + | GameObject.FindとGetComponentの両方 |
<pre> | <pre> | ||
| − | public class | + | public class FindComponentScene : MonoBehaviour |
{ | { | ||
int num = 0; | int num = 0; | ||
| 行35: | 行35: | ||
</pre> | </pre> | ||
| − | + | GetComponentのみ | |
<pre> | <pre> | ||
| − | public class | + | public class FindComponentGetComponentScene : MonoBehaviour |
{ | { | ||
List<GameObject> texts; | List<GameObject> texts; | ||
| 行72: | 行72: | ||
両方なし | 両方なし | ||
<pre> | <pre> | ||
| − | public class | + | public class FindComponentNotScene : MonoBehaviour |
{ | { | ||
List<Text> texts; | List<Text> texts; | ||
| 行104: | 行104: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | ==関連== | ||
| + | Unity 2019.2 から追加されたTryGetComponentを知る:https://xrdnk.hateblo.jp/entry/2020/03/23/210320#TryGetComponent%E3%81%A8%E3%81%AF | ||
| + | |||
| + | FindObjectOfTypeも重いかも | ||
2023年8月28日 (月) 12:25時点における最新版
GameObject.FindとGetComponentの負荷検証
実際に測ったらGameObject.Findはやばいけど、GetComponent側は差がなかった。Updateに40個ほど用意したもので検証。
GC Allocの数値比較
- GameObject.FindとGetComponentの両方:4.1KB
- GetComponentのみ:1.4KB
- 両方なし:1.4KB
GameObject.FindとGetComponentの両方
public class FindComponentScene : MonoBehaviour
{
int num = 0;
void Start()
{
for (int i = 1; i <= 40; i++)
{
GameObject prefab = (GameObject)Resources.Load("Text");
Vector3 position = new Vector3(0, 0, 0);
GameObject obj = Instantiate(prefab, position, Quaternion.identity, GameObject.Find("Canvas").transform);
obj.transform.localPosition = new Vector3((int)((i - 1) / 10) * 200 - 200, i % 10 * 100 - 400, 0);
obj.name = "Text" + i;
}
}
void Update()
{
for (int i = 1; i <= 40; i++)
{
GameObject.Find("Text" + i).GetComponent<Text>().text = num.ToString();
num++;
}
}
}
GetComponentのみ
public class FindComponentGetComponentScene : MonoBehaviour
{
List<GameObject> texts;
int num = 0;
void Start()
{
texts = new List<GameObject>();
for (int i = 1; i <= 40; i++)
{
GameObject prefab = (GameObject)Resources.Load("Text");
Vector3 position = new Vector3(0, 0, 0);
GameObject obj = Instantiate(prefab, position, Quaternion.identity, GameObject.Find("Canvas").transform);
obj.transform.localPosition = new Vector3((int)((i - 1) / 10) * 200 - 200, i % 10 * 100 - 400, 0);
obj.name = "Text" + i;
}
for (int i = 1; i <= 40; i++)
{
texts.Add(GameObject.Find("Text" + i));
}
}
void Update()
{
for (int i = 1; i <= 40; i++)
{
texts[i - 1].GetComponent<Text>().text = num.ToString();
num++;
}
}
}
両方なし
public class FindComponentNotScene : MonoBehaviour
{
List<Text> texts;
int num = 0;
void Start()
{
texts = new List<Text>();
for (int i = 1; i <= 40; i++)
{
GameObject prefab = (GameObject)Resources.Load("Text");
Vector3 position = new Vector3(0, 0, 0);
GameObject obj = Instantiate(prefab, position, Quaternion.identity, GameObject.Find("Canvas").transform);
obj.transform.localPosition = new Vector3((int)((i - 1) / 10) * 200 - 200, i % 10 * 100 - 400, 0);
obj.name = "Text" + i;
}
for (int i = 1; i <= 40; i++)
{
texts.Add(GameObject.Find("Text" + i).GetComponent<Text>());
}
}
void Update()
{
for (int i = 1; i <= 40; i++)
{
texts[i - 1].text = num.ToString();
num++;
}
}
}
関連
Unity 2019.2 から追加されたTryGetComponentを知る:https://xrdnk.hateblo.jp/entry/2020/03/23/210320#TryGetComponent%E3%81%A8%E3%81%AF
FindObjectOfTypeも重いかも
