Lotte 'just an idiot' May 💖🍓🌈🐹🎮☕🍫🍩 Profile picture
Hi im Lotte and i make stuff. Gay useless lesbian/Yuri subtext/GameDev/Code Maid/Absolutely tired always/VIDEOGAMES: working on HYPER GUNSPORT/ACAB

May 5, 2021, 17 tweets

Sup Friends! Lets do some Editor Scripting! theres a whole bunch of cool tech unity have added over the last year or so thats SO worth knowing about. Lets buildsomething with...

- [SerializeReference]
- TypeCache
- SerializedObjects
- ReorderableArrays

exciting! #UnityTips

So heres the dream. We want to have a list of *things* that we can assign in the editor, and then at runtime we want to iterate though that list and do some *stuff*. its a simple idea! but untill recently, its been HELLA hard to implement something like this in unity!!

So lets look at the first part of that problem. we want a list of stuff! and stuff to go in the list! and a (hacky) way to get stuff in the list!

So, what we have here is simple, we got a MonoBehvaiour that has a List of KindOfStuff objects in it.

KindOfStuff is a base class, we actually have two uhhh kinds of stuff-an IntStuff (which helpfully holds a number) and a StringStuff (you can guess what this is!) We then have a terrible editor script that draws a button we can use to test adding Stuff into our List! lets try it!

huh? Okay that clearly dose not work! All the stuff kinda ends up in the list.. but we only see the Name field, never the Text or the Number that were kind of expecting.

This is actually a REAL old limitation in the serialization system used to save our game objects. It does not support Polymorphic collections AT ALL! What this practically means for us is that our IntStuff and StringStuff objects get saved as their base type, KindOfStuff!

So we end up losing all the per type info :( THIS SUCKS! and is a real big gotcha people fall into all the time. I mean, this is such an obvious pattern! In older versions of Unity it even looked like it was working, until the scene closed and reopened and all your stuff was gone

The good news is THIS NOW WORKS! since 2019.3ish, unity has had a new attribute you can tag an array with, called [SerializeReference]. This tells Unity that the refrence type info IS important to us and it serialized the list as a collection of references will full type info.

You can find the Unity Docs for SerializeReference here docs.unity3d.com/ScriptReferenc…

Okay cool. Next up lets fix that AWFUL editor script. Heres how it sucks:
- we just directly touch the MonoBehaviour when we add stuff. This is bad, undo/redo wont work.
- actually it wont even save!!
- we kinda want a button that adds each KindOfStuff class we add to the code

ALSO we want those things to happen AUTOMAGICALLY. screwing around with an editor script each time we add something SUCKS.
First lets use Unitys SerialziedObject system to fix the Undo/Redo/Saving not working... Its takes a little bit of black magi, but its easy enough!!

Next... lets add the buttons in AUTOMAGIALLY. In the old days we would have to invoke some Reflection witchcraft... but now Unity has a helpful Editor only system called TypeCache that makes doing something like this SO EASY! docs.unity3d.com/ScriptReferenc…

and like.. LOOKIE!! IT WORKS!! It automagically adds buttons for all our stuff types! We did Reflection without touching System.Reflection! and since Unity 2020.3 (I think) Lists in the editor ARE REORDERABLE!! we can draw stuff around! look at that! AHH! 💖

And the best thing about this? Its real EASY TO DO THIS NOW!!! There is NOTHING stopping you from making custom tools on this pattern! I think its real powerful if you use this as a way to make a list of commands... this is the code for the demo at the top! Turtle programming!!

GRATS! We just made a custom visual scripting system. Just iterate over the stuff in the list at runtime and do things! This is REAL powerful for handling stuff like UI transition/control logic for example. I recently coupled this with a Tween library and made a animation system!

It really is HYPER powerful. Go make something cool <3 (This pattern also pairs REALLY well with making your own asset types with ScriptableObject. )

Pst- if you found this useful you could always GetLotte.Coffee

PRO TIP: SerializeReference isnt just for collections you want to be polymorphic. Imagine we wanted to make a node in our little Turtle programming example that could hold two other nodes and execute them both at the same time, a CombinedNode if you will.. so many possibilities!

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling