Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Texture2D in OnGUI appears semi-transparent

Discussion in 'Scripting' started by Cybearg, Jul 3, 2015.

  1. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    I'm trying to use a pure black texture2D with varying alpha values to create a fade-in/fade-out effect for scenes. However, the black texture2D appears semi-transparent, even when its alpha is 1. What's wrong?

    Code (csharp):
    1.  
    2. private Texture2D blackTexture;
    3.  
    4. void Start () {
    5.         blackTexture = new Texture2D (Screen.width, Screen.height);
    6. }
    7.  
    8. void OnGUI() {
    9.         GUI.depth = -1000;
    10.         GUI.color = new Color(0, 0, 0, 1f);
    11.         GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), blackTexture );
    12. }
    13.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're not setting the texture's colour to anything... no idea what the default for a newly created texture is but try setting it to something you want.
     
  3. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    Thank you! That was likely the problem.

    I also found this example, where the poster creates a 1x1 pixel Texture2d, so there's only 1 pixel that needs to be set to black. Using it that way, it works great!

    Apparently there's also a static Texture2D.blackTexture, but I couldn't get it to work; I didn't see anything when I attempted to draw it. No idea why.