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;
}
Answer by Chizhevsky · May 18, 2020 at 01:06 PM
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.
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
Your answer
Follow this Question
Related Questions
Editor Window 1 Answer
How to add Canvas Scaler and Graphic Raycaster to Canvas in code 1 Answer
Custom EditorWindow indicator 0 Answers
GUI.Window. Wanting to allow clickthrough 0 Answers
How to Have a ReoderableList in a Custom Editor Window 1 Answer