- Home /
Making GUI Buttons on a GUI Texture
I've got the following script on my Camera:
using UnityEngine;
using System.Collections;
public class GUIDisplay : MonoBehaviour
{
public Texture2D background;
public GUISkin mySkin;
void OnGUI()
{
GUI.skin = mySkin;
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background);
}
}
This allows me to assign a texture in the Inspector that adjusts to the screen size.
The texture I'm using is like a HUD and has text on it that I want to use as buttons. Is it best to use GUI.Button and try and mathematically line it up with the text, or is there another way?
I'm thinking that that way may be unreliable if the texture is adjusting to different screen sizes; the buttons would slide out of line right?
Answer by Srki94 · Oct 13, 2014 at 01:28 PM
I wouldn't suggest using fixed values. Instead of X pixels from edge, use percentage.
That way no matter what size of, for example, window you are using, all controls will stay on the same positions.
I'm thinking that that way may be unreliable if the texture is adjusting to different screen sizes; the buttons would slide out of line right?
Kind of. If you place button at 500 px from X and it looks good, then you resize your texture, button will be at same 500px position.
Here is a snippet of code that I used once to keep all components inside window at same position, no matter what size window is :
GUI.Label(Rect(60.0/100*prozor.width, 9.6/100*prozor.height, 80, 50), "This is an label!");
Where "prozor" is actual GUI Window (read : Rect that Window uses). In your case you would probably want to use texture W & H instead.
This will allow you to keep all components at same percentage of window or texture, no matter what size that is. Also, if you resize that window or texture anytime at runtime, all components will follow.
Good luck !
Your answer
Follow this Question
Related Questions
GUI.DrawTexture on GUI.Button press 1 Answer
Animate GUI Elements 1 Answer
Assiging an Image to a Button 1 Answer
Best way to show an image 2 Answers
How to create a button that doesnt render in the main camera? 2 Answers