Quantcast
Viewing all articles
Browse latest Browse all 86

Answer by Vandarthul

Lets just assume that your game's scene's name is *level1* and gameover scene's name is *gameOver*. First of all you will have to change your code to display GameOver screen as Application.LoadLevel("gameOver"); Now you will have to design your Game Over scene. Since this is a simple scene that you've asked, I would go prepare a Game Over screen via a painting program(photoshop, paint.net) and googling "game over screens" would give you a quick idea how to make one. Generally its "Game Over" text over a black background. Anyway when your Game Over screen is ready drag it to the unity. Now create a script and attach it to the Main Camera. Open it and; public Texture gameOverTexture; void OnGUI() { GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture); if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!")) { Application.LoadLevel("level1"); } if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2 + 25, 150, 25),"Run away, you coward! That's the only thing that you can do here.")) { Application.Quit(); } } Now attach your Game Over screen to gameOverTexture in Editor and press play, this should be pretty much it. Well, you may need to change messages over the buttons here :) **In case you don't know what's going on there, read this:** Now lets go through the code. Rect(left,top,width,height); *Rect* takes 4 parameters here, first 2 being *top* and *left* which is simply "how much pixel you want to add to your object starting from left and top" So when you type 10, 10 there your object will start occuring from left, after 10 pixels and from top after 10 pixels. Now the other 2 parameter says *width* and *height* this is the width and height of your object that you want to create. Pretty much self-explanatory. GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture); So on your *GUI.DrawTexture* code, you see *Screen.width* and *Screen.height* and those are the values of your screen's width and screen's height. Considering your game will be played in different resolutions, those little variables there automatically taking the computer's resolution and making your objects appear corretly. So either you open it at 800*600 or 1920*1080 you will get your Game Over screen correctly drawed(make sure your Game Over Screen's resolution is high enough to cover deformations over low-res screens. Lastly we add our *gameOverTexture* because our *GUI.DrawTexture* wants us so :) if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!")) When we add *GUI.Button* using if statement, we simply say "draw this button and whenever someone clicks to it ..." here, we say start *level1*. This piece of code doesn't have anything different from what I've explained, except *Screen.width / 2* and *Screen.height / 2*. Yes, we can do mathematical operations using Screen.width and Screen.height methods. So *Screen.width / 2* will start from in the middle of the screen vertically, and *Screen.height / 2* starts from in the middle of the screen horizontally. When looking at the message, -wow its a long message- it asks for *GUIContent*. Maybe you want to create a little image just before your text inside your button, you can create it with *GUIContent*(haven't tried but should happen). if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2 + 25, 150, 25),"Run away, you coward! That's the only thing that you can do here.")) I know it's all the same except *Screen.height / 2 + 25* part. Now that's an important part. So we want this button to appear right next to our previous button. Therefore we draw this button after 25 pixels of our previous button horizontally. Because our previous button's height is 25 pixels. I believe you got the idea. **What if you don't want to use default unity styling here?** Now you got your unfriendly buttons insulting your player, but what if you want to change the button's appearing, or font? Say hello to our dear friend *GUISkin*. Firstly you will have to create a GUISkin on your Editor by clicking Create - GUI Skin under your Project. This basically allows you to change every piece of GUI material in your project. After creating, go through your code and add a public variable: public GUISkin myGUISkin; And on top of your ONGUI: void OnGUI() { GUI.skin = myGUISkin; GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),gameOverTexture); if (GUI.Button(new Rect(Screen.width / 2, Screen.height /2, 150, 25),"Don't try again, you are going to lose it anyway, because you are a LOOSER!")) { . . //Rest of the code here . Now, select our newly-created GUISkin and attach it to the public variable that we've just created in the code. After that, click to GUISkin again and Click *Button-Normal* and change *Text Color*. When you start the screen, you will see that your texts' color will change accordingly. So you can change the background of your buttons as you want. The rest, *Hover Active Focused* are states of your buttons. For example if you bring your cursor over your button its actually hovering your cursor over button. So if you want to create funky GUI, you can also change the Hover's background and achieve it. That's pretty much it. I hope you enjoy my explanations and I strongly suggest you to search for your problem first. For example googling "how to make a button in unity" "how to customise a button in unity" would do the trick. There are ridiculously number of tutorials out there just for unity covering mostly every aspect of it. And you can be sure that general questions/problems like this is being asked before you. And its a lot faster than opening a new question and waiting for an answer. Also and lastly you should use [**THIS**][1](quick tip, CTRL+F to search is way faster than that little search box over there) website as it's your main source of unity scripting. Its like uber magic handbook of unity scripting, and you will get what you want mostly. In case of your problem, you could simply look at [**HERE**][2]. Anyway that's it for me, good luck with your game :) **Note:** I didn't quite try the scripts that I've provided, but you got the idea and you are now capable of fixing funny stuff if happening. [1]: http://docs.unity3d.com/Documentation/ScriptReference/index.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/GUI.html

Viewing all articles
Browse latest Browse all 86

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>