- Home /
Problem is not reproducible or outdated
I have created a button with photoshop and i need to use it in my menu
I have created a button with photoshop and i am trying to use it instead of my buttons I created.
Till now I have a script with C# that is attached to the main camera. The script basically makes the main menu for me but i would like to change the buttons generated with an image I designed.
This is what I have till now:
using UnityEngine; using System.Collections;
public class myMenu : MonoBehaviour {
private delegate void MenuDelegate();
private MenuDelegate menuFunction;
private float screenHeight;
private float screenWidth;
private float buttonHeight;
private float buttonWidth;
// Use this for initialization
void Start () {
screenHeight = Screen.height;
screenWidth = Screen.width;
buttonHeight = screenHeight * 0.3f;
buttonWidth = screenWidth * 0.4f;
menuFunction = anyKey;
}
void OnGUI()
{
menuFunction();
}
void anyKey()
{
if(Input.anyKey)
{
menuFunction = mainMenu;
}
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.Label(new Rect(screenWidth * 0.45f, screenHeight * 0.45f, screenWidth * 0.1f, screenHeight * 0.1f), "Press any key to continue");
}
void mainMenu()
{
// Main Menu
// Start Game
// How to Play
// Settings
// Quit
GUI.Box(new Rect((screenWidth - buttonWidth) * 0.6f, screenHeight * 0.05f, buttonWidth * 0.7f, buttonHeight * 2.5f), "Main Menu");
if (GUI.Button (new Rect((screenWidth - buttonWidth) * 0.7f, screenHeight * 0.15f, buttonWidth * 0.4f, buttonHeight * 0.4f), "Start Game"))
{
Application.LoadLevel ("Level1");
}
if (GUI.Button (new Rect((screenWidth - buttonWidth) * 0.7f, screenHeight * 0.3f, buttonWidth * 0.4f, buttonHeight * 0.4f), "How to Play"))
{
}
if (GUI.Button (new Rect((screenWidth - buttonWidth) * 0.7f, screenHeight * 0.45f, buttonWidth * 0.4f, buttonHeight * 0.4f), "Settings"))
{
}
if (GUI.Button (new Rect((screenWidth - buttonWidth) * 0.7f, screenHeight * 0.6f, buttonWidth * 0.4f, buttonHeight * 0.4f), "Quit Game"))
{
Application.Quit();
}
}
}
My goal at the moment is to find a way how to make the script load the image and once clicked it does something like application.quit.
i have never coded in C# before, just Java and .Net, but i can understand C# since they are similar.
and what if i just simply drag and drop it in front of the main camera, making the camera facing 1 position during the main menu. Will i achieve the same results? what happens if the game gets maximized? will the GUI scatter? Sorry but i am still new to unity
Answer by Joseph118 · Feb 03, 2013 at 03:58 PM
after some research i have found that i need to call the image from my materials using the following code
private Texture btnStart;
btnStart = (Texture)Resources.Load("btnStart");
if (GUI.Button (new Rect((screenWidth - buttonWidth) 0.7f, screenHeight 0.15f, buttonWidth 0.4f, buttonHeight 0.4f), btnStart))
Thank you for your attempts
Answer by DryTear · Feb 02, 2013 at 05:51 PM
add
public GUISkin mySkin;
after the OnGui function has to be
void OnGui(){
GUI.skin = mySkin;
menuFunction();
}
it gives me a compile error, please tell me more about GUI.skin
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Selecting and deselecting not working 0 Answers
Distribute terrain in zones 3 Answers