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

public static class GameObjectTextExtension
{
    public static FloatingText FloatText(this GameObject o, string s)
    {
        return FloatingTextManager.Instance().CreateFloatingText(o, s);
    }
}

public class FloatingTextManager : MonoBehaviour {

    public GameObject Prefab = null;
    public FloatingText CreateFloatingText(GameObject obj, string s)
    {
        GameObject ftObj = null;
        if (obj != null)
        {
            ftObj = GameObject.Instantiate(Prefab, obj.transform.position, Quaternion.identity) as GameObject;
            ftObj.transform.SetParent(transform);

            ftObj.GetComponentInChildren<Text>().text = s;
        }

        FloatingText result = ftObj.GetComponent<FloatingText>();

        return result;

    }
    private static FloatingTextManager _instance;
    public static FloatingTextManager Instance()
    {
        if (_instance == null)
        {
            GameObject newObj = new GameObject("FloatingTextManager");
            _instance = newObj.AddComponent<FloatingTextManager>();
            _instance.Prefab = Resources.Load("FloatingText") as GameObject;
        }

        return _instance;
    }

    public FloatingTextManager()
    {
        if (_instance == null)
        {
            _instance = this;
        }
    }

    void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            if (this != _instance)
                Destroy(this.gameObject);
        }
    }

    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
