Quantcast
Viewing all articles
Browse latest Browse all 8905

【初心者向け】処理負荷の軽減に繋がるコーディング【Unity】

簡単だけど意識しておくだけで負荷軽減に繋がるコードの書き方を紹介します。

1.毎回取得する必要のないものはキャッシュする

    void Start()
    {
    }

    void Update()
    {
      // 毎回呼ばれる
        Text text = GetComponent<Text>();

        // テキストの文字を変更
        text.text = "文字";
    }

    // キャッシュ用メンバ変数
    Text m_text = null;

    void Start()
    {
        // 最初の一回だけGetComponentを呼び出しキャッシュする
        m_text = GetComponent<Text>();
    }

    void Update()
    {
        // テキストの文字を変更
        m_text.text = "文字";
    }

2.毎回処理する必要のないものはUpdateに書かない

    Text m_text = null;      
    void Start()
    {
        m_text = GetComponent<Text>();
    }

    void Update()
    {
        // テキストの文字を変更
        m_text.text = "文字";
    }

    Text m_text = null;      
    void Start()
    {
        m_text = GetComponent<Text>();
        // テキストの文字を変更
        m_text.text = "文字";
    }

   // 不要なUpdate関数は削除しましょう

   

頭の隅に入れて書くだけでも後々負荷軽減に繋がりチームメンバーからいいねを貰えると思います。

偉大な公式の最適化マニュアル


Viewing all articles
Browse latest Browse all 8905

Trending Articles