- Home /
Problem Resolution
Hi to all.. I created a main menu, in unity editor it's all right http://imageshack.us/a/img542/4328/3h4h.png , but when I build the game, the position of the public 2D Texture that I had put, are all wrong..(http://imageshack.us/a/img29/2083/lqgf.png) in all resolution, they change position... How can I block their position? I have to set fixed resolution? any ideas?
This is the script:
using UnityEngine;
using System.Collections;
public class MenuCompleto : MonoBehaviour {
public AudioClip suono;
public GUISkin PersonalizzaBottone1;
public GUISkin PersonalizzaBottone2;
public GUISkin PersonalizzaBottone3;
public GUISkin PersonalizzaBottone4;
public bool Mostamenu;
public bool MostraFinestraOpzioni = false;
public Texture sfondo;
public Texture AidaGames;
public Texture MainMenu;
public Texture RideSimulator;
// Use this for initialization
void Start ()
{
Mostamenu = true;
Screen.fullScreen = !Screen.fullScreen;
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
GUI.DrawTexture(new Rect(0,0,1920,1080),sfondo);
GUI.DrawTexture(new Rect(1075,540,200,100),AidaGames);
GUI.DrawTexture(new Rect(100,80,250,80),MainMenu);
GUI.DrawTexture(new Rect(600,80,600,250),RideSimulator);
if(Mostamenu)
{
GUI.skin = PersonalizzaBottone1;
if(GUI.Button(new Rect(100,200,200,70),""))
{
GUI.Button(new Rect(100,200,200,70),"");
audio.PlayOneShot(suono);
Mostamenu = false;
Application.LoadLevel("livello1");
}
GUI.skin = PersonalizzaBottone2;
if(GUI.Button(new Rect(100,280,200,70),""))
{
GUI.Button(new Rect(100,280,200,70),"");
audio.PlayOneShot(suono);
Mostamenu = false;
}
GUI.skin = PersonalizzaBottone3;
if(GUI.Button(new Rect(100,360,200,70),""))
{
GUI.Button(new Rect(100,360,200,70),"");
audio.PlayOneShot(suono);
MostraFinestraOpzioni = true;
Mostamenu = false;
}
GUI.skin = PersonalizzaBottone4;
if(GUI.Button(new Rect(100,440,200,70),""))
{
GUI.Button(new Rect(100,440,200,70),"");
audio.PlayOneShot(suono);
Mostamenu = false;
Application.Quit();
}
}
if(MostraFinestraOpzioni)
{
GUI.Window(0,new Rect(Screen.width / 2 - 250 ,Screen.height / 2 - 125,500,150),Opzioni,"Opzioni");
}
}
void Opzioni (int id)
{
if(GUILayout.Button("Video"))
{
Debug.Log("Tasto Video Premuto");
}
GUILayout.Button("Audio");
GUILayout.Button("Comandi");
if(GUILayout.Button("Chiudi"))
{
audio.PlayOneShot(suono);
MostraFinestraOpzioni = false;
Mostamenu = true;
}
}
}
Answer by fafase · Aug 22, 2013 at 10:18 AM
This is probably due to your Free Aspect resolution. Top left of the Game view is a drop down that allows to select a particular resolution.
This helps to get the appropriate resolution for various devices.
Also, it is better to use Screen.height/width to make sure something shows up and rescale automatically.
So you should me to remove display resolution dialog? And set a specific graphic size? (in the settings when I build the game) I tried the game in another PC (more small) with the fixed resolution,and not work well! :( the menu's images are cut.. :(
That is what I am telling you. If you set an object at 900,0, on a 1280/960 it goes fine, on a screen, on a 640/480 it is out of the screen. If ins$$anonymous$$d you had used:
x = Screen.width / 10 * 9;
Then whatever the resolution is, the button will get at 9/10 of the screen. Use the same process for the size of the rect and everything will automatically scale to the screen.
But where should I put this part of script? (x = Screen.width / 10 * 9;) sorry for the stupid question..
GUI.DrawTexture(new Rect(0,0,1920,1080),image);
Let's say you want a square box with side 1/2 of the width of the screen to be right in the middle.
Rect box;
void Start(){
float edge = Screen.width / 2;
// xPos is from the middle
float xPos = Screen.width / 2 - edge / 2;
float yPos = Screen.height / 2 - edge / 2;
box = new Rect(xPos, yPos, edge, edge);
}
void OnGUI(){
GUI.DrawTexture(box, image);
}
I recommend to do that on the Start as it makes the method clearer to me. Also, storing values in variables like edge allows you to have a quick way to modify the size without changing many values.
If for instance, you want your box to be two third of the screen ins$$anonymous$$d of one half, you only do:
float edge = Screen.width / 3 * 2;
and the rest will follow.
But so the image seen at the middle of the screen.. I hadn't ask for the middle, but in a specific fixed position.. :)
Answer by Skibur · Aug 24, 2013 at 06:22 AM
Replace this
GUI.DrawTexture(new Rect(1075,540,200,100),AidaGames);
to:
GUI.DrawTextrue(new Rect(Screen.width - AidaGames.width - 10, Screen.height - AidaGames.height - 10, AidaGames.width, AdiaGames.height), AidaGames);
using Screen.width/Screen.height will automatically obtain the length and width of your game window, making your icon always at the corner of your screen no matter what.
The one you wrote is a fixed position meaning that no matter what you resize your game to, it will always draw at that coordinate that you assign the texture to.
You still have a 10 there in your solution. So partly fix this issue, this 10 will be 10 whatever the dimension. And why 10? why not 42 since it is always the answer.
the 10 is making sure the icon isn't exactly at the corner, so it appears to be at the corner but with a little space inward of the window.