Prefabutility



Prefabutility

I’ve been working with Prefab Variants, which let you create prefabs which “extend” other prefabs, allowing the changes on the base prefab to automatically trickle down to the variant. Extremely useful feature but Unity unfortunately makes it quite hard to tell what’s a base prefab and what’s a variant.

I decided to try to fix this by adding the Prefab Variant icon to the thumbnail of the asset so I can tell what’s what at a glance.

Before

After

Below is a tutorial for how you can do this too.

Prefabutility
  • If (PrefabUtility.GetPrefabType(go) PrefabType.Prefab) this seems to be obsolete now and I cant figure out whats the replacement for it.
  • Adds a prefab data cleaner window under Window/Prefab data cleaner. Place the scripts in a folder named Editor in your project's Assets folder.; Open the window by selecting Window/Prefab data cleaner.; What it does.
  • Description Loads a Prefab Asset at a given path into an isolated Scene and returns the root GameObject of the Prefab. You can use this to get the content of the Prefab and modify it directly instead of going through an instance of the Prefab. This is useful for batch operations.

I'm designing a racing game for my graduation project. I used PrefabUtility.SaveAsPrefabAsset to save my color change on the car prefabs, I finished the codes and it work well in Unity, but when I want to build my project,the Unity give me a error:The name 'PrefabUtility' does not exist in the current context.

PrefabutilityUnity create prefab in script

Bind to EditorApplication.projectWindowItemOnGUI

Every time an item in the project window is drawn, the projectWindowItemOnGUI callback is triggered, giving us metadata about what has been drawn. We want to bind to this callback so we can draw an icon on the assets which are prefab variants.

To do this, let’s make a new class which takes advantage of the InitializeOnLoad attribute.

The InitalizeOnLoad tells Unity to call the static constructors for this class when this script is loaded. So let’s write a static constructor which binds to projectWindowItemOnGUI and a static function which is called when the callback is triggered.

Filter out assets we don’t care about

This callback is going to be called for every single asset which is displayed in the project folder, which can be either a file or directory. In my case I only care about prefab variants, so let’s filter everything else out. First, get the filename of this asset:

We need to check to see if we’re a directory. Since we don’t want to add any icons to directories, return if we find one.

Now, let’s try loading the asset as a GameObject. All other assets besides GameObjects (sprites, animations, etc.) will fail to load.

If we successfully loaded the prefab, we can use PrefabUtility to check the type of the prefab to make sure it’s a variant.

At this point, we know we have a variant, now we can try adding the icon.

Add the Prefab Variant Icon to the asset thumbnail

Prefab Utility Building

One of the parameters into ProjectWindowItemOnGUICallback is Rect selectionRect. This represents the rectangle that the asset thumbnail is drawn in. We can use this to position the prefab variant icon ontop of the thumnail.

First, create an instance of the icon we want to set.

The 'Prefab Variant' string comes from this page which lists many of the built in editor icons Unity comes with.

The icon needs an associated Rect that defines the position and size of the icons on the screen. I’ve chosen to place the icon in the bottom right of the thumbnail. Since the icon’s origin is its upper left corner, we need to position the rect based on that.

With all this in place, we just need to make a Label with the icon and the rect.

And that’ll place the icon right on top of the asset!

Prefabutility Unity

One small detail is that when the project window is in “list” mode:

We don’t want to show our icons since there’s no room for it. We know we’re in this mode when the selection rect height is way longer than the width. So we can add this condition to the top of the function

Putting it all together

Unity Get Prefab Name

After all the steps above, our class looks like this:

Prefabutility.createprefab

And you’re done! You can use this technique with any asset to make thumnails more descriptive at a glance, like sprites or custom scriptable objects.