I am wondering how much differences between these two (especially performance):
// (1) animation is smooth!
class JSComponent : MonoBehaviour
{
void Update() {
// check user input 'jump' button
}
void FixedUpdate() {
// add force to rigidbody2D
}
}
// (2) animation is not smooth!
class JSComponent : MonoBehaviour
{
}
class JSComponent_Update_FixedUpdate : JSComponent
{
void Update() {
// check user input 'jump' button
}
void FixedUpdate() {
// add force to rigidbody2D
}
}
I have to put Update and FixedUpdate (and other predefined functions) in subclass for some reasons.
But I noticed that I seems this way costs performance. The frame rate does not change, but my animation with ‘JSComponent_Update_FixedUpdate’ is not smooth at all.
I can’t think of any reason why an animation not playing smoothly would be caused by this, from the details given. Are you sure the problem is actually with this? Even if some overhead existed (and in reality the only overhead is exactly the overhead you’d get from inheriting directly from Monobehaviour, in this example) it would be so tiny as to make no visible difference. If there IS a visible difference, then it’s either completely unrelated or a part of some chain-reaction effect that’s not directly caused by the inheritance chain but rather from some dependency on the old class that’s not being shifted to the new class.
Instead of the experiment you’ve made here, do this: put all of your functions and members back on the original class. Run it, make sure it works fine (as it originally did), then make a completely different (empty) class that inherits from MonoBehaviour and change the original class so that it inherits from the new one. That way, dependencies are still where they’re supposed to be, but you’ll be in the exact same type of chain.
In other words:
class JSComponent : JSComponent_Update_FixedUpdate
{
void Update() {
// check user input 'jump' button
}
void FixedUpdate() {
// add force to rigidbody2D
}
}
class JSComponent_Update_FixedUpdate : MonoBehaviour
{
}
There should be no difference, Unity uses reflection under the hood to find the Update/FixedUpdate/Awake etc functions, caches the result and then calls them. Does not matter how many levels of subclasses there are.
Thank you for you replies!
I think I already know why…
The character has an Animator component, and my JSComponent originally has ‘OnAnimatorMove’.
Animator in Inspector looks like this:
But when I use JSComponent_Update_FixedUpdate, I don’t have ‘OnAnimatorMove’ in JSComopnent.
It has nothing to do with MonoBehaviour inheritance.
If you are interested what the difference is:
Download 2DPlatformer from asset store, add ‘OnAnimatorMove’ to PlayerControl.cs to see the difference!! (Press UP on keyboard to let character jump)
It’s so sad that this spent me 2 days to figure out why.