ScriptableObject Tutorial: Getting Started | raywenderlich.com

In this tutorial you’ll learn how to create and use ScriptableObject in Unity. ScriptableObjects in Unity can increase your workflow, reduce memory usage, and even decouple your code architecture.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2826197-scriptableobject-tutorial-getting-started

Great Article!

I really like this approach because it has no coupling. But everywhere people recommend not to use UnityEvents. Instead, you should use C# Events/Delegates because it has a better performance. Why did you choose UnityEvents or is there a good way to combine this with the C# native Event/Delegate System?

Glad you enjoyed the tutorial!

I opted to used UnityEvents because this is a Unity tutorial and we’d like to work with Unity not fight against it. That said UnityEvents are definitely not perfect, and have flaws. You can just as easily use C# native events and delegates within a ScriptableObject to get the same decoupled result.

Thank you so much for this tutorial ! i’m so lucky i found this website. but it would be awesome if you make some sections where you explain things( what are listeners, game events and where we use them) then you provide a link to those sections or something, because now for example, you just say " 3- A list of GameEventListeners that will subscribe to your GameEvent " and i have no idea what these two are. but thank you again it helps a lot.

@jayfeesh thank you for such a great platform.

one real quick question, don’t mind if it’s not much logical ,i 'm a newbie.
My question is, when we have sword.cs class which has ability to show data in UI(if we attach the sword.cs script on each sword and pass the reference of each sword’s Scriptable object to this class), then OnMouseDown() function will get the values from each Scriptable object and show in the UI.
isn’t is pretty simple then creating events and event listeners and additional merchant script.?

or is there advantage of using events and merchant script.?
why we did events and event handling stuff.?

@dover Can you please help with this when you get a chance? Thank you - much appreciated! :]

The events and event listeners were used to demonstrate the flexibility of Scriptable Objects.

@umerfarooq904 as @jayfeesh has said, it was to display the power of the events system in combination with Scriptable Objects.

Consider an example where a clicking on a object, using a key combination, and also selecting a menu item with a gamepad, all result in you selecting one of the swords. Each one of these simply needs to raise the same event in order to update the UI, rather than having to code a solution for each scenario.

Got to end of tut but can’t finish because I cannot assign the function on the listener. Am also getting some warnings…not sure what they are related to.

image

This particular tutorial seems to require setting things on every gameobject plus adding individual listeners to the merchant. If I have a lot of gameobjects or am creating gameobjects on the fly is there a way to automate attaching all the different parts?

The warnings are related to having private serialized fields. If you’d like them gone you can add = default; next to each Serialized Field. This is sort of a bug with the new Roslyn Compiler, which has a fix in place. So expect those warnings to disappear in future versions of Unity.

@kalmdown3d @dover Consider a scenario where you have an Audio Engine that wants to play a different Audio Clip for each sword, when you select it. This is where having a unique event for each sword is useful, the Audio Engine can listen to these different events and play the correct sword selected sound. Keeping it an entirely decoupled system.

In regards to automatically assigning the different parts. Yes, there are definitely ways, but they were outside the scope of this tutorial. Take a look at how you can extend the editor in this tutorial: https://www.raywenderlich.com/939-extend-the-unity3d-editor

Some other references that might help get you started for this particular automation job:

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!