Home Game Development unity – Decoupling Components – Game Development Stack Exchange

unity – Decoupling Components – Game Development Stack Exchange

0
unity – Decoupling Components – Game Development Stack Exchange

[ad_1]

One useful gizmo for decoupling parts in Unity are occasions.

The high-level thought of occasions is that one part says “Something occurred with me” and different parts can select to subscribe to such occasions after which react on them. The part which posted the occasion doesn’t have to know who listens to the occasion it invokes (or if anybody listens in any respect). And the parts reacting to them don’t have to know which part invoked the occasion. That means you may have parts which talk with one another with out even figuring out about their existence.

Unity truly has a number of occasion architectures. I’ll current two right here, the basic message broadcast occasions and the a lot newer UnityEvents.

Messages

One is the basic methodology SendMessage("OnDamage", damageAmount). It’s a part of the category Component, so you possibly can name it from any script. This methodology appears to be like in any respect parts of the identical gameObject for any strategies referred to as “OnDamage” and calls these strategies passing arbitrary knowledge as an argument. The benefit of this system is that it is extremely straightforward to implement. But it has two main drawbacks:

  1. It is proscribed to parts on the identical gameObject, so you possibly can’t use it for communication between completely different objects. OK, there are additionally BroadcastMessage that calls the strategies on the thing or any youngsters, and SendMessageUpward that calls it on its mother and father. But each require data of your object hierarchy. That’s not likely free coupling. And you possibly can’t use it to ship occasions to things on one other department of the scene graph in any respect.
  2. The methodology names are “stringly typed”. When you misspell the strategy identify (for instance “onDamage” as an alternative of “OnDamage”), then nothing goes to inform you. The code simply runs and nothing occurs. Which is a symptom that may be brought on by every kind of causes. Which may be actually irritating to debug.

UnityEvents

A much better occasion approach, although, are Unity Events. For instance, when you may have a part which needs to be allowed to submit harm occasions, then you definately merely add a brand new area to the category:

public UnityOccasion<int> damageEvent;

When you now have a look at the inspector of a gameObject with that compoennt, then you will note that you’ve a brand new widget as you would possibly understand it from buttons within the enter system. You can now add any strategies from any parts of any gameObject to it (so long as it is a methodology that does not require any extra arguments). Those strategies will then be referred to as if the part with the occasion calls damageEvent.Invoke(damageAmount).

You may arrange such occasion subscriptions at runtime. For instance, when you may have a distinct part on the identical gameObject which ought to at all times react to the damageEvent of the Damageable part, you’d do that:

void Start() {
    GetComponent<Damageable>().damageEvent.AddListener(OnDamage)
} 

non-public void OnDamage(int quantity) {
    Console.Log($"Ouch, I obtained {quantity} harm!");
}

If you want a full instance for find out how to use Unity Events, I received one on this reply.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here