Unity Achievements System Tutorial Pt.2

Previous Part

In this part I’ll demonstrate how to read values from JSON files and mapping them into Achievement Class. I’ll refer to scripts from  previous part, so you should have them working already.

[caption id=”attachment_2310” align=”alignnone” width=”630”]TestAch03 Awesome Icon, I know.[/caption]

Let’s crate JSON file first. If you are not familiar with this type of data, read my article about it here.

[
 {
 "ID":0,
 "Title": "Slayer",
 "Description": "kill 10 Enemies"
 },
 {
 "ID": 1,
 "Title": "Explorer",
 "Description": "Visit another planet"
 },
 {
 "ID" : 2,
 "Title": "First Blood",
 "Description": "Die!"
 },
 {
 "ID": 3,
 "Title": "Collecter",
 "Description": "collect 5 items"
 }
]

In this file we will store information that will be displayed in our achievement panel. We need Title and Description.

 

Next You will need a database of items, Items will be stored in generic List.

We take all vales form JSON file and add 2 new properties State and Sprite, sprite is loaded from array of sprites. Sprites must be sliced first to store them in array.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;

public class Database_Achievements : MonoBehaviour
{ 
  private List<Achievement> databaseAchievements = new List<Achievement>();
  private JsonData AchData;
  // Use this for initialization
  void Awake()
  {
     AchData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Achievements.json"));

     ConstructItemDatabase();
     Debug.Log(databaseAchievements[1].ID);
  }

  void ConstructItemDatabase()
  {
     for (int i = 0; i < AchData.Count; i++)
     {
databaseAchievements.Add(new Achievement((int)AchData[i]["ID"], AchData[i]["Title"].ToString(), AchData[i]["Description"].ToString()));
     }
  }
public Achievement FetchItembyID(int id)
  {
    for (int i = 0; i < databaseAchievements.Count; i++)
    {
      if (databaseAchievements[i].ID == id)
          return databaseAchievements[i];
    }
     return null;
  }
 public class Achievement
 {
  public string Title { get; set; }
  public string Describtion { get; set; }
  public Sprite image { get; set; }
  public bool active { get; set; }
  public int ID { get; set; }
  private Sprite[] sprites = UnityEngine.Resources.LoadAll<Sprite>("Achievements");
public Achievement(int newID, string name, string desc)
  {
     this.Title = name;
     this.Describtion = desc;
     this.active = false;
     this.image = sprites[newID];
     this.ID = newID;
  }
Achievement(string name)
  {
      this.Title = name;
  }

 }
}

We need a method for finding item in database, we will use FetchItembyID(int id) to find item by int ID number.

We add new method to the AchievementPanel Class.

void PassInformations(int ID)
{
  Database_Achievements.Achievement ach =database.FetchItembyID(ID);
  Debug.Log(ach.ID);
  panelAchievements.transform.GetChild(0).GetComponent<Text>().text = ach.Title;
  panelAchievements.transform.GetChild(1).GetComponent<Text>().text = ach.Describtion;
  panelAchievements.transform.GetChild(2).GetComponent<Image>().sprite = ach.image;
}

This will pass the values from database to UI.

I’m calling it inside existing CountClicks() method.

//We need to add following namespace
using UnityEngine.UI;
//and reference
public Database_Achievements database;
  void CountClicks()
  {
  numberOfClicks++;
    if (numberOfClicks >= 10 && !achievement01Unlocked)
    {
      PassInformations(1);
      StartCoroutine(PanelCall());
      achievement01Unlocked = true;
    }
  }

You need to add reference to the database. You can attach database script to any object, i would crate new empty one. In next part i’ll show you how to create a new scene for collection of all achievements. You will also learn how to pass information between scenes. Stay Awesome!