Unity – Better Texture Previews

Enhanced Texture Previewer for Unity. Zoom, pan, channels, custom 3D Materials.

Making The Editor

If you create a [CustomEditor] for a type Unity already supports you can override already existing editors. So I pulled out the Unity source and duplicated their Texture editor.

I started work on zooming capabilities. After some work messing around with GUI coordinates and figuring out that zoom-to-cursor isn’t actually what you want, but zoom-to-center is, I had a working zoom. I stripped out the code that forced textures to be point filtered, and restructured the editor’s inheritence pattern to better fit my needs. Falling back to the default editors with Texture types I had not yet implemented.

Next came RGB toggles. Easy, make a material that handles this and replace the null material provided to EditorGUI.DrawPreviewTexture. Nope.

Poor Documentation

It appears Unity magically clips the GUI in the shader it uses to draw IMGUI graphics. Ok, lets check the docs… never mind, nothing there of course. Lets just Google it… no relevant results either. Well, lets just get the shader and select it, see if there’s any properties. Okay, we can find out the shader we want is called PreviewColor2D.shader, load that, select it and…

Nope. The Debug Inspector mode doesn’t tell us anything either. Oh well. Lets see if a Frame Debugger knows what’s going on.

Ah nothing here either, the shader can’t be decompiled and none of these viewports, rects, stencil buffers seem to be what’s letting Unity clip the texture. Damn.

Save it as an asset and hope we can read it. Ah, the shader’s in editor default resources; a file we can’t save assets from. Well, we could instance the shader and save it, likely the same unreadable crap already present in unity editor resources. Lets just see if it there’s anything in there anyway.

Ah, well there it is nestled amongst the NUL|SOH|ooo?iii?KW??3Q?3Q?JU??iii?|BEL|SI|ACK|BS

Wait… what’s that?

unity_GUIClipTextureMatrix? ??

Okay. Give that a search and TA-DA! Everything is answered. Apparently we could have found this Editor shader in the builtin shaders the whole damn time! Who’d have known?! Internal-GUITextureClip.shader, the answer to our prayers.

Add these additions to your shader and everything works.

 

The Usual Conclusions

It’d be nice to not have to go through hours of trawling and work to find out how to do these things, so I’ve left a comment with the User Contributed Notes for Unity Docs extension and this post to help people in future!

Well, now that nightmare’s resolved we can add support for custom 3D materials.

Easy as that! Hah! The hardest part about writing tools for Unity in my experience is dealing with small undocumented features, bugs, and problems that may seem simple at first but are either well hidden or intertwined with core editor code. So help a tool dev out and document these problems as you run into them!

Download