﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[ExecuteInEditMode]
public class PercentageText : MonoBehaviour
{
    public ProgressBar ProgressBar;
    private Text Percentage;

    void Awake()
    {
        Percentage = GetComponent<Text>();
        if (Percentage == null)
        {
            Debug.LogError("Failed to get a UnityEngine.UI.Text component in this GameObjct");
            return;
        }

        if (ProgressBar == null)
        {
            Debug.LogError("Failed to load ProgressBar in this PercentageText");
            return;
        }

        ProgressBar.OnPercentageChanged += percentage =>
        {
            Percentage.text = (percentage * 100.0f).ToString("F2");
        };
    }
}
