Unity Achievements System Tutorial Pt.3

Unity Achievements System Tutorial Pt.3

Advertisements

Previous Part

In this part You will learn how to create new scene with panel that will list all achievements in your game, and it will highlight achievements that are unlocked.

 

First you need to prefab the Achievement panel from first tutorial. Than add new new Scene to the project, and create UI panel  and add multiple panels to it. Use Grid Layout to adjust the locations of panels.

Capture2

Capture3.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Now Add buttons to bots scenes and make them change between scenes. Use the following method. Remember to add scenes to build settings.

public void LoadLevel(int i)
{
  Application.LoadLevel(i);
}

Attach Script with this method to a button on the scene, and select the scene number;

Capture4

Now Let’s create a scrip that will create achievements panels, and check if achievement is is unlocked.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
using UnityEngine.UI;
public class PanelWithAchievements : MonoBehaviour {

 public static Dictionary<string, bool> achievements = new Dictionary<string, bool>();
 private static bool DictCreated=false;
 private List<Database_Achievements.Achievement> databaseAchievements;
 private JsonData AchData;
 public GameObject achPanel;

  void Start()
  {
  Debug.Log("DictCreated" + DictCreated);
  databaseAchievements = Database_Achievements.databaseAchievements;
//Create Panels based on database items.
  InstntiatePanels();
//Set them all to locked as a default
  foreach (var kvp in achievements)
  {
     GameObject achievement = this.transform.FindChild(kvp.Key).gameObject;
     achievement.transform.FindChild("Panel").gameObject.SetActive(true);
  }
//This will highlight unlocked achievements
  UpdateBoard();

  }

  void InstntiatePanels()
  {
  if (this.transform.childCount==0)
{
     foreach (Database_Achievements.Achievement ach in databaseAchievements)
     {
         GameObject newach = Instantiate(achPanel);
         newach.transform.SetParent(this.transform);
         newach.name = ach.Title;
         newach.transform.GetChild(2).GetComponent&lt;Image&gt;().sprite = ach.image;
         newach.transform.GetChild(0).GetComponent&lt;Text&gt;().text = ach.Title;
         newach.transform.GetChild(1).GetComponent&lt;Text&gt;().text = ach.Describtion;
//Condition for adding only new items
         if (!achievements.ContainsKey(ach.Title))
         {
             achievements.Add(ach.Title, false);
         }
      }
    }
   }
void UpdateBoard()
   {
     foreach (var kvp in achievements)
     {
         if (kvp.Value == true)
         {
             GameObject achievement = this.transform.FindChild(kvp.Key).gameObject;
             achievement.transform.FindChild("Panel").gameObject.SetActive(false);
          }
      }
   }
}

I’m using dictionary to store the state of an achievement and i am using name as a key.
This scrip is attached to a panel object on new scene.

In our AchievementPanel Scrip from previous part we add one extra line in CountClicks() Method

void CountClicks()
{
 numberOfClicks++;
 if (numberOfClicks &gt;= 10 &amp;&amp; !achievement01Unlocked)
 {
   PassInformations(1);
   StartCoroutine(PanelCall());
   achievement01Unlocked = true;
//This one here
   PanelWithAchievements.achievements["Explorer"] = true;

 }
}

 

You need to delete all achievements panels from layout, because script will create a new one. Start game on Game Screen and then if achievement will be unlocked go to next scene and see if achievement is highlighted;

 

Tut04

 

To Avoid creating new panels every time site loads. We need to include one condition in Database script.

if (databaseAchievements.Count==0)

This will prevent creating too many objects.

 void ConstructItemDatabase()
 {
     if (databaseAchievements.Count==0)
{
       for (int i = 0; i &lt; AchData.Count; i++)
       {
databaseAchievements.Add(new Achievement((int)AchData[i]["ID"], AchData[i]["Title"].ToString(), AchData[i]["Description"].ToString()));
       }
     Debug.Log("Database Created");
     }
 }

 

 

 

This will probably sum up the topic of achievements. Now you know how to trigger achievements, read data from files and pass information between scenes. If you have any questions write in comments.  Stay Awesome!

 

 

 

 

Author: indiedevart

Indie Games Developer

One thought on “Unity Achievements System Tutorial Pt.3”

  1. Hi, I feel stupid for asking this but after following your wonderful tutorial I have hit a roadblock whereby I had to make the databaseAchievements variable public instead to solve it as I was receiving “object reference is required to access non-static field” error. Is there anyway you can help me?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s