Home Game Development structure – Unity: Decoupling Components

structure – Unity: Decoupling Components

0
structure – Unity: Decoupling Components

[ad_1]

One useful gizmo for decoupling elements in Unity are occasions.

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

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

Messages

One is the traditional technique SendMessage("OnDamage", damageAmount). It’s a part of the category Component, so you’ll be able to name it from any script. This technique appears in any respect elements of the identical gameObject for a way referred to as “OnDamage” and calls that technique passing arbitrary information as an argument. The benefit of this method is that it is rather simple to implement. But it has two main drawbacks:

  1. It is proscribed to elements on the identical gameObject, so you’ll be able to’t use it for communication between totally different objects. OK, there are additionally BroadcastMessage that calls the strategies on the article or any youngsters, and SendMessageUpward that calls it on its mother and father. But each require information of your object hierarchy. That’s probably not unfastened coupling. And you’ll be able to’t use it to ship occasions to things on one other department of the scene graph in any respect.
  2. The technique names are “stringly typed”. When you misspell the strategy identify (for instance “onDamage” as a substitute 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 all types of causes. Which may be actually irritating to debug.

UnityEvents

A much better occasion method, although, are Unity Events. For instance, when you will have a element which ought to be allowed to put up injury occasions, you then merely add a brand new discipline 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 notice that you’ve a brand new widget as you may understand it from buttons within the enter system. You can now add any strategies from any elements of any gameObject to it (so long as it is a technique that does not require any further arguments). Those strategies will then be referred to as if the element with the occasion calls damageEvent.Invoke(damageAmount).

You also can arrange such occasion subscriptions at runtime. For instance, when you will have a special element on the identical gameObject which ought to all the time react to the damageEvent of the Damageable element, you’ll do that:

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

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

If you want a full instance for the way to use Unity Events, I bought one on this reply.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here