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! ImageImage
Now we need to tell unity to actually LOOK for our new TexturePreviewAttribute, and what to do when it tries to draw a property that is tagged with it. Lets add a new class called 'TextureDrawDrawer' to our project. THIS NEEDS TO BE IN AN EDITOR FOLDER! Image
and basically, thats it~ lets see how it looks in editor now OH WE DID IT FRIENDS~~
OH WAIT NO ACTUALLY THIS SUCKS! I mean, we just proved that we hooked everything up right, but like.. uhh.. we got no logic there. It works exactly the same as a regular Texture2D property right now except its got the words 'No GUI Implemented' pasted on top. lets fix that up!! Image
Back in our drawer class lets implement stuff~ In OnGUI() we pull the texture out of the SerializedProperty Unity passes us, and we then just draw it into the rectangle of UI space unity gave us. Simple! Image
By default an inspector rectangle is 18 pixels high (one standard unity inspector 'line'), but we want it BIGGER. in the GetPropertyHeight() method we told unity that we want to draw in an area thats half as high as the inspector is wide. that gives us a nice amount of space
Jumping back to unity and AHH lookie! its our test texture! ~shes beautiful!~ but kinda stretched out? eww. Also theres no property field to set the texture any more! we probably don't want to do that in this use case, but it might be useful in others.. lets go fix that Image
We can use EditorGUIUtility.singleLineHeight to get the size of a inspector control. with that, and a lil maths we can increase the height we requested and make custom rectangles to draw both the texture and the property field! Lets add a scale mode onto the texture draw too ImageImage
that looks WAY WAY better. I think ScaleToFit is a sensible default, rather than stretching which is normally GROSS... but it would be neat if that was an option we could set on the attribute? heck.. drawing the property field should be optional! lets add that to the attribute! ImageImage
we can cast the this.attribute property in our drawer to the actual TexturePreviewAttribute type we know it is and we can then read out whatever info we let people shove into it! WHICH IS NEAT. now that scale mode is customizable! and we can disable that field! SIMPLE ImageImage
I hope this gives you a glimpse of how quick and easy it can be to make custom controls for Unitys inspector. Making full custom inspectors is tedious - I end up making drawers ALL the time, even for one off things! look how much stuff we did in like, 30 mins friends! PROUD!!
PRO TIPS #UnityTips
Having problems laying out controls inside your Drawer? use GUI.Color and GUI.DrawTexture to fill the entire rect before you draw the actual control. Very helpful for debugging.

Also EditorGUIUtility.singleLineHeight & standardVerticalSpacing are your friend ImageImage
MORE PRO TIPS: remember to test what type the property has been applied to. This PropertyDrawer we made together only works with a Texture2D, we should test for that and print out a error message if its on something else like an int!
We should also handle the SerializedProperty being null, we DEFFO didn't do that and its a nullref exception disaster waiting to happen! Its good to keep what could go wrong in mind and code defensively <3
If you found this helpful, don't forget you could always GetLotte.Coffee <3 ☕👩‍💻 also if you make something cool SHOW ME IT OMFG!!!
OOPS that would be TexturePreviewDrawer not TextureDrawDrawer… that was the original name before I noticed that it was TERRIBLE and renamed stuff before do thing the thread. OOPSIE

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Lotte 'just an idiot' May 💖🍓🌈🐹🎮☕🍫🍩

Lotte 'just an idiot' May 💖🍓🌈🐹🎮☕🍫🍩 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @LotteMakesStuff

23 Jul 20
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
Read 30 tweets
25 Aug 19
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.
Read 17 tweets
11 Apr 18
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.
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!