Performance: Button component and click events
MEDIUM IMPACT
This affects the responsiveness and smoothness of user interactions in the game or app.
public void OnButtonClick() {
HandleClick();
}
// Assign OnButtonClick to the Button's OnClick event in the Inspectorvoid Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit)) {
if (hit.collider.gameObject.name == "Button") {
HandleClick();
}
}
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual raycast in Update | N/A (GameObjects checked each frame) | N/A | High CPU usage for raycasts | [X] Bad |
| Unity Button OnClick event | N/A (Event-driven) | N/A | Minimal CPU usage | [OK] Good |