LoginSignup
2

More than 3 years have passed since last update.

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

Last updated at Posted at 2019-12-02

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

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関数は削除しましょう

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

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2