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...
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.
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!
• • •
Missing some Tweet in this thread? You can try to
force a refresh
Sup Friends! lets build an editor tool together, we haven't done that in AGES! I need to preview a Texture2D in the inspector, and theres no easy way to do it without making a custom editor. Lets fix that by making a PropertyDrawer! Its going to look like this #UnityTips
If you dont know, Property Drawers let us customize the inspector by adding a Attribute tag onto a property. As an example, lets add a Range drawer onto the Face index in my example code. Range is built into unity and is a good demo of the power we get from PropertyDrawers~
So lets make our own! The goal is to make the texture thats assigned to the SelectedFace property draw in the inspector. We need to make a new PropertyAttribute that triggers the editor to draw the texture field exactly how we want.. Lets add one called TexturePreviewAttribute!
So Unity 2020.1 release today! lets dig though it and poke around at the new features together huh?
One real big (and relatively invisible) change is Asset Import Pipeline v2 is now stable and the default! if you've not switched to it yet, I SUPER recommend it - it allows for hella fast switching between target platforms. being able to switch almost instantly is a gamechanger!
In the Project Setting -> Editor window you can totally customize how unity numbers duplicated objects, which is neat
Heya friends! 🙋♀️💖 Have you noticed this new* button thats been on your Unity toolbar and wondered what the hell it is? 🛠 WELL ITS ACTUALLY SUPER COOL! ITS CUSTOM EDITOR TOOLS TIME LETS LEARN ✨ALL✨ABOUT THEM!!
(*well, new since 2019.1)
So this new Custom Editor Tools system lets us write our own tools that are treated as first class tools, just like the inbuilt tools (Move, Rotate or whatever). They can draw UI to the scene view aswell as use the Handle API to manipulate objects…or whatever you want them to do
So, we got two types of tool - Global and Context tools.
Global tools show up on that main toolbar button and can effect any kind of object. Context tools obvs only show up for objects with a component they target, these show up in the tool panel.
Ohh the new @unity3D Mathematics library is now up on github! github.com/Unity-Technolo… It looks a lot like the math api you use for writing shaders. Why use this over the current maths types? Well this is designed for Burst! It can do SIMD optimizations that makes this HELLA fast!
Actually... lets demonstrate how much FASTER it is by making a tiny lil example with Unity C# jobs! lets do this in real time~ first, to use it you need Unity 2018.1, you can get the Matematics library from the Package Manager like this, also set the new .net runtime!
Lets code up a little example - here we have two jobs defined. They both do a simple bit of maths - its basic eluer physics intergration using the regular Unity Vector3 type, nothing fancy. one job does it single threaded and the other does it multithreaded.