ScriptableObject Events In Unity (C# Tutorial) | Unity Scriptable Objects

preview_player
Показать описание

In this Unity tutorial we learn how to use ScriptableObject based events in order to create a simple, easy to edit and assign via the Inspector workflow that keeps game logic decoupled and hopefully should prove scalable in the long term.

Using events in C# is a well known pattern but here we add a little bit of additional Unity specific context by turning those events into ScriptableObject assets and having them call Unity Events, which are essentially serializable function calls which we can wire up via the Inspector.

---

Learn Unity and game development with experienced indie game developers. Subscribe for tutorials, devlogs, and gamedev advice uploaded weekly.

Рекомендации по теме
Комментарии
Автор

I was one of the people mesmerized by Ryan Hipple's talk, started to study this pattern for some months and implemented it in my project (with "ScriptableObject Architecture" asset from asset store). However, I threw it out again, because it had a major flaw I want to warn others of:
The Scriptable Object architecture does not allow you to keep track of what Object or class is using which Scriptable Object (SO) anymore. So at some point, your project grows bigger and bigger. And you have dozens if not even hundreds of SO's which do all kind of things. With no overview. Race conditions. Objects being able to still trigger SOs, even though your current game state does not desire it (e.g. using input during cut scene). Refactored code leaving "dead" SOs, which you are too scared to delete ("who knows if some other script of mine still uses it?"). Huge nightmare. Great for very small projects, small apps. Don't use it for bigger, more complex games. If you are asking yourself "but then how do I manage my game state and logic without "bad" Singletons with DontDestroyOnLoad?" -> Check out additive scene loading. Unity allows to add multiple scenes on top of each other, which can interact, collide with each other, as it would be one scene. Then have a "Core" scene, which has all your managers in it. Then additively load your "Level1" scene. Your "Core" scene can keep track of score, state, player input. While you switch out levels. Also, in complex games, your "Core" scene can inject dependencies (like your input logic, your UI canvas references, your score, etc.) into the additive loaded scenes, if they need a reference (e.g. the dying enemy telling the manager to add +1 to the score). It's much more safe. Controllable. Reliable. And you don't lose your sanity while your project keeps growing bigger.

aldigangster
Автор

Thanks for the content Matt! I'm still fairly new to much of "intermediate/ advanced " issues, and it's always great to get different ways to approach or solve problems. Keep up the great work.

erz
Автор

Super glad I found this channel, I'm excited to see where it's going. Great videos so far, thanks for putting in the effort!

ghost.cassette
Автор

Holy smokes, I just found your channel and it's amazing! you got yourself a new fan

MiRoDevYT
Автор

Great timing on this, was about to get down and dirty with some Inventory and shop mechanics and this video has inpsired me to finally try out events. Thanks for the video, also top notch production value with the screen fade around the code *thumbs up*

crazyfingers
Автор

I tend to use scriptable objects only for data and not so much for logic, but now I can see it can be really useful if you want non-developer to be able to build their own stuff. Thanks for the video.

AnthelmeDumont
Автор

Ive been using a system like this for awhile now (adapted from Dapper Dinos tutorials) and find it to be super helpful, especially when using a lot of spawned Gui/objects. Events are stored on prefabs and if you use Rider, you can view all assets using your event objects. I still use a few singletons but these events make having a somewhat complex Gui much easier to manage.

quinnjackson
Автор

Great stuff man, loving the new channel!

willgoldstone
Автор

When I study events system in Unity, there are several ways to deal with, like event broker using delegates, observer pattern using interface, + this approach. I think this approach requires more memory than the other ways(since it need to create each event listener and scriptable object), this way is the far easiest to debug, and easy to understand the flow of events. I will definitely choose this approach for my toy project. Thank you for great explanation!

ErhuMaster
Автор

Nice! Thanks for taking the time to do this! :)

seithcg
Автор

Thanks a lot for giving this tutorial! It is awesome!

silong
Автор

Seems like a clean way of dealing with various UI things. I'm still trying to figure out a good workflow for integrating UI elements in my game

TheJungaBoon
Автор

You will be the next brackeys, good luck.

Specifix
Автор

I would make the gameEvent a plain C# object then creating a scriptable so that I can have it as an asset, but if I needed to make a GameEvent via code I could use them without having the overhead and complications of ScriptableObjects.

I'd also implement implicit and explicit operators so I could use said GameEvent ScriptableObjects with more ease and flexibility in the future.

MrHandsy
Автор

This is gold mine! Thanks for this man. I am now refactoring my singleton Game event system, one quick question though how can you pass parameters?

veinsreyes
Автор

Hey @Thousand Ant thanks for this tutorial. It would be great if you could enlarge the text or zoom in when looking at your code though.

orchard
Автор

Couple this with "Better Event" (available for free on GitHub but requires Odin) and it is so awesome !

LaCreArthur
Автор

I really like this concept, thx for the video. In a procedurally generated game, how would you us add such an event listener through scripting for those gameobjects that only exist in playmode after they have been generated?

edwarddewolf
Автор

First. This is the clearest explanation I have found on how to implement events, scriptable objects and listeners. Thank you very much Mat.
Second. That game has a really cool vibe. It is something that I would consider buying. How is the project going?

PESCADORSHOES
Автор

Don't want to implement this yourself?
Try or *_sigtrapgames/SmartData_* or *_BLUDRAG/SOFlow_*
Search them on GitHub.

denovodavid