Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make texture centre and fill screen

Discussion in 'Scripting' started by AtomicCabbage33, Nov 28, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I am trying to make my texture fill the entire screen but stay centred, it needs to do this despite the current render window's size so I have used Screen.width and Screen.height. However, the texture does not centre?

    Code (CSharp):
    1.     void OnGUI()
    2.     {
    3.         float width = 600;
    4.         float height = 600;
    5.        
    6.         if (_isScoped) {
    7.             GUI.DrawTexture (new Rect ((Screen.width / 2) - (width/2), (Screen.height / 2) - (height/2), Screen.width, Screen.height), scopeGUI);  
    8.             //weaponCam.clearFlags = CameraClearFlags.SolidColor;
    9.         }
    10.     }
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    Try to use only Screen values to centre it.

    Code (CSharp):
    1. GUI.DrawTexture (new Rect (Screen.width / 2, Screen.height / 2, Screen.width, Screen.height), scopeGUI);
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    That appears to have fixed the scaling issue however the texture now gets centred by its upper left corner, rather than the centre of the image. I have changed the pivot of the texture inside of unity so that the X and Y values are half the size of the image, which should centre it however this made no difference?
     
  4. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    You're using legacy GUI. Use the new UI, and this kind of stuff is simple and script-free.
     
  5. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I have thought about it but as the canvas would have to be attached to the gun game object, the image would move around rather than having a static position in the centre of the screen. The canvas cannot be just a gameobject in the map as you cannot reference it in another script without making it a prefab, and this causes nothing to be displayed. I think that using the new UI will end up being more complicated so I resorted to the old legacy GUI.
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Nope. It's definitely more complicated with the legacy GUI. You can reference the canvas in another script without making it a prefab. I'm not sure what your process was, but if what you're trying to do wasn't working with the new UI, you were doing it wrong. For one, the canvas doesn't need to be attached to your gun GameObject.
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You can have multiple canvases. And if you need a image centered on the screen independent of anyting else.. then ScreenSpace-Overlay type of canvas is made exactly for this. It always just covers your screen. You can then stick a UI Image in the middle of it with your Texture in it. Super simple to do.
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    There is no pivot with IMGUI.
    Code (CSharp):
    1.         GUI.DrawTexture (new Rect (0,0, Screen.width, Screen.height), texture);
    2.  
     
    AtomicCabbage33 likes this.
  9. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Worked perfectly, cheers.