Using Scriptable Objects for Events in Unity | Scene Independent Event System
Вставка
- Опубліковано 1 сер 2024
- 🚨 Wishlist Revolocity on Steam! store.steampowered.com/app/27... 🚨 Using Scriptable Objects we can build better, more scalable event systems in Unity and reduce the amount of dependency we have on Scriptable Monobehaviours that have to sit in the scene and move between scenes.
📁 Get access to my tutorial project files over on Patreon: / danpos
💬 Join in with the Blender and Game Dev conversation over on The Creative Magpie Discord Server: / discord
-- VIDEO RESOURCES --
Unity Scriptable Objects Video: • Better Data with Scrip...
Singleton Pros and Cons: www.freecodecamp.org/news/sin...
Singleton Cons: erdiizgi.com/avoiding-singlet...
Unity ChopChop: github.com/UnityTechnologies/...
❤️ Find me elsewhere on the internet:
Gumroad: gumroad.com/danpos_
Twitter: / danpos_
Instagram: / danpos_
📜 If you like this video and want to develop your skills I can highly recommend the following courses I have taken*:
Blender 2.8 Launch Pad Course: academy.cgboost.com/p/blender...
Substance Painter Launch Pad: academy.cgboost.com/p/substan...
Unreal Engine Blueprint Game Developer:
click.linksynergy.com/deeplin...
Unreal Engine C++ Developer:
click.linksynergy.com/deeplin...
Blender 3D Sculpting for Beginners: click.linksynergy.com/deeplin...
Beginning C++ Programming:
click.linksynergy.com/deeplin...
The Ultimate Guide to Game Development With Unity:
click.linksynergy.com/deeplin...
🖱️ This is the gear that I use to create*
Lenovo L24Q - QHD 24" Monitors: amzn.to/3s6Rba0
BONTEC Dual Monitor Arm: amzn.to/3s7tgr1
Logitech G213 Keyboard: amzn.to/38pe3tq
Logitech Z333 2.1 Speaker System: amzn.to/2LBdNic
Logitech 720p Webcam: amzn.to/39eyecG
Blue Yeti Microphone: amzn.to/2Le7EbB
Corsair VOID ELITE RGB Headphones: amzn.to/35i50bB
Corsair Harpoon RGB Optical Gaming Mouse: amzn.to/3olwCUV
Corsair iCUE LS100 Smart Lightin Strip Starter Kit: amzn.to/3q5mMXP
Corsair iCUE 465X RGB Case: amzn.to/3noyNpp
Corsair Hydro 100i RGB Cooling System: amzn.to/3q0K8xH
RTX 2060: amzn.to/35kewLF
Large Mouse Mat: amzn.to/2MEQdBn
Gel Wrist Rest: amzn.to/3bj3Pg0
Video Music Provided by EpidemicSound: www.epidemicsound.com/referra...
*These are affiliate links which means I receive a small commission if you decide to buy something - the price will be no different to you though! I would massively appreciate any support :) *
Chapters:
00:00 - Coming Up
00:15 - Singletons and why we won't use them
01:15 - What are Scriptable Objects
02:14 - Demonstration
03:35 - Showing the code
07:30 - Creating a new channel from scratch
14:05 - Showing the broadcaster in a seperate scene
15:10 - Video outro - Ігри
⏰ UNITY BLACK FRIDAY SALE ENDS DECEMBER 4th!
⚡ LIGHTNING DEALS SEE 90% OFF SELECT PRODUCTS
Take 50% off the best selling assets on the Unity asset store in their Black Friday sale, now on!
prf.hn/click/camref:1101lkANY/creativeref:1011l65259
One small suggestions: Rather than making new classes for each data type, you could create an abstract class that takes in a generic type. This way you can design the general Channel api once without every having to rewrite the functionality. Now, when you want to make, say, a RIgidBody Event Channel, you could simply create a class that derives from the abstract class with RigidBody passed as the generic. Now if you want to change the functionality of your channels, you can do so in the abstract class and any derived classes will inherit that changed behavior. Great video!
That's a good idea! I'll try and refactor it to take this into account :)
@@DanPos Thanks! I hope that ends up being helpful!
@@VoonNBuddies Can you give more detailed information about implementing generic classes in actions for not creating extra classes for extra data typeS? :)
I got it. :)
Can you please show us roughly how you'd do that? Wouldn't that be something like OnEventRaised?.Invoke(); ?
Awesome video, thanks. I retooled it for my own purposes slightly -- I made a single EventChannel script and created an Arguments class that I can basically add any data I want to. Then every event just uses this one event type, and every channel can support any amount of data. It's probably not the most efficient, but it works for a smaller game where performance isn't super important. The core concept is great and helped me start a pretty huge refactor. Much appreciated.
Great tutorial, completely understood the concept after watching!
please fix your mic input. The repeating high frequency audio really hurts while listening.
This is a really great tutorial, thanks for the great example and explanation 👏👏
Thank you! Glad you enjoyed it 😊
Excellent tutorial and demonstration of ScriptableObjects and Unity events!
Thank you! Glad you liked it :)
Great video! thank you so much.
It has helped me a lot!
great one
Thanks for a great video!!
Glad you liked it!
That‘s a great tutorial. But I‘m left with a question. In my case I have multiple scriptable objects from the same SO-Script (different Items in my case). How could I check which Item invoke the event? Passing the name as a string with the event doesn‘t feel right.
Is it just me, or is there a high pitched beep in the background?
Hi Dan, do you know how to use coroutine within the listener function? thanks
Really cool video. Does a system like this work for more Complicated systems such as camera shaking or ui management?
Yes, absolutely! Here's a great video that uses similar concepts to create an Inventory System: ua-cam.com/video/TB5SCQ14jLw/v-deo.html
Exactly what I needed to know, all the respect man to you
My middleman singleton for events is gone thanks to you! I just wonder why you use UnityAction instead of Action? Both work the same but Action can take more args if needed.
Does it work in a built game too?
I really liked the idea! But what happens if we have for example Trader NPC's and all would have their own "OnTradeCompleted" scriptable object? I found myself overkilling, assigning it different SO events for every trader. If i use same SO, when one completed, others are also affected.
You could have an OnTradeCompleted event that takes one argument, the Trader NPC class. When each receives the event, it checks if the Trader NPC passed is itself and if so, performs the action
I usually just use a static Events class with a bunch of C# action delegates, then there's no need to wire anything up in the inspector -- plus I can use Visual Studio to find all references to see what's subscribed and what's invoking. I don't like the scriptable object approach that's been evangelized by the likes of Ryan Hipple (I tried it in my own project and it felt like a scalability nightmare).
I agree. Ryna Hipples systems was a boilerplate nightmare to set up and maintain.
Definitely another great solution, my main goal with this video was to provide an alternative solution to the Singleton way of doing things but you're right a static Events class which also people a good option and achieve similar results to this, without the need for hooking things up in the inspector.
That's the great thing about coding - lots of different ways of doing stuff and its whatever works for you and your project. For example, this implementation could work well if you're working with designers who need to hook things up in the inspector and not look at code.
Exactly my thoughts. The hard part is not isolating objects from each other, it's to connect them dynamically, without using the editor to hook things up. In a real case scenario you often need to establish listeners and raisers dynamically, and this becomes an issue even when they are not referring to each other directly. A static Event manager system becomes much easier to manage, you just create a SINGLE reference relationship on ONE end of the raiser-listener system (usuall on the listener side). But what I wonder is, which one is better amongst the following two: (1) all your listeners susbcribe to the EventManager system, who in turn subscribe to raiser objects? Or (2) Raisers just use the static Event Manager reference directly and call a function there, which in turn fires an event to which the listeres are subscribed to?