- Home /
strange results with GUI.DrawTexture() in the editor scene
I'm creating a quick prototype to test an idea, and working on some editor tools for it. I want to display a sprite over certain points on a grid to indicate where a creature will spawn when the game runs. But when I use GUI.DrawTexture(), I get strange results:
As you can see, the textures only render in the upper right quadrant of the scene view. As I scroll the scene view around, the textures disappear as they cross the center point.
Another odd thing is that if I add a 0.5f vector offset to the position in the code below, the position of the creature textures don't change. And if I half the size of the Rect they disappear.
Here's the code:
[DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy)]
static void DrawSprite(GridObject gridObject, GizmoType gizmoType)
{
if (gridObject.spawnType == GridProperties.SpawnType.Basic)
{
Sprite sprite = gridObject.properties.spawns [((int)gridObject.spawnType) - 1].GetComponentInChildren <SpriteRenderer> ().sprite;
Rect texRect = new Rect (gridObject.transform.position, new Vector2 (1f, -1f));
GUI.DrawTexture (texRect, sprite.texture);
}
}
The only think I can think is that I'm doing something fundamentally wrong with the GUI.DrawTexture method.
Answer by adsilcott · Apr 16, 2017 at 06:00 PM
I made a test scene and tried it again but with:
GUI.DrawTextureWithTexCoords ()
EditorGUI.DrawTextureTransparent ()
EditorGUI.DrawPreviewTexture ()
EditorGUI.DrawTextureAlpha ()
They all do the same thing, clipping to the upper right corner of the scene view, even in 3d view. I'm wondering if this should be a bug report.
Anyway, what finally worked for me was:
Gizmos.DrawGUITexture(texRect, sprite.texture);
The new code is:
[DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy)]
static void DrawSprite(GridObject gridObject, GizmoType gizmoType)
{
Rect texRect = new Rect (gridObject.transform.position, new Vector2 (1f, -1f));
if (!Application.isPlaying)
Gizmos.DrawGUITexture(texRect, gridObject.sprite.texture);
}
However I still can't seem to offset the position of the Rect. It always snaps to the nearest 1 unit grid lines. For example if I change it to:
Rect texRect = new Rect ((Vector2) gridObject.transform.position + new Vector2(.5f, .5f), new Vector2 (1f, -1f));
They all jump over one unit space, rather than half a unit as expected. I might start a new question for this.
I stucked on the same problem when Gizmos.DrawGUITexture jump over units, did you find a solution yet?
I'm afraid not, I moved on to another project and haven't used Gizmos.DrawGUITexture much since then.