You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity开发技术问询:在Update中触发C#事件是否影响性能?调用Action是否为不良实践,是否应改用方法?

Hey there! Let's break down your two Unity questions one by one, since you're focused on avoiding tight coupling while keeping performance in check.


1. Does triggering C# events in Unity's Update method hurt performance?

Short answer: Almost never, unless the subscribed methods themselves are performance-heavy.

The Invoke() call on an Action/Delegate has minimal overhead—it's just a quick invocation of a list of subscribed methods. In Unity's Update loop (which runs every frame), this tiny overhead is negligible for most projects.

The real performance impact comes from what happens inside the methods you've subscribed to the event. If those methods do heavy calculations, frequent GameObject searches, or other expensive operations every frame, that's where you'll hit performance issues—not the event trigger itself.


2. Is calling an Action in Update a bad practice, and should we use direct method calls instead?

Calling an Action in Update is not a bad practice—in fact, it's a great way to achieve the loose coupling you're aiming for. Let's compare your two examples to see why:

Your Example 1 (Action approach)

public Action OnRotation; 
void Update() { OnRotation?.Invoke(); }

This is a solid approach for decoupling:

  • Your class doesn't need to know which other classes handle rotation logic—it just broadcasts that a rotation update should happen.
  • You can add/remove subscribers dynamically (e.g., enable/disable rotation handlers without modifying this class).
  • It follows the Dependency Inversion Principle, since you depend on an abstraction (the Action) rather than a concrete class.

The only thing to watch for is ensuring you don't leave orphaned subscriptions (which can cause memory leaks), but that's a general best practice for events, not specific to Update.

Your Example 2 (Direct method call with FindObjectOfType)

private SomeClass someClass => FindObjectOfType<SomeClass>(); 
private void Update() { someClass.Rotation(); }

This is a bad practice—but not because of the direct method call. The critical issue here is calling FindObjectOfType<SomeClass>() every frame in Update.

FindObjectOfType scans the entire scene hierarchy to locate an object of the specified type, which is extremely expensive if done every frame. Even if you fix that by caching the reference in Awake() or Start():

private SomeClass someClass;

void Awake()
{
    someClass = FindObjectOfType<SomeClass>();
}

void Update()
{
    someClass?.Rotation();
}

You're still tightly coupled to SomeClass—if you ever need to replace SomeClass with a different implementation of rotation logic, you'll have to modify this class directly.


Final Takeaways

  • Using Actions/Events in Update is totally acceptable and helps with loose coupling—don't avoid it.
  • The performance impact of the event itself is trivial; focus on optimizing the logic inside subscribed methods.
  • Never call FindObjectOfType in Update—cache references upfront if you must use direct method calls.
  • If loose coupling is your priority, the Action approach is far better than direct calls to concrete classes.

内容的提问来源于stack exchange,提问作者Roman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:44:41