Search Unity

Gizmos.GUIDrawTexture() How does it work?

Discussion in 'Immediate Mode GUI (IMGUI)' started by rightpurdy, Sep 21, 2008.

  1. rightpurdy

    rightpurdy

    Joined:
    Oct 31, 2007
    Posts:
    39
    Can someone please give me a working example of this? I have tried for about 4 hours to get a GUITexture gizmo to show on screen and there is nothing visible.

    Here is what I am doing:
    Code (csharp):
    1.  
    2. var buttonGizmo : Texture2D;
    3. var pixelPosX = 0;
    4. var pixelPosY = 0;
    5.  
    6. function OnDrawGizmos()
    7. {
    8.         Gizmos.DrawGUITexture(Rect(pixelPosX,pixelPosY,buttonGizmo.width,buttonGizmo.height,buttonGizmo);
    9. }
    I'm not sure why that doesn't work , I have the Gizmos button turned on in the Game View, maybe I just don't understand how DrawGUITexture works.

    Thank you for any help.
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Gizmos are only viewable in the scene view unless you toggle gizmos on in the game view. However the documentation on this particular function is quite sparse. I don't get how the Rect parameter is used.
     
  3. teaMate

    teaMate

    Joined:
    Nov 18, 2010
    Posts:
    4
    I know this is an old thread, but i'm experiencing the same problem with DrawGUITexture. No matter what i try, the texture doesn't show up.
    Nor in the scene view neither in the game view(i have Gizomos toggled on).
    If someone have found a solution in the meanwhile, can help me?
     
    IgorAherne likes this.
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Welcome to the forum, teaMate!

    Can you post the code that you are having problems with?
     
  5. teaMate

    teaMate

    Joined:
    Nov 18, 2010
    Posts:
    4
    Thank you!
    Ok here is my code
    Code (csharp):
    1.  
    2. private var mainGUI : DrawMainMenu;
    3. var gui_reference1 : Texture;
    4. ...
    5. ...
    6. function Start(){
    7.  mainGUI = GetComponent(DrawMainMenu);
    8.  ...
    9. }
    10. ...
    11. ...
    12. function OnDrawGizmo(){
    13.  
    14.  Gizmos.DrawGUITexture(Rect(mainGUI.tab.inset), gui_reference1);
    15.  
    16. }
    17.  
    I got no errors or warnings.
    Everything is fine but it doesn't draw any texture on viewport.
    I need this texture to help in the building of the gui, to better understand the position of elements on screen, without play the scene everytime.
     
    Last edited: Nov 18, 2010
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I hate to tell you this, but the problem is that the function is really supposed to be called OnDrawGizmos with an S at the end. Declaring a function called OnDrawGizmo is not an error but it doesn't get called automatically by Unity.
     
  7. teaMate

    teaMate

    Joined:
    Nov 18, 2010
    Posts:
    4
    Yes you're right. I appreciate your help. I'm feeling a bit "stupid"...oh well.
    Thank you again
     
  8. teaMate

    teaMate

    Joined:
    Nov 18, 2010
    Posts:
    4
    I'm still having problems figuring out how the Rect parameter is used.
    From the scripting reference:
    But it seems it doesn't use screen coordinates.
    Doesn't it works similar to a GUI element? Am i missing something?
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The description is a bit misleading. What actually happens is that a texture gets drawn in the scene in the XY plane (ie, it treats the plane as though it were a "screen" with the Z coordinate ignored). The texture's bottom left corner and also its width and height are given in world coordinates.
     
  10. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410

    Attached Files:

    Rodolfo-Rubens likes this.
  11. Turbine

    Turbine

    Joined:
    Nov 9, 2012
    Posts:
    11
    Don't even try Gizmos.matrix here. It does nothing.
     
  12. coward

    coward

    Joined:
    Jan 9, 2013
    Posts:
    21
    This is an OLD post, but turns out that the RECT relates to world coordinates. So 0,0 puts it at transform.position... almost. I seem to need to make it 0, -1 for position.
     
  13. gnumaru

    gnumaru

    Joined:
    Dec 26, 2013
    Posts:
    20
    Not intending to necrobump the thread, but I also had a hard time with 'UnityEngine.Gizmos.DrawGUITexture' apparently not working as intended. For the sake of future googlers, here is a working example:


    using System;
    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif

    public class MyGizmo : MonoBehaviour {
    public Texture texture;
    public Vector2 viewSize = Vector2.one;

    private Rect rect;
    private Texture _tex;
    private static Texture defautlTexture; // shared default texture

    public static readonly Color fullTransparent = new Color(0, 0, 0, 0);

    public void OnDrawGizmos() {
    #if UNITY_EDITOR
    if(defautlTexture == null) defautlTexture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/defaultGizmo.png");
    #endif

    if(_tex == null) {
    if(texture!=null) _tex = texture;
    else if(defautlTexture != null) _tex = defautlTexture;
    else {
    Debug.Log("OnDrawGizmos: No texture assigned.");
    return;
    }
    }

    rect = new Rect((Vector2)transform.position - Vector2.one, viewSize);
    // render _tex instead of texture for alowing to load and render a default texture without setting the default texture reference to the inspector field
    Gizmos.DrawGUITexture(rect, _tex);

    // draw a full transparent box just to make the texture gizmo clickable
    Gizmos.color = fullTransparent;
    Gizmos.DrawCube(transform.position, Vector3.one);
    }

    public void OnDrawGizmosSelected() => OnDrawGizmos();
    }
     
  14. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Gizmos.DrawGUITexture - still trash in Unity 2019.4. Docs are bad. Hard to believe anyone checked this before publishing it: https://docs.unity3d.com/ScriptReference/Gizmos.DrawGUITexture.html

    It's only a couple of lines long, and yet that source code is blatantly wrong (and appears not to have been tested).

    EDIT: The method itself? Badly designed, does an almost useless thing (who thought it was a good idea to arbitrarily place the texture at a random place in Z and not allow us to position it?). Unity is a *3D* engine, and the rest of the engine often positions things in Z, but this stupid method messes that up. Argh. So many bugs caused by this!
     
    gjf likes this.
  15. gjf

    gjf

    Joined:
    Feb 8, 2012
    Posts:
    53
    Did you find a workaround? If only they'd allow icons in any folder and not just Assets\Gizmos! Or can you place them anywhere now?
     
  16. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    All of the Gizmos.* API needs to be avoided / deleted - it's not being maintained, it's not had fixes in many years, and the last bug I submitted got rejected with little more than "yeah we don't see a problem with the fact this doesn't work, even though we had to create an internal method to workaround it, that you can see in our source-code - so our own staff feel it's broken, but we're not going to fix it".

    Rewrite Gizmos yourself, and do it properly - don't use Unity's junk code (that they don't care about any more :))
     
    gjf likes this.