Notifying about character/item selection
I have a UI system, which lets you set up a character.
From this UI system it is possible to open character selection screen in the same scene. Upon selecting character, it informs the UI system using the Observer pattern, so that it can display that character's information.
The issue is that I have multiple different select screens, such as character, items etc., so the UI system's inheritance list bloats like this:
MyUiSystem : MonoBehaviour, ISelectCharacterObserver, ISelectItemObserver
While it works, it creates a lot of mess. Character selection screen is used in multiple UI systems, so it cannot directly reference any of them and I would prefer not to. Is there a nicer pattern for acting when an Item is selected?
The observer pattern can be simplified in C# thanks to events
. It may simplify your implementations.
https://docs.microsoft.com/en-us/dotnet/standard/events/
You may simplify all of this even more using serialized Unity events.
https://answers.unity.com/questions/892053/button-onclick-inspector-how-do-i-do-this.html
Answer by Arteminio777 · Jan 30, 2020 at 03:16 PM
As @Hellium commented, events greatly simplified my approach.
I created events inside character selection screen and subscribed the UI system to that event. As a result whenever a character is selected, UI system is immediately notified.
Good short tutorial on events made by Unity: https://learn.unity.com/tutorial/events-uh#