Re: How to make Achievements?
Posted: Fri Aug 19, 2016 3:45 pm
Thanks a lot! 

Support and discussion forum for Pixel Crushers products
https://www.pixelcrushers.com:443/phpbb/
Code: Select all
Variable["Achievement_MadeGirlAngry"] = true;
ShowAlert("Achievement: Made Girl Angry")
Code: Select all
Variable["Achievement_MadeGirlAngry"]==true
Code: Select all
Variable["Achievement_MadeGirlAngry"]==false
Code: Select all
AddAchievement("MadeGirlAngry");
ShowAlert("Achievement: Made Girl Angry")
Code: Select all
using UnityEngine;
using System;
using System.Collections.Generic;
using PixelCrushers.DialogueSystem;
public class Achievements: MonoBehaviour
{
[Serializable]
public class Achievement
{
public string name;
public GameObject icon;
}
public List<Achievement> achievements = new List<Achievement>();
private void Start()
{
Lua.RegisterFunction("AddAchievement", this, SymbolExtensions.GetMethodInfo(() => AddAchievement(string.Empty)));
UpdateIcons();
}
public void AddAchievement(string achievementName)
{
if (string.IsNullOrEmpty(achievementName))
{
Debug.LogError("AddAchievement was provided a blank achievement name");
}
else if (achievements.Find(x => string.Equals(x.name, achievementName)) == null)
{
Debug.LogError("Achievement '" + achievementName + "' is not defined in the Achievements component");
}
else
{
PlayerPrefs.SetInt(achievementName, 1);
}
}
public void UpdateIcons()
{
foreach (var achievement in achievements)
{
if (achievement != null)
{
if (string.IsNullOrEmpty(achievement.name))
{
Debug.LogError("Achievement name is blank");
}
else if (achievement.icon == null)
{
Debug.LogError("Achievement '" + achievement.icon + "' icon is unassigned");
}
else
{
var hasAchievement = PlayerPrefs.GetInt(achievement.name, 0) == 1;
achievement.icon.SetActive(hasAchievement);
}
}
}
}
}