Hi, I’m having this same issue with rotated textures being drawn inside a GUI Group not clipping to the group’s bounds. I see it was marked as resolved over two years ago, but this doesn’t seem to be the case.
I am trying to draw icons on a map, one of these icons represents the player, and it needs to be rotated to match where the player is facing.
Per suggestion, here is a simplified example of how I’m drawing the icons:
void ChildOnGUI (){
...
//Start GUI Group
GUI.BeginGroup(maskedMapTexture.cachedBounds);
//draw location icons
for(int i=0; i<_drawList.Count; i++){
//If angle is not 0, rotate matrix around center of icon before drawing
Matrix4x4 rollbackMatrix = GUI.matrix;
if(drawData.angle != 0){
Vector2 pivotPoint = new Vector2(drawData.x + halfIconSize, drawData.y + halfIconSize);
//Rotate GUI around pivot point
GUIUtility.RotateAroundPivot(drawData.angle, pivotPoint);
}
//Draw
GUI.DrawTexture(new Rect(drawData.x, drawData.y, iconPixelSize, iconPixelSize), drawData.texture);
//Rollback matrix
GUI.matrix = rollbackMatrix;
}
//End GUI Group
GUI.EndGroup();
...
}
Here is my actual code:
//Menu Draw Method (called from Monobehaviour OnGUI method)
override protected void ChildOnGUI (){
base.ChildOnGUI ();
//Toggle map mode button
mapModeButton.Draw(gameObject);
//Directions label
directionsLabel.Draw(gameObject);
//Draw map background
GUI.Box(maskedMapTexture.cachedBounds, "", "Map Background");
//Draw map
maskedMapTexture.Draw(gameObject);
//Start GUI Group
GUI.BeginGroup(maskedMapTexture.cachedBounds);
//draw location icons
if(drawLocationIcons){
for(int i=0; i<_drawList.Count; i++){
_DrawIcon(_drawList[i]);
}
}
//End GUI Group
GUI.EndGroup();
//Draw overlay texture
GUI.Box(maskedMapTexture.cachedBounds, "", "Map Overlay");
}
//Icon draw method
protected void _DrawIcon(DrawData drawData){
//Calculate x position
float halfIconSize = iconPixelSize / 2;
//Draw icon
if(drawData.texture){
//Calc x draw origin
float x = _zoom * maskedMapTexture.cachedBounds_w * (drawData.normCoords.x - maskedMapTexture.texCoords.x) - halfIconSize;
//Calc y draw origin
float y = _zoom * maskedMapTexture.cachedBounds_h * (drawData.normCoords.y - (1 - maskedMapTexture.texCoords.height - maskedMapTexture.texCoords.y))
- halfIconSize;
//If angle is not 0, rotate matrix around center of icon before drawing
Matrix4x4 rollbackMatrix = GUI.matrix;
if(drawData.angle != 0){
Vector2 pivotPoint = new Vector2(x + halfIconSize, y + halfIconSize);
//Rotate GUI around pivot point
GUIUtility.RotateAroundPivot(drawData.angle, pivotPoint);
}
//Draw
GUI.DrawTexture(new Rect(x, y, iconPixelSize, iconPixelSize), drawData.texture);
//Rollback matrix
GUI.matrix = rollbackMatrix;
}
}
My map and all of my non-rotated icons draw perfectly, clipping at the bounding edges of the GUI group. Its only the rotated icon that is having issue. Any help would be appreciated.