Thursday, September 24, 2020

Selected Objects Count in Unity

Another year, another dive into EditorWindows. A question at work about where to find a count of the currently selected objects in a scene lead me to make this one. Further inspired by the similar stats option found in 3DS Max and other modeling software I wanted this to be visible from the Scene window itself, not in a separate pop-up Editor Window.

Multiple questions I had when working on this (When do I use GUILayout? How to make a toggleable menu item? What can I do with SceneView?) were, in typical Unity fashion, poorly documented. Thankfully for Google and the braver, brighter Unity devs that already slogged through this this was a pretty quick tool to write up.

Count with me!

I'm mostly pleased with the final product - nice and simple! Select objects in the scene or hierarchy and a counter in the bottom left will be updated. The UI overlay can even be toggled ON/OFF in a menu item.

My only grief is that technically this counter doesn't count only scene objects - it counts any selected object in the Editor, including in the Project window. However, I didn't search out a way to filter these out as I could imagine someone having a use for counting objects in the Project window.

The full code is below or a Unity Package can be downloaded here. Either way, the script must be added to an Editor folder to work.

Wednesday, September 4, 2019

Nesting Animation Clips in Unity

Been doing some more work with Unity's UI lately. And in doing so learned that when auto generating animations for Buttons Unity helpfully adds those new clips as children of the Button's Animator Controller. Awesome.

Problem: Unity has no other way of nesting Animation Clips under Controllers. Such a fantastic way to keep folders full of Animation Clips organised but we have no way to do it right out of the box.


Nothing a new script with the help of some Googling can't fix. I took my first dive into EditorWindows this week to write up an implementation of nesting Animation Clips. It's simple, it's rough, to the more experienced of you out there it's probably ugly, but it works!


NestedAnimation.cs adds a set of options to the Assets/Create menu, these are available so long as an Animator Controller is selected. The Float, Int, Bool, and Trigger Animation options all quick create a new Animation Clip with a matching parameter of the selected type, a generic name (ex: "New Float"), and add a transition from "Any State" to the new clip. Custom Animation does all of the above and opens up a new window with some extra options: custom naming, speed setting, and a field for a speed multiplier parameter.


I stopped here in adding features as they currently satisfy my project's needs, however I hope to return to it in the future as well as creating a tool for deleting nested Animation Clips. In the meantime, I offer up NestedAnimation.cs for anyone to use and expand on with credit given. Find the full code below, or download a Unity Package here.

Wednesday, October 3, 2018

Unity's Additive Animations Not Working? Here's a Fix

Unity's Additive Animation system allows you to blend layers of animations together. So on one layer you could have your run/walk animations, then have an additive layer above this with different turn poses or different exhaustion states. In theory. Because right out the box the Additive Animation system keeps some information hidden from you that makes working with them difficult/right out makes them not work: you need Additive Animation Reference Poses.

What Unity expects you to do is add a frame of T-Pose at the start of every single animation you want to use as an additive animation, and... You can quickly see how this quickly gets tedious and makes testing animations more laborious.

I recently discovered a work-around for this, though, allowing you to use any other animation clip as your reference clip without having to muck around with adding T-pose frames or scripting:

  1. Duplicate the animation clip out from your imported animation (this is to get around the original being Read-Only)
  2. Select the duplicated clip and set the Inspector window to Debug mode, in the menu on the top right corner
  3. Open up the collapsed Animation Clip Settings menu, there you will see an field called Additive Reference Pose Clip. Drag and drop or select an animation clip to set as you reference pose here
  4. Set the "Additive Reference Pose Time" to the frame you want to be your reference
  5. Check off the "Has Additive Reference Pose" box
  6. You're all set, enjoy hassle free additive animations!

This can be done with multiple animations selected, making the process that much quicker.

Tuesday, March 6, 2018

Unity Shader: Adding a Gradient Overlay

Let's get right to it. Here's the shader, in all its amateurish glory:

But why use this?

A gradient is a great way of focusing attention, either towards the bottom or the top of an element. In the example below, the gradient was used to focus attention on the main street area. Additionally, it was be used to fake street-level lighting by darkening the tops of buildings and to add a feeling of cohesion between the sky colours and environment colours (like a vertical "fog").
Buildings with gradient

By changing the the value of screenPosition from Y in the highlighted section, we can change the direction of the gradient.
Oh, the places you can go

Now, this is a fairly limited shader that I created for a very specific use-case in a project. So there are some blatant improvements to be made to it:
  • Despite being named "Standard (Gradient Overlay)", it is missing many properties before actually matching Unity's Standard shader
  • The shader is in screenspace, so any camera movements on the same axis as the gradient will affect the appearance of the gradient
  • The emissive in on by default, blowing out the gradient. So if it looks like it's not working... Try changing the emissive colour to black first
  • There's no snazzy custom editor for it
Next time (eventually): improving the gradient shader.

Thursday, February 22, 2018

Hello World!

Welcome to my blog, where I hope to document my slow, painful transformation from a 3D Artist to a semi-competent Technical Artist! Thanks for stopping by, maybe someday you'll find something useful here.