LoginSignup
0
0

More than 5 years have passed since last update.

MonoBehaviour調査1

Posted at

Updateの負荷ってどのくらい?

MonoBehaviourのUpdate関数はvirtualじゃない。実装されていなければ実行されないのでUpdateなどは実装しないに超したことはない。
知識では知っているけど実際どのくらいなのか確認。

public class WithUpdate : MonoBehaviour {
    int a_;
    void Start () {
        a_ = 0;
    }
    void Update () {
        a_++;
    }
}

Update付きのスクリプトと

public class WithoutUpdate : MonoBehaviour {
    void Start () {
    }
}

Update無しのスクリプトを空のGameObject 10000個にAddComponentした場合を比較。


Update有り


Update無し

計測が適当すぎるからいい加減だけど、Update無い場合は明らかにコールされてない感じ。
(AddComponentしない状態と目視では変わらず)


次に、WithUpdate / WithoutUpdateを継承したクラスで実験。継承先にはUpdateは無い。

public class ChildOfWithUpdate : WithUpdate {
    void Start () {
    }
}

public class ChildOfWithoutUpdate : WithoutUpdate {
    void Start () {
    }
}

順当な結果。継承元にUpdateがあると呼ばれてしまうので無駄なコールが増える。継承でonUpdate()などと実装すると無駄になる。

0
0
0

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
0
0