using UnityEngine;
using System.Collections;

public class Billboard : MonoBehaviour {

    public Texture2D slide;


	void Awake () {
        Renderer ren = transform.GetComponentInChildren<Renderer>();
        ren.materials[1].mainTexture = slide;
        this.useGUILayout = false;

	}

    void OnGUI()
    {
        if (visible)
        {
            if (Time.frameCount <= 5) displayPercent = 1;
            displayPercent += Time.deltaTime * 3;
        }
        else if (displayPercent > 0)
        {
            displayPercent -= Time.deltaTime * 3;                  
        }

        if (displayPercent > 0)
        {
            displayPercent = Mathf.Clamp01(displayPercent);
            float w = Screen.width * displayPercent;
            float h = Screen.height * displayPercent;
            Rect rect = new Rect((Screen.width / 2 - w / 2), (Screen.height / 2 - h / 2), w, h);
            GUI.DrawTexture(rect, slide, ScaleMode.ScaleToFit);
        }
    }

    private bool visible = false;
    private float displayPercent = 0;

    void OnTriggerEnter (Collider other ) {
        if (other.transform.tag == "Player")
        {
            displayPercent = 0;
            visible = true;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.transform.tag == "Player")
        {
            visible = false;
        }
    }


}