using UnityEngine;
using System.Collections;

public class Sprite : MonoBehaviour
{
    public Vector2 spriteLowerLeft;
    public Vector2 spriteSize;
    public string textureName;

    void Start()
    {
        MeshFilter meshFilter = (MeshFilter)gameObject.GetComponent("MeshFilter");
        if (meshFilter == null)
            meshFilter = (MeshFilter)gameObject.AddComponent("MeshFilter");

        MeshRenderer meshRenderer = (MeshRenderer)gameObject.GetComponent("MeshRenderer");
        if (meshRenderer == null)
            meshRenderer = (MeshRenderer)gameObject.AddComponent("MeshRenderer");

        meshRenderer.sharedMaterial = Resources.Load(textureName) as Material;

        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[] {
            new Vector3(-10f, -10f),
            new Vector3(-10f, 10f),
            new Vector3(10f, -10f),
            new Vector3(10f, 10f)
        };

        mesh.uv = new Vector2[] {
            new Vector2(0f, 0f),
            new Vector2(0f, 1f),
            new Vector2(1f, 0f),
            new Vector2(1f, 1f)
        };

        mesh.triangles = new int[] { 0, 1, 3, 0, 3, 2 };
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshFilter.mesh = mesh;
    }
}