Displaying the achievements on screen

Just as we did with the stats, we will have a new menu for achievements. First, we'll start by adding a couple of variables:

public bool showAchievements = false;
public Rect achRect = new Rect(Screen.width / 2, Screen.height / 2, 700, 700);

Adding the GUI functions

Now, we will add the functions to show the achievements on the screen. The first function is the OnGUI function, which we will add now:

void OnGUI()
{
  if(showAchievements)
  {
    achRect = GUI.Window(0, achRect, AchGUI, "Achievements");
  }
}

Just as in the stats menu, we check whether we want to show the achievements menu. If we do it, is shown on screen; if not, we hide it.

Next, we will add the AchGUI function that is being called in the OnGUI function. This is a large function, but it will allow us to show the achievements that we need. It is similar to the stat menu, except we will show buttons instead of a number. We use buttons just as a proof of concept; normally, you would use an image for your achievements.

What this function will do is use a switch statement to check the level of each achievement that we track. Then, it will show the number of achievement buttons onscreen according to what level the player is at, within that achievement. Let's add the new function now:

void AchGUI(int ID)
{
  GUILayout.BeginArea(new Rect(15, 25, 700, 700));
  
  GUILayout.BeginVertical();
  GUILayout.Label("Level");
  GUILayout.Label("Kills");
  GUILayout.Label("Total Gold");
  GUILayout.Label("Gold Spent");
  GUILayout.Label("Rounds Won");
  GUILayout.Label("Time Played");
  GUILayout.EndVertical();
  
  GUILayout.EndArea();

  GUILayout.BeginArea(new Rect(50, 25, 700, 700));
  
  GUILayout.BeginHorizontal();
  if(achLvl >= 1)
      GUILayout.Button("Level 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achLvl >= 2)
      GUILayout.Button("Level 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achLvl >= 3)
      GUILayout.Button("Level 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achLvl >= 4)
      GUILayout.Button("Level 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achLvl >=5)
      GUILayout.Button("Level 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achLvl >=6)
      GUILayout.Button("Level 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();

  GUILayout.BeginHorizontal();
  if(achKills >= 1)
      GUILayout.Button("Kills 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achKills >= 2)
      GUILayout.Button("Kills 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achKills >= 3)
      GUILayout.Button("Kills 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achKills >= 4)
      GUILayout.Button("Kills 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achKills >=5)
      GUILayout.Button("Kills 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achKills >=6)
      GUILayout.Button("Kills 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();
  GUILayout.EndArea();
  
  GUILayout.BeginArea(new Rect(90, 80, 700, 700));
  GUILayout.BeginHorizontal();
  if(achTotGold >= 1)
      GUILayout.Button("Total Gold 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achTotGold >= 2)
      GUILayout.Button("Total Gold 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achTotGold >= 3)
      GUILayout.Button("Total Gold 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achTotGold >= 4)
      GUILayout.Button("Total Gold 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achTotGold >=5)
      GUILayout.Button("Total Gold 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achTotGold >=6)
      GUILayout.Button("Total Gold 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();
  
  GUILayout.BeginHorizontal();
  if(achGoldSpnt >= 1)
      GUILayout.Button("Gold Spent 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achGoldSpnt >= 2)
      GUILayout.Button("Gold Spent 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achGoldSpnt >= 3)
      GUILayout.Button("Gold Spent 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achGoldSpnt >= 4)
      GUILayout.Button("Gold Spent 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achGoldSpnt >=5)
      GUILayout.Button("Gold Spent 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achGoldSpnt >=6)
      GUILayout.Button("Gold Spent 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();
  
  GUILayout.BeginHorizontal();
  if(achRndsW >= 1)
      GUILayout.Button("Rounds Won 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achRndsW >= 2)
      GUILayout.Button("Rounds Won 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achRndsW >= 3)
      GUILayout.Button("Rounds Won 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achRndsW >= 4)
      GUILayout.Button("Rounds Won 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achRndsW >=5)
      GUILayout.Button("Rounds Won 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achRndsW >=6)
      GUILayout.Button("Rounds Won 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();
  
  GUILayout.BeginHorizontal();
  if(achTime >= 1)
      GUILayout.Button("Time Played 1", GUILayout.Height(25), GUILayout.Width(75));
    if(achTime >= 2)
      GUILayout.Button("Time Played 2", GUILayout.Height(25), GUILayout.Width(75));
    if(achTime >= 3)
      GUILayout.Button("Time Played 3", GUILayout.Height(25), GUILayout.Width(75));
    if(achTime >= 4)
      GUILayout.Button("Time Played 4", GUILayout.Height(25), GUILayout.Width(75));
    if(achTime >=5)
      GUILayout.Button("Time Played 5", GUILayout.Height(25), GUILayout.Width(75));
    if(achTime >=6)
      GUILayout.Button("Time Played 6", GUILayout.Height(25), GUILayout.Width(75));
  GUILayout.EndHorizontal();
  
  GUILayout.EndArea();
}

For testing purposes, I made the achievement level variables public and set them to various values. Drag the script onto the camera, remove the stats script, and set the showAchievements Boolean value to true. If you run the scene, you should see the following results:

Adding the GUI functions

You will probably have slightly different results, based on what you assigned each of the achievement level variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.75