void CreateCell(int x, int y, bool full)
    {
        // Instantiate the cell animation and add it to the set.
        CellScript anim = Instantiate(cellAnim, transform.position + transform.rotation * new Vector3(x, y, 0), transform.rotation);

        anim.transform.parent = transform;
        cells.Add(new Cell(x, y), anim);

        // Set properties on the cell animation.
        anim.GetComponent <SpriteRenderer>().color = color;
        anim.GetComponent <Animator>().speed       = speed;

        if (full)
        {
            anim.SkipToFull();
        }
    }
    void Start()
    {
        // Load the Tiled map layer from the XML document.
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(File.ReadAllText(string.Format("tiled/{0}.tmx", pattern)));
        XmlNode layer = xmlDoc.SelectSingleNode("map/layer");

        int width  = int.Parse(layer.Attributes["width"].Value);
        int height = int.Parse(layer.Attributes["height"].Value);

        cells = new Dictionary <Cell, CellScript>();

        // Split the CSV data into rows, and parse them from bottom to top.
        string[] rows = layer["data"].InnerText.Trim().Split(new char[] { '\n' });
        Array.Reverse(rows);
        for (int y = 0; y < height; y++)
        {
            // Split each row into an array of ints.
            string[] cellsStr = rows[y].Split(new char[] { ',' });
            for (int x = 0; x < width; x++)
            {
                // Create a cell in the array for every nonzero int.
                if (int.Parse(cellsStr[x]) > 0)
                {
                    CreateCell(x, y, true);
                }
            }
        }

        // Get the effective length of the animation clip so we can schedule generation updates.
        generationDuration = cellAnim.GetComponent <Animator>().runtimeAnimatorController.animationClips.ToList <AnimationClip>().Max(clip => clip.length) / speed;

        // Schedule the first generation update.
        nextGenTime = initialDelay;
    }