help to fix bug with using GUIUtility.RotateAroundPivot + GUI.DrawTexture

Please help to fix this bug. I added my code + gif with bug


private void DrawRotatedTexture(Vector2 itemPosition,int itemAngle, float cellSize,ElementInfo itemInfo,bool displayItemBackground,bool isButton,Action onClickCallBack)
    {
        fullRect = itemInfo.GetFullRect(itemPosition.x, itemPosition.y, cellSize);
        textureRect = itemInfo.GetRect(itemPosition.x, itemPosition.y, cellSize);

        matrixBackup = GUI.matrix;
        GUIUtility.RotateAroundPivot(itemAngle, itemPosition);    
        
        if (displayItemBackground)
        {
            GUI.DrawTexture(fullRect, itemBackgroundTexture, ScaleMode.StretchToFill);
        }
        
        GUI.DrawTexture(textureRect, itemInfo.texture, ScaleMode.ScaleToFit);
        
        if (isButton)
        {
            if (GUI.Button(fullRect, GUIContent.none, GUIStyle.none))
            {
                onClickCallBack?.Invoke();
            }
        }   
        
    
        GUI.matrix = matrixBackup;
    }

159388-bug-gif.gif

I discovered that this is simply unity bug since 2012. No matter what you do unity cuts the image and only then rotates. I created a solution that was far from perfect but enought for my situation.

159822-photo-2020-05-14-15-01-41.jpg
my solution

If we need to draw item in position of blue rectangle and there are not enought space. We draw item in position of green rectangle and rotate using yellow axis and then rotate again with blue axis. See the code below:


`private void DrawRotatedTexture(Vector2 itemPosition, int itemAngle, float cellSize, ElementInfo itemInfo, bool displayItemBackground, bool isButton, Action onClickCallBack)
{
matrixBackup = GUI.matrix;

    fullRect = itemInfo.GetFullRect(itemPosition.x, itemPosition.y, cellSize);
    textureRect = itemInfo.GetRect(itemPosition.x, itemPosition.y, cellSize);

    if ((itemAngle != 0) && ((Screen.width- fullRect.xMax < 30) || (Screen.height - fullRect.yMax + globalScrollVector.y < 30)))
    {
        secondRotatingPoint = itemPosition;
        firstRotatingPoint = secondRotatingPoint - fullRect.size;
        fullRect.position = firstRotatingPoint - fullRect.size;
        textureRect = itemInfo.GetRect(fullRect.position.x, fullRect.position.y, cellSize);

        //GUI.DrawTexture(fullRect, greenTexture, ScaleMode.StretchToFill); debug stuff
        GUIUtility.RotateAroundPivot(180, firstRotatingPoint);
        //GUI.DrawTexture(fullRect, redTexture, ScaleMode.StretchToFill);   debug stuff
        GUIUtility.RotateAroundPivot(180 + itemAngle, secondRotatingPoint);
    }
    else
    {
        GUIUtility.RotateAroundPivot(itemAngle, itemPosition);           
    }

    if (displayItemBackground)
    {
        GUI.DrawTexture(fullRect, itemBackgroundTexture, ScaleMode.StretchToFill);
    }

    GUI.DrawTexture(textureRect, itemInfo.texture, ScaleMode.ScaleToFit);

    if (isButton)
    {
        if (GUI.Button(fullRect, GUIContent.none, GUIStyle.none))
        {
            onClickCallBack?.Invoke();
        }
    }

    GUI.matrix = matrixBackup;
}`

Hope it helps someone