This has been an ongoing problem for over 5 years. Modifying the GUI.matrix to rotate an element inside a BeginArea doesn’t work correctly. The elemnent is clipped to its containing area BEFORE the rotation is applied, therefore you end up with all kind of wacky stuff when you try to move an object inside the area (pics below).
In-game GUI probably has lots of possible solutions, but I’m dealing with editor GUI here. I can’t think of any other way to deal with rotating an element apart from making hundreds of textures and swapping. Another possible solution would be to replace BeginArea, but I don’t think enough of the drawing core is exposed to do this (could be wrong.) Anyone have any ideas on where to start with a workaround?
Edit: Just had an idea… I could draw the rotated box to a texture every frame. I’d have to come up with some image rotation formula though.
Well, it works, but it’s really really slow when drawing a texture of any significant size. >:(
Again, unusable.
For now, I settled on a compromise. I’m creating a new Texture2D every frame the box is modified (rotated, shape changed, etc.), but the box is not full resolution. I create a smaller box at (width / height * 100px) by 100px to keep the same aspect ratio, rotate it in a new texture, calculate the final full-size rect, then I use GUI.DrawTexture(rect, ScaleMode.StretchToFill) to draw the box larger. I end up with a blocky-edged box if its > 100px, but it draws WAYYYY faster than drawing every pixel. Obviously, this wouldn’t work for images besides simple color fills. In fact, I can’t even draw my border around the box anymore because of this. But it’s the only possible solution I can think of to this likely-eternally present trimming problem in Unity GUI.
Tips:
- Create your temportary textures with HideFlags.DontSave to prevent texture leaking errors.
- Don’t forget to DestroyImmediate your old textures every time you make a new one or if you drop the containing object from use or you’ll get memory leaks.
