Skip to content

Instantly share code, notes, and snippets.

@krypt-lynx
Last active November 20, 2022 03:45
Show Gist options
  • Save krypt-lynx/2bfbd846d6e6ac8d777fcf44d16d2230 to your computer and use it in GitHub Desktop.
Save krypt-lynx/2bfbd846d6e6ac8d777fcf44d16d2230 to your computer and use it in GitHub Desktop.
Unity imgui vertical label
public void VerticalLabel(Rect rect, Vector2 clipSize, string text)
{
// the issue:
// while we transforming gui, whe clip area rotates with it
// so, if we want to draw somewhere on untrasformed screen, we have invalid clip area to deal with.
// the best idea I have is to draw on (0,0) of current clip area and move label to desized position using gui matrix intead of label coordinates
// limitations:
// we need to apply clip effect to transformed label manually, we need to know clip area size for that, but I see no way to obrain it
// original clip is still applied, and width and height is now inverted. So, original clip have no space to render the label *horizontaly*, the vertical label will not be rendered correctly either.
Matrix4x4 matrix = GUI.matrix;
GUI.matrix = Matrix4x4.identity;
Vector2 unclipped = UnclipVector2(Vector2.zero);
GUI.matrix =
matrix *
// move origin to (0,0) of current clip; rotate around (0,0)
Matrix4x4.TRS(unclipped, Quaternion.Euler(0f, 0f, -90), Vector3.one) *
// move (0,0) to label possition
Matrix4x4.TRS(new Vector2(-rect.yMax - unclipped.x, rect.xMin - unclipped.y), Quaternion.identity, Vector3.one);
// calculating clipping effects
var leftClip = Mathf.Min(rect.xMin, 0);
var rightClip = Mathf.Max(rect.xMax - clipSize.x, 0);
var topClip = Mathf.Min(rect.yMin, 0);
var bottomClip = Mathf.Max(rect.yMax - clipSize.y, 0);
// applying clip
var clip = new Rect(bottomClip, -leftClip, rect.height + topClip - bottomClip, rect.width + leftClip - rightClip);
GUI.BeginClip(clip);
// rendering whatever we want to render
var adjusted = new Rect(-bottomClip, leftClip, rect.height, rect.width);
GUI.color = Color.black;
GUI.DrawTexture(adjusted, BaseContent.WhiteTex);
Text.Font = GameFont.Medium;
GUI.color = Color.white;
Widgets.Label(adjusted, text);
GUI.EndClip();
// restoring GUI matrix
GUI.matrix = matrix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment