- Home /
Help with C# coding a loaded button (3.x Game Development Essentials)
Hey everyone! I've been following Will's excellent new book "3.x Game Development Essentials" and fished it earlier today, however there is a small hiccup causing me some annoyances... In the book he creates a Play button, but if the game is to be loaded in a web player, this play button is replaced by a "loading" button, until 100% has been loaded, and then the button should swap back over to play. My problem lies with 2 things; 1. This button also appears on the Standalone version 1. The button is always there. Above my play button, the "Loading..100% loaded" is always visible (this doesnt prevent you from clicking the play button though) I checked over the book plenty of times, but simply cannot figure out where I went wrong, I'm pasting my entire "MenuButtons" script below, hoping for some help (:
using UnityEngine; using System.Collections; [RequireComponent (typeof (AudioSource))]
public class MainMenuGUI : MonoBehaviour {
public AudioClip beep; public GUISkin menuSkin; public Rect menuArea; public Rect playButton; public Rect instructionsButton; public Rect quitButton; Rect menuAreaNormalized; string menuPage = "main"; public Rect instructions;
void Start() {
menuAreaNormalized = new Rect(menuArea.x * Screen.width - (menuArea.width * 0.5f), menuArea.y * Screen.height - (menuArea.height * 0.5f), menuArea.width, menuArea.height);
}
void OnGUI() {
GUI.skin = menuSkin;
GUI.BeginGroup (menuAreaNormalized);
if(menuPage == "main") {
if(Application.CanStreamedLevelBeLoaded("Island")){
if(GUI.Button(new Rect (playButton), "Play")) {
StartCoroutine("ButtonAction", "Island");
}else{
float percentLoaded =
Application.GetStreamProgressForLevel(1) * 100;
GUI.Box (new Rect(playButton), "Loading.." + percentLoaded.ToString("f0") + "% Loaded");
}
}
if(GUI.Button(new Rect (instructionsButton), "Instructions")) {
audio.PlayOneShot(beep);
menuPage="instructions";
}
if(Application.platform != RuntimePlatform.OSXWebPlayer && Application.platform != RuntimePlatform.WindowsWebPlayer){
if(GUI.Button(new Rect (quitButton), "Quit")) {
StartCoroutine("ButtonAction", "quit");
}
}
} else if(menuPage == "instructions") {
GUI.Label (new Rect(instructions), "You awake on a mysterious island... Find a way to signal for help or face certain doom!");
if(GUI.Button (new Rect(quitButton), "Back")) {
audio.PlayOneShot(beep);
menuPage="main";
}
}
GUI.EndGroup();
}
IEnumerator ButtonAction(string levelName) {
audio.PlayOneShot (beep);
yield return new WaitForSeconds(0.35f);
if(levelName != "quit") {
Application.LoadLevel (levelName);
}else{
Application.Quit ();
Debug.Log ("HaveQuit");
}
}
}
The specific snippet affecting the button is here;
if(Application.CanStreamedLevelBeLoaded("Island")){ if(GUI.Button(new Rect (playButton), "Play")) {
StartCoroutine("ButtonAction", "Island");
}else{
float percentLoaded =
Application.GetStreamProgressForLevel(1) * 100;
GUI.Box (new Rect(playButton), "Loading.." + percentLoaded.ToString("f0") + "% Loaded");
}
}
Thank you so much in advance!
Answer by prototype7 · Feb 23, 2013 at 01:20 AM
what if you add this script
GUI.BeginGroup (menuAreaNormalized);
if (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer)
{
// GUI stuff
}
GUI.EndGroup();
And it works! :) Thank you so much mr./ms. prototype! :)