facebook twitter hatena line email

「Unity/負荷軽減/GameObjectFind」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(同じ利用者による、間の2版が非表示)
行1: 行1:
==GameObject.FindとGetCompornentの負荷検証==
+
==GameObject.FindとGetComponentの負荷検証==
実際に測ったらGameObject.Findはやばいけど、GetCompornent側は差がなかった。Updateに40個ほど用意したもので検証。
+
実際に測ったらGameObject.Findはやばいけど、GetComponent側は差がなかった。Updateに40個ほど用意したもので検証。
  
 
==GC Allocの数値比較==
 
==GC Allocの数値比較==
*GameObject.FindとGetCompornentの両方:4.1KB
+
*GameObject.FindとGetComponentの両方:4.1KB
*GetCompornentのみ:1.4KB
+
*GetComponentのみ:1.4KB
 
*両方なし:1.4KB
 
*両方なし:1.4KB
  
GameObject.FindとGetCompornentの両方
+
GameObject.FindとGetComponentの両方
 
<pre>
 
<pre>
public class FindCompornentScene : MonoBehaviour
+
public class FindComponentScene : MonoBehaviour
 
{
 
{
 
     int num = 0;
 
     int num = 0;
行35: 行35:
 
</pre>
 
</pre>
  
GetCompornentのみ
+
GetComponentのみ
 
<pre>
 
<pre>
public class FindCompornentGetComponentScene : MonoBehaviour
+
public class FindComponentGetComponentScene : MonoBehaviour
 
{
 
{
 
     List<GameObject> texts;
 
     List<GameObject> texts;
行72: 行72:
 
両方なし
 
両方なし
 
<pre>
 
<pre>
public class FindCompornentNotScene : MonoBehaviour
+
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

2022年2月21日 (月) 18:06時点における版

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