Resetting Scriptable Objects - Unity Playmode
Вставка
- Опубліковано 11 лип 2024
- 🕹️Steam Wishlist: store.steampowered.com/app/25...
✅🎮👉 Current Build on Itch: onewheelstudio.itch.io/hex-game
Scriptable objects are a useful tool in the Unity game engine to hold static data. However, if you need to change data or values in a scriptable object at runtime things get a bit tricky. With lifecycle functions like OnEnable and OnDisable or using the NonSerialized attribute we can solve those problems.
Support the Channel!
**********************
► Buy me a coffee: www.buymeacoffee.com/onewheel...
► Shop the Unity Asset Store (affiliate): assetstore.unity.com/top-asse...
► Become A Channel Member: / @onewheelstudio
► Or a Patron: / onewheelstudio
► My video and streaming kit: kit.co/onewheelstudio/tutoria...
► Buy a Game Design Book: kit.co/onewheelstudio/game-de...
Video Gear List (Amazon Affiliate)
***********************************
► Sony A6100 Camera: amzn.to/32logXz
► Sigma 16mm 1.4 Camera Lens: amzn.to/3KA4Wr4
► GoXLR (audio interface): amzn.to/35dmr03
► Shure SM7B Mic: amzn.to/3fNbV1E
► Blue Mic Arm: amzn.to/3tHgwdQ
Neewer Light: amzn.to/3rD8eRv
Light Softbox: amzn.to/3GUaHgQ
Neewer RGB Light: amzn.to/3rCTQJ9
Other Links
************
Discord: discord.onewheelstudio.com
Twitter: / onewheelstudio
Devlog: onewheelstudio.com/
Twitch: / onewheelstudio
Timestamps
*************
0:00 SOs are awesome, but...
0:34 Deep Space Directive
1:00 Change SOs at Runtime
2:39 Solution #1 - OnEnable & OnDisable
4:32 Solution #2 - NonSerialized Attribute (!!!)
5:36 THE END...
#GameDevelopment #Unity3D #indiedeveloper
If only I'd got the camera to focus on me and not the microphone.... 😑Guess I'm a bit rusty?
🕹Steam Wishlist: store.steampowered.com/app/2510180/Deep_Space_Directive/
✅🎮👉 Current Build on Itch: onewheelstudio.itch.io/hex-game
SO's behave differently when a game is built. When a SO is unloaded it resets in build. This was a "gotcha" I once ran into.
right when i needed this
thank you so much!
Wow, very interesting! I had no idea SOs serialized private fields. And I didn’t know how their life cycle worked either.
I think I’ll still prefer to not modify SOs at runtime… but it’s good to understand the tools more deeply!
I like your teaching approach, thank you for sharing this useful experience.
Thanks for the info!
Lmao that final solution would so easily be overlooked, thank you for that I appreciate you!
on 2023.2.11 I only observe OnEnable being called on domain reload - not when entering play mode.
The most obviouse approach to "reset" your SOs data, is not using the asset at all, but instantiating it before. A clone is created and the changed values aren't stored into the asset.
Most definitely. I thought about that approach. For my use case I would need some sort of factory to hand out references to the SOs - to ensure everyone has the same copy. Which is doable but felt more complex than I needed.
cool ...my method is to create a differente class for all the entities or even a whole class for taking care of this, but with "non-serializable" I am going to try.
Thanks for that. But you said the problem started because you needed to update/set different values to the SO, and once you set the variables to private how did it solve your problem? becuase now you can't change these values from anywhere in your game
In a lot of cases I'm using functions to change the values of local/private variables after doing some calculations. You can also use getters/setters that are public to change the values of private variables. Lots of ways to do it ;)
But in all cases if you don't use the NonSerialize attribute changes made in play can persist out of playmode.