- Home /
Problem is not reproducible or outdated
inheritance vs multiple component for UI
hi, this is mostly a question about performance of the unity event system for UI
i am currently making an effect button class to add various effect like shaking, resize, AnimatedSpriteButton
my current design is abstract class ButtonEffect : MonoBehaviour , IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler , IPointerUpHandler
this main class deal with most of the basic stuff.
by exemple:
public bool IsActive
{
get{return isActive;}
set
{
isActiveChanged();
isActive = value;
}
}
//function called when isActive value is changed from the outside, must be implemented by all child class
protected abstract void isActiveChanged();
most of the function are virtual and also manage some boolean like isPressed and isInside
then i have a child class like Button resize that use all these function and data to do what it has to do I can then have multiple different child as different component of different complexity on the same uibutton
by example i have the button spin with these 2 components: ButtonResize : ButtonEffect and ButtonAnimatedSprite : ButtonEffect
each component is independent but here is my question since each component derive from ButtonEffect, each component OnPointer event function calls will be duplicated and i heard that it is much slower than regular c# event
Should i keep this design or instead change my abstract ButtonEffect for non abstract ButtonEvents and all button effect will instead of inheriting from ButtonEvents will RequireComponent ButtonEvents and subscribe to various event raised by ButtonEvents class. this way, for each button, all unity OnPointer event will only be called once, then each button effect will subscribe to the event he need
My game is on webgl so optimisation is a must
thank you for your time
An event is just a delegate, sometimes wrapped up in layers of abstraction, but still just a method call at the end. Yes, it can cost performance when doing it every frame in an Update loop or something similar but for buttons with discrete click events you probably won't notice any difference. It's more of a design decision and whether or not you want there to actually have to be two events fired for the same reason in the first place.
Answer by SunnyChow · Feb 03, 2016 at 04:46 AM
The component system is designed for designer-friendly, instead of performance. So Yes, the less components you use, the better performance you have. But i don't think there is much improvement you can do in UI. Try do the optimisation on game mechanic first.
in webgl when your main target is woman between 30 to 40, you have to make the game for low end pc. Every bit of optimisation count
Also, it is not just a question about performance but also good design
currently im going with the first option Each button effect is more independent and with abstract functions and simple override it look faster and im sure everything is implemented correctly.
We dont have any game level designer, i do all the client side code for the visual